Google 云存储Java XML API 文件上传并标记上传文件public

Google Cloud storage Java XML API file upload and mark uploaded file public

我正在使用 Google Cloud storage Java XML API 将文件从客户端上传到 Google Cloud Bucket,文件上传成功,但我希望上传的文件 public 可以访问(希望文件具有 public-读取权限),如何在上传的文件上设置 public-read ACL,从而使其 public 可访问。这是我使用的代码

httpTransport = GoogleNetHttpTransport.newTrustedTransport();


String p12Content = Files.readFirstLine(new File("key.p12"), Charset.defaultCharset());
            Preconditions.checkArgument(!p12Content.startsWith("Please"), p12Content);
            //[START snippet]
            // Build a service account credential.
Path path = Paths.get(MyFilePath);
    byte[] byteArray = java.nio.file.Files.readAllBytes(path);
HttpContent contentsend = new ByteArrayContent("text/plain", byteArray );

GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
                .setJsonFactory(JSON_FACTORY)
                .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
                .setServiceAccountScopes(Collections.singleton(STORAGE_SCOPE))
                .setServiceAccountPrivateKeyFromP12File(new File("key.p12"))

                .build();
            // Set up and execute a Google Cloud Storage request.
String URI = "https://storage.googleapis.com/" + BUCKET_NAME + "/myfile";
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
GenericUrl url = new GenericUrl(URI);
HttpRequest request = requestFactory.buildPutRequest(url,contentsend);
HttpResponse response = request.execute();

包含值为 "public-read" 的 "x-goog-acl" header,如下所示:

request.setHeader("x-goog-acl", "public-read");

有关更多信息,您可以查看这些文档页面:

https://cloud.google.com/storage/docs/reference-headers#xgoogacl https://cloud.google.com/storage/docs/access-control