【问题标题】:Google Appengine Cloud Storage谷歌 Appengine 云存储
【发布时间】: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:这些是我遵循的步骤:

  1. 我有一个基本工作的 JSP 示例应用程序 - 开发服务器和部署的版本都工作正常
  2. DataStore 正在工作 - 插入/更新/删除
  3. 我在this tutorial之后添加了云存储的示例代码
  4. 我关注this tutorial 安装 GCloud SDK 并进行初始化并设置默认身份验证

【问题讨论】:

  • 根据官方文档向我们展示您的尝试。
  • @ZigMandel 我已经在应用引擎上运行了一个 JSP 应用程序。我创建了一个测试 servlet 来测试云存储。我只想将几个字节写入测试文件。我现在在问题中添加了 servlet 代码。

标签: java google-app-engine google-cloud-storage


【解决方案1】:

Appengine 项目已通过其各自的存储桶进行身份验证。不需要额外的身份验证 - 一旦按照 https://cloud.google.com/appengine/docs/java/googlecloudstorageclient/setting-up-cloud-storage 创建了存储桶。

然后,您可以按照这些文档或 blobstore https://cloud.google.com/appengine/docs/java/blobstore/ 使用存储桶。

【讨论】:

  • 我浏览了您的第一个链接。问题可能是从本地部署访问。我像这样从 maven 启动项目: mvn appengine:devserver 我在哪里可以添加这个标志? --default_gcs_bucket_name [BUCKET_NAME]
  • 谢谢!当我部署到云时,访问会自动工作。现在问题只出在开发环境上。
  • 我试过这个......但这也没有用:mvn appengine:devserver -Ddefault_gcs_bucket_name=abcxyz.appsopt.com
  • 您这样做是为了测试 - 即测试您的代码是否适用于将数据放入 gcs,或者您是否尝试访问 gcs 中的实时数据以进行本地测试?前者应该可以工作 - 您不需要访问真正的 gcs 进行本地测试。后者有点棘手
  • 只是测试将东西放入 GCS 的代码。但是,不知何故它不适用于开发服务器。如果我上传它就可以了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-10
  • 2013-12-08
  • 2016-10-12
  • 1970-01-01
  • 2012-08-19
相关资源
最近更新 更多