Skip to content

Instantly share code, notes, and snippets.

@rafayali
Created November 21, 2014 11:14
Show Gist options
  • Save rafayali/fe89a946b00b9b7e78f7 to your computer and use it in GitHub Desktop.
Save rafayali/fe89a946b00b9b7e78f7 to your computer and use it in GitHub Desktop.
Uploads buffered image to amazon S3 server
public static void uploadBufferedImageToServer(BufferedImage image, String fileName, String imageType) throws IOException, NullPointerException {
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
ImageIO.write(image, "png", outstream);
byte[] buffer = outstream.toByteArray();
InputStream is = new ByteArrayInputStream(buffer);
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType("image/" + imageType);
meta.setContentLength(buffer.length);
AmazonS3 s3client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey));
s3client.putObject(new PutObjectRequest(awsBucketName, fileName, is, meta).withCannedAcl(CannedAccessControlList.PublicRead));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment