Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement