如何使用 Java API 更新 Google 云存储中文件的 ACL

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

我正在尝试将图像上传到 google 云存储并添加

的 ACL
allUsers  READER

Java 代码和问题

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 ??       

}

我正在使用:

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

知道了

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;               
    }