我有一个App引擎应用程序。我想使用云端存储来存储一些用户上传的文件。Google Appengine云存储
我想知道如何进行简单身份验证以访问每个应用程序引擎应用程序随附的单个免费存储桶。
我只想让应用程序本身与Cloud存储交谈,并且用户不需要与Cloud存储进行任何交互。每个文件都可以放在同一个文件夹中,只需要App引擎JVM就可以访问它。
是否有一种简单的身份验证方式,无需通过Oauth2等?一个简单的服务器到服务器的访问,如果可能的话!
编辑:这是一样的吗?
gcloud beta auth application-default login
编辑2:试过Gcloud上述缺省认证设置。仍然抛出相同的异常。
com.google.cloud.storage.StorageException: Invalid Credentials
测试代码,我使用:
private static Storage storage = null;
static {
storage = StorageOptions.getDefaultInstance().getService();
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter out = resp.getWriter();
out.println("Hello, world");
testCloudStorage();
}
private void testCloudStorage() throws IOException{
ByteArrayInputStream baIs = new ByteArrayInputStream("TEST FILE CONTENT".getBytes());
uploadFile("test-content.txt", baIs, "xyzabc.appspot.com");
}
/**
* Uploads a file to Google Cloud Storage to the bucket specified in the BUCKET_NAME
* environment variable, appending a timestamp to end of the uploaded filename.
*/
public String uploadFile(String fileName, InputStream inputStream, final String bucketName) throws IOException {
DateTimeFormatter dtf = DateTimeFormat.forPattern("-YYYY-MM-dd-HHmmssSSS");
DateTime dt = DateTime.now(DateTimeZone.UTC);
String dtString = dt.toString(dtf);
final String extendedFileName = fileName + dtString;
// the inputstream is closed by default, so we don't need to close it here
BlobInfo blobInfo =
storage.create(
BlobInfo
.newBuilder(bucketName, extendedFileName)
// Modify access list to allow all users with link to read file
.setAcl(new ArrayList<>(Arrays.asList(Acl.of(Acl.User.ofAllUsers(), Role.READER))))
.build(),
inputStream);
// return the public download link
return blobInfo.getMediaLink();
}
编辑3:这是我遵循的步骤:
- 我有一个基本的工作JSP示例应用程序 - 无论devserver并部署版本正常工作
- DataStore正在工作 - 插入/更新/删除
- 我添加以下this tutorial
- 对于云存储的样本代码,我跟着this tutorial安装GCloud SDK和做初始化和设置的缺省auth
向我们展示您基于官方文档所尝试的内容。 –
@ZigMandel我已经有一个JSP应用程序在应用程序引擎上运行。我创建了一个测试servlet来测试云存储。我只想写一个字节到测试文件。我现在在问题中添加了servlet代码。 – Teddy