gpt4 book ai didi

java - Google云存储Java XML API文件上传并将上传的文件标记为公开

转载 作者:行者123 更新时间:2023-11-30 03:27:52 26 4
gpt4 key购买 nike

我正在使用 Google Cloud storage Java XML API 将文件从客户端上传到 Google Cloud Bucket 中,该文件正在成功上传,但我希望上传的文件可以公开访问(想要文件具有公共(public)读取权限),如何在上传的文件上设置公共(public)读取 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” 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

关于java - Google云存储Java XML API文件上传并将上传的文件标记为公开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29747579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com