|
| 1 | +package com.stubbornjava.common.aws; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStream; |
| 6 | +import java.io.InputStreamReader; |
| 7 | +import java.nio.charset.Charset; |
| 8 | + |
| 9 | +import org.jooq.lambda.Unchecked; |
| 10 | +import org.slf4j.Logger; |
| 11 | +import org.slf4j.LoggerFactory; |
| 12 | + |
| 13 | +import com.amazonaws.auth.AWSStaticCredentialsProvider; |
| 14 | +import com.amazonaws.auth.BasicAWSCredentials; |
| 15 | +import com.amazonaws.regions.Regions; |
| 16 | +import com.amazonaws.services.s3.AmazonS3; |
| 17 | +import com.amazonaws.services.s3.AmazonS3ClientBuilder; |
| 18 | +import com.amazonaws.services.s3.model.GetObjectRequest; |
| 19 | +import com.amazonaws.services.s3.model.ObjectMetadata; |
| 20 | +import com.amazonaws.services.s3.model.PutObjectRequest; |
| 21 | +import com.amazonaws.services.s3.model.S3Object; |
| 22 | +import com.amazonaws.services.s3.model.S3ObjectInputStream; |
| 23 | +import com.amazonaws.util.IOUtils; |
| 24 | +import com.google.common.base.Charsets; |
| 25 | +import com.google.common.io.CharStreams; |
| 26 | + |
| 27 | + |
| 28 | +public class S3Client { |
| 29 | + private static final Logger log = LoggerFactory.getLogger(S3Client.class); |
| 30 | + |
| 31 | + private final AmazonS3 client; |
| 32 | + |
| 33 | + private S3Client(AmazonS3 client) { |
| 34 | + this.client = client; |
| 35 | + } |
| 36 | + |
| 37 | + public void putBytes(String bucket, String key, byte[] bytes) { |
| 38 | + ObjectMetadata md = new ObjectMetadata(); |
| 39 | + md.setContentLength(bytes.length); |
| 40 | + PutObjectRequest request = new PutObjectRequest(bucket, key, new ByteArrayInputStream(bytes), md); |
| 41 | + client.putObject(request); |
| 42 | + } |
| 43 | + |
| 44 | + public void putString(String bucket, String key, String content) { |
| 45 | + putBytes(bucket, key, content.getBytes(Charset.forName(Charsets.UTF_8.name()))); |
| 46 | + } |
| 47 | + |
| 48 | + public void putInputStream(String bucket, String key, InputStream inputStream) { |
| 49 | + // This can blow up on large files but that's ok for now. |
| 50 | + byte[] bytes = Unchecked.supplier(() -> IOUtils.toByteArray(inputStream)).get(); |
| 51 | + putBytes(bucket, key, bytes); |
| 52 | + } |
| 53 | + |
| 54 | + public InputStream get(String bucket, String key) { |
| 55 | + GetObjectRequest request = new GetObjectRequest(bucket, key); |
| 56 | + S3Object response = client.getObject(request); |
| 57 | + return response.getObjectContent(); |
| 58 | + } |
| 59 | + |
| 60 | + public String getString(String bucket, String key) { |
| 61 | + GetObjectRequest request = new GetObjectRequest(bucket, key); |
| 62 | + S3Object response = client.getObject(request); |
| 63 | + try (S3ObjectInputStream is = response.getObjectContent()) { |
| 64 | + return CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8)); |
| 65 | + } catch (IOException ex) { |
| 66 | + throw new RuntimeException(ex); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public static S3Client fromAccessAndScret(String accessKey, String secretKey, Regions region) { |
| 71 | + BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secretKey); |
| 72 | + return new S3Client(AmazonS3ClientBuilder.standard() |
| 73 | + .withRegion(region) |
| 74 | + .withCredentials(new AWSStaticCredentialsProvider(awsCreds)) |
| 75 | + .build()); |
| 76 | + } |
| 77 | + |
| 78 | + public static void main(String[] args) { |
| 79 | + S3Client client = S3Client.fromAccessAndScret("key", "secret", Regions.US_EAST_1); |
| 80 | + client.putString("quickdraw-logos", "test.txt", "test"); |
| 81 | + client.putBytes("quickdraw-logos", "test2.txt", "test".getBytes(Charset.forName("utf8"))); |
| 82 | + client.putInputStream("quickdraw-logos", "test3.txt", new ByteArrayInputStream("test".getBytes(Charset.forName("utf8")))); |
| 83 | + |
| 84 | + log.debug(client.getString("quickdraw-logos", "test.txt")); |
| 85 | + } |
| 86 | +} |
0 commit comments