1
votes

I'm using Google Cloud storage Java XML API to upload file from client into the Google Cloud Bucket , The file is getting uploaded succesfully but i want the uploaded file to be publicly accessible ( the want the file to have public-read permission) , how to set public-read ACL on the uploaded file and thus making it publicly accessible . Here is the code i've used

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();
1

1 Answers

2
votes

Include an "x-goog-acl" header with the value "public-read", like this:

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

For more, you could check these documentation pages:

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