【发布时间】:2017-02-04 12:44:27
【问题描述】:
我有一个 App 引擎应用程序。我想使用 Cloud Storage 来存储一些用户上传的文件。
我想知道如何进行简单的身份验证以访问每个应用引擎应用程序附带的单个免费存储桶。
我只希望应用程序本身与云存储对话,用户无需与云存储进行任何交互。每个文件都可以放在同一个文件夹中,并且只能由 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 示例应用程序 - 开发服务器和部署的版本都工作正常
- DataStore 正在工作 - 插入/更新/删除
- 我在this tutorial之后添加了云存储的示例代码
- 我关注this tutorial 安装 GCloud SDK 并进行初始化并设置默认身份验证
【问题讨论】:
-
根据官方文档向我们展示您的尝试。
-
@ZigMandel 我已经在应用引擎上运行了一个 JSP 应用程序。我创建了一个测试 servlet 来测试云存储。我只想将几个字节写入测试文件。我现在在问题中添加了 servlet 代码。
标签: java google-app-engine google-cloud-storage