Aws-sdk-java: How to upload a folder to S3 with public read permission

Created on 13 Mar 2019  路  2Comments  路  Source: aws/aws-sdk-java

I am using TransferManager. uploadDirectory() to upload a folder with htmls, images and videos files to s3. But can't access the content after upload successful.

I notice that the below uploadDirectory method have the argument about permission but have no idea how to use it.

Is there any example about this? Thanks a lot.

public MultipleFileUpload uploadDirectory(String bucketName,
                                          String virtualDirectoryKeyPrefix,
                                          File directory,
                                          boolean includeSubdirectories,
                                          ObjectMetadataProvider metadataProvider,
                                          ObjectTaggingProvider taggingProvider,
                                          ObjectCannedAclProvider cannedAclProvider)

guidance

Most helpful comment

Hi @megolee, take a look at the Access Control List (ACL) Overview.

The ObjectCannedAclProvider will receive a callback via the provideObjectCannedAcl method, allowing you to customize the canned ACL for each object being uploaded.

Here's an example on how to use the ObjectCannedAclProvider interface:

ObjectCannedAclProvider cannedAclProvider = new ObjectCannedAclProvider() {

        public CannedAccessControlList provideObjectCannedAcl(File file) {
                // If this file is a JPEG, it will be granted private canned ACL; 
                // otherwise, the file will receive the public-read canned ACL
                if (isJPEG(file)) {
                        return CannedAccessControlList.Private;
                }
                return CannedAccessControlList.PublicRead;
        }
}

MultipleFileUpload multiUpload = transferManager.uploadDirectory(bucketName, keyPrefix, 
              directory, includeSubdirectories, null, null, cannedAclProvider);

Let us know if that helps.

All 2 comments

Hi @megolee, take a look at the Access Control List (ACL) Overview.

The ObjectCannedAclProvider will receive a callback via the provideObjectCannedAcl method, allowing you to customize the canned ACL for each object being uploaded.

Here's an example on how to use the ObjectCannedAclProvider interface:

ObjectCannedAclProvider cannedAclProvider = new ObjectCannedAclProvider() {

        public CannedAccessControlList provideObjectCannedAcl(File file) {
                // If this file is a JPEG, it will be granted private canned ACL; 
                // otherwise, the file will receive the public-read canned ACL
                if (isJPEG(file)) {
                        return CannedAccessControlList.Private;
                }
                return CannedAccessControlList.PublicRead;
        }
}

MultipleFileUpload multiUpload = transferManager.uploadDirectory(bucketName, keyPrefix, 
              directory, includeSubdirectories, null, null, cannedAclProvider);

Let us know if that helps.

@debora-ito Yes, it helps me well. Thank you so much.

Was this page helpful?
0 / 5 - 0 ratings