|
| 1 | +package services; |
| 2 | + |
| 3 | +import com.amazonaws.auth.AWSStaticCredentialsProvider; |
| 4 | +import com.amazonaws.auth.BasicAWSCredentials; |
| 5 | +import com.amazonaws.regions.Regions; |
| 6 | +import com.amazonaws.services.s3.AmazonS3; |
| 7 | +import com.amazonaws.services.s3.AmazonS3ClientBuilder; |
| 8 | +import com.amazonaws.services.s3.model.*; |
| 9 | +import play.Application; |
| 10 | +import play.Configuration; |
| 11 | +import play.mvc.Http; |
| 12 | + |
| 13 | +import javax.inject.Inject; |
| 14 | +import java.io.File; |
| 15 | +import java.io.InputStream; |
| 16 | +import java.net.MalformedURLException; |
| 17 | +import java.net.URL; |
| 18 | +import java.util.UUID; |
| 19 | + |
| 20 | +public class S3ImageStore implements ImageStore { |
| 21 | + |
| 22 | + private static final String AWS_S3_BUCKET = "aws.s3.bucket"; |
| 23 | + private static final String AWS_ACCESS_KEY = "aws.access.key"; |
| 24 | + private static final String AWS_SECRET_KEY = "aws.secret.key"; |
| 25 | + |
| 26 | + private final String bucket; |
| 27 | + |
| 28 | + private final AmazonS3 s3Client; |
| 29 | + |
| 30 | + @Inject |
| 31 | + public S3ImageStore(Configuration config) { |
| 32 | + |
| 33 | + // You can create your own Amazon S3 bucket |
| 34 | + // There is a free tier up to a limit (https://aws.amazon.com/s3/pricing/) |
| 35 | + // Key and secret should never be stored in the source code (some one can get it from Github) |
| 36 | + // Instead export them as environment variable in your Terminal |
| 37 | + // $ export AWS_ACCESS_KEY=<Your AWS Access Key> |
| 38 | + // $ export AWS_SECRET_KEY=<Your AWS Secret Key> |
| 39 | + // When you compile your code the application.config will be updated with these values |
| 40 | + String accessKey = config.getString(AWS_ACCESS_KEY); |
| 41 | + String secretKey = config.getString(AWS_SECRET_KEY); |
| 42 | + BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); |
| 43 | + |
| 44 | + s3Client = AmazonS3ClientBuilder.standard() |
| 45 | + .withRegion(Regions.AP_SOUTH_1) |
| 46 | + .withCredentials(new AWSStaticCredentialsProvider(credentials)) |
| 47 | + .build(); |
| 48 | + |
| 49 | + bucket = config.getString(AWS_S3_BUCKET); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public String save(File file) { |
| 54 | + |
| 55 | + final String key = getKey(); |
| 56 | + |
| 57 | + PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, file); |
| 58 | + putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead); |
| 59 | + s3Client.putObject(putObjectRequest); |
| 60 | + |
| 61 | + return key; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public InputStream get(String id) { |
| 66 | + S3Object object = s3Client.getObject(bucket, id); |
| 67 | + S3ObjectInputStream objectContent = object.getObjectContent(); |
| 68 | + return objectContent; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public boolean delete(String id) { |
| 73 | + DeleteObjectsRequest dor = new DeleteObjectsRequest(bucket) |
| 74 | + .withKeys(id); |
| 75 | + s3Client.deleteObjects(dor); |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public URL downloadUrl(String id, Http.Request request) { |
| 81 | + |
| 82 | + try { |
| 83 | + return new URL("https://" + bucket + ".s3.amazonaws.com/" + id); |
| 84 | + } catch (MalformedURLException e) { |
| 85 | + e.printStackTrace(); |
| 86 | + return null; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private static String getKey() { |
| 91 | + return UUID.randomUUID().toString() + ".png"; |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments