How to update ACL of a file in Google Cloud Storage using Java API

197 views Asked by At

I am trying to upload a image to google cloud storage and add ACL of

allUsers  READER

Java code and question

public static StorageObject uploadSimpleImageAndMakeItPublic(
        Storage storage, String bucketName, String objectName,
        InputStream imageData) throws IOException {
    InputStreamContent mediaContent = new InputStreamContent("image/png", imageData);
    Storage.Objects.Insert insertObject = storage.objects().insert(bucketName, null, mediaContent).setName(objectName);
    insertObject.getMediaHttpUploader().setDisableGZipContent(true);
    StorageObject uploadedImage = insertObject.execute();

    // new acl to make it public
    ObjectAccessControl publicACLItem = new ObjectAccessControl();
    publicACLItem.setEntity("allUsers");
    publicACLItem.setRole("READER");

    uploadedImage.getAcl().add(publicACLItem);

    // HOW TO UPDATE THIS ACL NOW ???
    // AND HOW TO FETCH THE PUBLIC URL ??       

}

I am using:

 <dependency>
      <groupId>com.google.apis</groupId>
      <artifactId>google-api-services-storage</artifactId>
      <version>v1-rev18-1.19.0</version>
    </dependency>
1

There are 1 answers

0
Bhuvan On BEST ANSWER

GOT IT

public static StorageObject uploadSimpleImageAndMakeItPublic(
            Storage storage, String bucketName, String objectName,
            InputStream imageData) throws IOException {
        InputStreamContent mediaContent = new InputStreamContent("image/png", imageData);
        Storage.Objects.Insert insertObject = storage.objects()
                            .insert(bucketName, null, mediaContent).setName(objectName);
        insertObject.getMediaHttpUploader().setDisableGZipContent(true);
        StorageObject uploadedImage = insertObject.execute();

        // new acl to make it public
        ObjectAccessControl publicACLItem = new ObjectAccessControl();
        publicACLItem.setEntity("allUsers");
        publicACLItem.setRole("READER");
        Insert insert = storage.objectAccessControls().insert(bucketName, objectName, publicACLItem);
        insert.execute();
        return uploadedImage;               
    }