Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. public boolean uploadDirectoryToAmazon(String directory, String bucketName, String s3DirectoryKey) {
  2. boolean result = false;
  3.  
  4. try {
  5. LOGGER.info("Uploading a directory to S3");
  6.  
  7. BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretAccessKey);
  8. AWSStaticCredentialsProvider awsStaticCredentialsProvider = new AWSStaticCredentialsProvider(credentials);
  9.  
  10. AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
  11. .withCredentials(awsStaticCredentialsProvider)
  12. .withRegion(amazonS3Region)
  13. .build();
  14.  
  15. //PutObjectResult putObjectResult = s3Client.putObject(putObjectRequest);
  16. //http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/transfer/TransferManager.html
  17. TransferManager transferManager = TransferManagerBuilder.
  18. standard().
  19. withS3Client(s3Client)
  20. .build();
  21.  
  22.  
  23. ObjectMetadataProvider objectMetadataProvider = new ObjectMetadataProvider() {
  24.  
  25. @Override
  26. public void provideObjectMetadata(File file, ObjectMetadata metadata) {
  27. LOGGER.info("objectMetadataProvider: file:"+file.getName());
  28.  
  29. }
  30.  
  31. };
  32.  
  33.  
  34. File dirToUpload = new File(directory);
  35. MultipleFileUpload uploadDirectoryResult = transferManager.uploadDirectory(bucketName, s3DirectoryKey, dirToUpload, false, objectMetadataProvider);
  36.  
  37. //Call method to log the progress
  38. logProgress(uploadDirectoryResult);
  39.  
  40. result = true;
  41. transferManager.shutdownNow();
  42. } catch (AmazonServiceException ase) {
  43. LOGGER.error("Caught an AmazonServiceException, which means your request made it to Amazon S3, but was rejected with an error response for some reason.");
  44. LOGGER.error("Error Message: " + ase.getMessage());
  45. LOGGER.error("HTTP Status Code: " + ase.getStatusCode());
  46. LOGGER.error("AWS Error Code: " + ase.getErrorCode());
  47. LOGGER.error("Error Type: " + ase.getErrorType());
  48. LOGGER.error("Request ID: " + ase.getRequestId());
  49. } catch (AmazonClientException ace) {
  50. 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.");
  51. LOGGER.error("Error Message: " + ace.getMessage());
  52. }
  53. return result;
  54. }
  55.  
  56. s3Client.setObjectAcl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement