2

我正在使用 Google Cloud Storage 存储图像。我注意到您可以上传多个文件,但我看不到任何有关一次性授予所有文件权限的信息。我授予对存储桶的完全访问权限,但其中的所有文件都没有获得相同的权限,我必须一一给予。我还实现了一个处理程序以编程方式上传文件。任何想法如何在上传时完全访问我的图像?请看下面的代码,我使用的是 Spring MVC 控制器:

@RequestMapping(value="/gcs/userimages/{id}", method = RequestMethod.PUT)
  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws   IOException {
      GcsService gcsService =
                   GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
    //  GcsFileOptions go = new GcsFileOptions(Builder.);
      GcsFileOptions options = new   GcsFileOptions.Builder().mimeType("image/png").build();
    GcsOutputChannel outputChannel =
        gcsService.createOrReplace(getFileName(req), options);
    //outputChannel.write(ByteBuffer.wrap(content));
    copy(req.getInputStream(), Channels.newOutputStream(outputChannel));
  }

  private GcsFilename getFileName(HttpServletRequest req) {
    String[] splits = req.getRequestURI().split("/", 4);
    if (!splits[0].equals("") || !splits[1].equals("gcs")) {
      throw new IllegalArgumentException("The URL is not formed as expected. " +
          "Expecting /gcs/<bucket>/<object>");
    }
    return new GcsFilename(splits[2], splits[3]);
  } 
4

2 回答 2

3

文档中引用

默认对象 ACL

每个存储桶都有一个默认对象 ACL,并且此 ACL 将应用于上传到该存储桶的所有对象,而无需预定义 ACL。

因此,如果您将默认对象 ACL编辑为您需要的任何内容,则该存储桶中上传的每个文件都将获得默认 ACL

阅读文档以了解编辑这些参数的可用方法:更改默认对象 ACL

您还可以使用JSON API,您可以在其中使用APIs Explorer进行测试

于 2013-10-12T11:17:55.223 回答
2

我想我找到了一种方法。我只是将代码更改为:

GcsFileOptions options = new   GcsFileOptions.Builder().acl("public_read").mimeType("image/png").build();

现在每次上传文件时,每个人都有读取权限。

于 2013-10-12T12:34:50.480 回答