Guest User

Untitled

a guest
Jul 17th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. package com.jcruzmartini.g2llc;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. import java.net.URI;
  8. import java.net.URISyntaxException;
  9.  
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12.  
  13. import com.amazonaws.services.s3.AmazonS3Client;
  14. import com.amazonaws.services.s3.model.ObjectMetadata;
  15.  
  16. public class S3FileUploadManager {
  17.  
  18. private static final Logger LOGGER = LoggerFactory.getLogger(S3FileUploadManager.class);
  19.  
  20. private final AmazonS3Client s3;
  21. private final String bucketName;
  22.  
  23. public S3FileUploadManager(final AmazonS3Client s3, final String bucketName) {
  24. this.s3 = s3;
  25. this.bucketName = bucketName;
  26. }
  27.  
  28. public S3FileUploader upload(final String key, final String contentType, int size) {
  29. return new S3FileUploader(key, contentType, size);
  30. }
  31.  
  32. public S3FileUploader upload(final String key, final String contentType) {
  33. return new S3FileUploader(key, contentType);
  34. }
  35.  
  36. private void upload(final String key, final String contentType, final byte[] contents, final int size) {
  37. final ObjectMetadata metadata = new ObjectMetadata();
  38. metadata.setContentLength(size);
  39. metadata.setContentType(contentType);
  40.  
  41. final ByteArrayInputStream stream = new ByteArrayInputStream(contents, 0, size);
  42. s3.putObject(bucketName, key, stream, metadata);
  43. }
  44.  
  45. private class UploadStream extends ByteArrayOutputStream {
  46.  
  47. public UploadStream() {
  48. super();
  49. // TODO Auto-generated constructor stub
  50. }
  51.  
  52. public UploadStream(int size) {
  53. super(size);
  54. // TODO Auto-generated constructor stub
  55. }
  56.  
  57. @Override
  58. public void close() {
  59. }
  60.  
  61. public synchronized byte[] getBuffer() {
  62. return buf;
  63. }
  64. }
  65.  
  66. public class S3FileUploader {
  67. private final String key;
  68. private final String contentType;
  69. private final UploadStream stream;
  70.  
  71. public S3FileUploader(final String key, final String contentType, int size) {
  72. this.stream = new UploadStream( size);
  73. this.key = key;
  74. this.contentType = contentType;
  75. }
  76. public S3FileUploader(final String key, final String contentType) {
  77. this.stream = new UploadStream();
  78. this.key = key;
  79. this.contentType = contentType;
  80. }
  81.  
  82. public OutputStream getStream() {
  83. return stream;
  84. }
  85.  
  86. public void upload() {
  87. try {
  88. stream.flush();
  89. } catch (IOException e) {
  90. LOGGER.error("Error flushing stream", e);
  91. }
  92. S3FileUploadManager.this.upload(key, contentType, stream.getBuffer(), stream.size());
  93. }
  94.  
  95. public URI getTargetUri() {
  96. try {
  97. return new URI("s3", bucketName, '/' + key, null);
  98. } catch (final URISyntaxException e) {
  99. LOGGER.error("Error formatting URI. bucketName={}, key={}", bucketName, key, e);
  100. throw new RuntimeException(e);
  101. }
  102. }
  103. }
  104. }
Add Comment
Please, Sign In to add comment