亚马逊 Java S3 SDK - 上传目录

Amazon Java S3 SDK - UploadDirectory

我正在使用 Java Amazon S3 SDK 上传文件 我想知道 - 当使用 transferManager 上传目录时 - 是否有更好的方法将 Acl 设置为 public-read

这是我的代码

public boolean uploadDirectoryToAmazon(String directory, String bucketName, String s3DirectoryKey) {
    boolean result = false;

    try {
        LOGGER.info("Uploading a directory to S3");

        BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretAccessKey); 
        AWSStaticCredentialsProvider awsStaticCredentialsProvider = new AWSStaticCredentialsProvider(credentials);

        AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                .withCredentials(awsStaticCredentialsProvider)
                .withRegion(amazonS3Region)
                .build();

        //PutObjectResult putObjectResult = s3Client.putObject(putObjectRequest);
        //http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/transfer/TransferManager.html
        TransferManager transferManager = TransferManagerBuilder.
                standard().
                withS3Client(s3Client)
                .build();


                    ObjectMetadataProvider objectTaggingProvider = new ObjectMetadataProvider() {
            public void provideObjectMetadata(File file, ObjectMetadata metadata) {
                if (BooleanUtils.isTrue(isPublic)) {
                    metadata.setHeader(Headers.S3_CANNED_ACL, CannedAccessControlList.PublicRead);
                }
            }
        };

        File dirToUpload = new File(directory);
        MultipleFileUpload uploadDirectoryResult = transferManager.uploadDirectory(bucketName, s3DirectoryKey, dirToUpload, false, objectMetadataProvider);

        //Call method to log the progress
        logProgress(uploadDirectoryResult);            

        result = true;  
        transferManager.shutdownNow();
     } catch (AmazonServiceException ase) {
        LOGGER.error("Caught an AmazonServiceException, which means your request made it to Amazon S3, but was rejected with an error response for some reason.");
        LOGGER.error("Error Message:    " + ase.getMessage());
        LOGGER.error("HTTP Status Code: " + ase.getStatusCode());
        LOGGER.error("AWS Error Code:   " + ase.getErrorCode());
        LOGGER.error("Error Type:       " + ase.getErrorType());
        LOGGER.error("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        LOGGER.error("Caught an AmazonClientException, which means the client encountered an internal error while trying to communicate with S3, such as not being able to access the network.");
        LOGGER.error("Error Message: " + ace.getMessage());
    }
    return result;
}

其他文件上传选项具有指定 ACL 的简单方法 - 只是想知道是否有更简单的方法用于 uploadDirectory 命令

谢谢 达米恩

由于无法提出替代解决方案而采用上述解决方案

您可以使用 TransferManager class 的替代方法:

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

ObjectCannedAclProvider cannedAclProvider = new ObjectCannedAclProvider() {
        public CannedAccessControlList provideObjectCannedAcl(File file) {
                return CannedAccessControlList.PublicRead;
        }
}

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

require gradle 依赖项是:compile("com.amazonaws:aws-java-sdk:1.11.519")

注意:AWS sdk-java 线程中有人提出了类似的问题。 https://github.com/aws/aws-sdk-java/issues/1938