· 6 years ago · Jul 23, 2019, 04:18 PM
1package pe.com.movistar.vass.backendpriority.util;
2
3import java.io.File;
4
5import org.slf4j.Logger;
6import org.slf4j.LoggerFactory;
7
8import com.amazonaws.AmazonServiceException;
9import com.amazonaws.SdkClientException;
10import com.amazonaws.auth.AWSStaticCredentialsProvider;
11import com.amazonaws.auth.BasicAWSCredentials;
12import com.amazonaws.services.s3.AmazonS3;
13import com.amazonaws.services.s3.AmazonS3ClientBuilder;
14import com.amazonaws.services.s3.model.CannedAccessControlList;
15import com.amazonaws.services.s3.model.ObjectMetadata;
16import com.amazonaws.services.s3.model.PutObjectRequest;
17
18/**
19 *
20 * @author VASSLATAM
21 * @category AWS-CLIENT
22 *
23 * This class provides a connection to AWS S3 specifically using accessKey and
24 * secretKey. All this content but comments are property of VASSLATAM.
25 *
26 *
27 */
28public class BucketeerClient {
29
30 private final static String clientRegion = "us-east-1";
31 private final static String bucketName = "bucketeer-76986011-cbde-430c-9e8c-6506872e974e";
32 private final static String accessKey = "AKIAJW44E5AITW24R7CA";
33 private final static String secretKey = "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";
34 private final static String publicPath = "public/";
35 private final static String bucketRootURI = "https://bucketeer-76986011-cbde-430c-9e8c-6506872e974e.s3.amazonaws.com/";
36
37 public final static String BUCKET_URI = (bucketRootURI + publicPath);
38
39 private static final Logger LOGGER = LoggerFactory.getLogger(BucketeerClient.class);
40
41 public static boolean bucketeerUpload(final String filePath, final String fileNameWithExt, final String contentType) {
42
43 boolean executed = false;
44 try {
45 BasicAWSCredentials creds = new BasicAWSCredentials(accessKey, secretKey);
46 AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds)).withRegion(clientRegion).build();
47
48 final String finalLocation = publicPath + fileNameWithExt;
49
50 PutObjectRequest request = new PutObjectRequest(bucketName, finalLocation, new File(filePath));
51 request.withCannedAcl(CannedAccessControlList.PublicRead);
52
53 //disposition = downloadable
54 final String disposition = "attachment; filename=" + fileNameWithExt;
55 ObjectMetadata metadata = new ObjectMetadata();
56 metadata.setContentType(contentType);
57 metadata.setContentDisposition(disposition);
58 metadata.addUserMetadata("x-amz-meta-title", finalLocation);
59 request.setMetadata(metadata);
60 s3Client.putObject(request);
61 executed = true;
62 } catch (AmazonServiceException e) {
63 //sdk da play pero amazon no se quiere dar play
64 LOGGER.error(e.getMessage());
65 LOGGER.error(e.getCause().getMessage());
66 } catch (SdkClientException e) {
67 //sdk no da play no puede dark play
68 LOGGER.error(e.getMessage());
69 LOGGER.error(e.getCause().getMessage());
70 }
71
72 return executed;
73 }
74
75 public static boolean bucketeerDoesExist(final String fileNameWithExt) {
76
77 boolean exists = false;
78 try {
79 final BasicAWSCredentials creds = new BasicAWSCredentials(accessKey, secretKey);
80 final AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
81 .withCredentials(new AWSStaticCredentialsProvider(creds)).withRegion(clientRegion).build();
82
83 exists = s3Client.doesObjectExist(bucketName, (publicPath + fileNameWithExt));
84
85 } catch (AmazonServiceException e) {
86 //sdk da play pero amazon no se quiere dar play
87 LOGGER.error(e.getMessage());
88 LOGGER.error(e.getCause().getMessage());
89 } catch (SdkClientException e) {
90 //sdk no da play no puede dark play
91 LOGGER.error(e.getMessage());
92 LOGGER.error(e.getCause().getMessage());
93 }
94
95 return exists;
96 }
97
98 /*
99 public static void main(String[] args) throws IOException {
100
101 bucketeerUpload("C://Users//Javier//Downloads/TuentiAgile.png", "testing2.png");
102 bucketeerDoesExist("testing2.png");
103
104 */
105}