Google云存储Java XML API文件上载并将上载的文件标记为公共
我正在使用Google云存储Java XML API文件上载并将上载的文件标记为公共,java,google-cloud-storage,Java,Google Cloud Storage,我正在使用Google Cloud storage Java XML API将文件从客户端上传到Google Cloud Bucket,文件已成功上传,但我希望上传的文件可以公开访问(希望文件具有公开读取权限),如何在上载的文件上设置public readACL,从而使其可公开访问。这是我使用的代码 httpTransport = GoogleNetHttpTransport.newTrustedTransport(); String p12Content = Files.readFirst
Google Cloud storage Java XML API
将文件从客户端上传到Google Cloud Bucket,文件已成功上传,但我希望上传的文件可以公开访问(希望文件具有公开读取权限),如何在上载的文件上设置public read
ACL,从而使其可公开访问。这是我使用的代码
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”头,如下所示:
request.setHeader("x-goog-acl", "public-read");
有关更多信息,您可以查看以下文档页面:
我想知道如何使用Google JSON API和
存储.Objects.Insert
对象来实现这一点。将“predefinedAcl”参数设置为“publicRead”。或者,您可以使用请求正文的“acl”部分来指定特定的acl。