Skip to content

Instantly share code, notes, and snippets.

@stephen-maina
Last active August 29, 2015 14:19
Show Gist options
  • Save stephen-maina/10a90044428ffbbbec60 to your computer and use it in GitHub Desktop.
Save stephen-maina/10a90044428ffbbbec60 to your computer and use it in GitHub Desktop.
AWS upload file
// save uploaded file to a defined location on the server
private Response saveFile(InputStream uploadedInputStream, String fileName,
String name, String description, String albumId, String userId) {
try {
File tempFile = File.createTempFile("doc-" + userId + "-"
+ albumId, ".tmp");
// tempFile.createNewFile();
tempFile.deleteOnExit();
File thumbFile = File.createTempFile("doc-" + userId + "-"
+ albumId + "thumb", ".tmp");
thumbFile.deleteOnExit();
try (OutputStream outpuStream = new FileOutputStream(tempFile)) {
IOUtils.copy(uploadedInputStream, outpuStream);
}
BufferedImage thumbnail = Scalr.resize(ImageIO.read(tempFile),
Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, 100, 100,
Scalr.OP_ANTIALIAS);
ImageIO.write(thumbnail, "jpeg", thumbFile);
TransferManager tx = new TransferManager(
new PropertiesCredentials(
Photo.class
.getResourceAsStream("/com/api/AwsCredentials.properties")));
AmazonS3 s3Conn = new AmazonS3Client(
new PropertiesCredentials(
Photo.class
.getResourceAsStream("/com/api/AwsCredentials.properties")));
String key = albumId + "/" + fileName;
String thumbKey = albumId + "/thumb-" + fileName;
java.util.List<Bucket> buckets = s3Conn.listBuckets();
if (!buckets.contains("doc-photo")) {
CreateBucketRequest createBucket = new CreateBucketRequest(
"doc-photo")
.withCannedAcl(CannedAccessControlList.PublicRead);
s3Conn.createBucket(createBucket);
}
// PutObjectResult resFull=s3Conn.putObject("doc-" +
// /*userId123+ "-" + albumId, key, tempFile);
Upload photoUpload = tx.upload(new PutObjectRequest(
"doc-photo", key, tempFile)
.withCannedAcl(CannedAccessControlList.PublicRead));
Upload thumbUpload = tx.upload(new PutObjectRequest(
"doc-photo", thumbKey, thumbFile)
.withCannedAcl(CannedAccessControlList.PublicRead));
// TODO:return asynchronous message of transferred bytes.
while (photoUpload.isDone() == false) {
// used transferred for showing % uploaded content
Long transfered = photoUpload.getProgress()
.getBytesTransfered();
}
while (thumbUpload.isDone() == false) {
Long transfered = thumbUpload.getProgress()
.getBytesTransfered();
}
tx.shutdownNow();
if (!photoUpload.isDone() || !thumbUpload.isDone()) {
return Response
.status(Status.INTERNAL_SERVER_ERROR)
.entity("{\"photo\" : \""
+ name
+ "\" , \"status\" : \"not uploaded\",\"error\" : {"
+ utils.createErrorResponse(
Status.INTERNAL_SERVER_ERROR,
"Oops it seems an error occured while uploading your photo, please try again.")
+ " }}").cacheControl(utils.cacheControl())
.build();
}
return uploadToDatabase(albumId, name, description,
"http://s3.amazonaws.com/doc-photo/" + key,
"http://s3.amazonaws.com/doc-photo/" + thumbKey,
userId);
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
return Response
.status(Status.INTERNAL_SERVER_ERROR)
.entity("{\"photo\" : \""
+ name
+ "\" , \"status\" : \"not uploaded\",\"error\" : {"
+ utils.createErrorResponse(
Status.INTERNAL_SERVER_ERROR,
"Oops it seems an error occured while uploading your photo, please try again.")
+ " }}").cacheControl(utils.cacheControl()).build();
} finally {
if (uploadedInputStream != null) {
try {
uploadedInputStream.close();
} catch (IOException e) {
log.error(e.getMessage());
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment