Skip to content

Instantly share code, notes, and snippets.

@andrisro
Created May 8, 2019 08:56
Show Gist options
  • Save andrisro/1b3831927b64a6a47a849046a980d730 to your computer and use it in GitHub Desktop.
Save andrisro/1b3831927b64a6a47a849046a980d730 to your computer and use it in GitHub Desktop.
Java AWS S3 Presigned URI Example
package ...;
import com.amazonaws.HttpMethod;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import java.net.URL;
import java.nio.file.FileAlreadyExistsException;
import java.util.Date;
public abstract class PresignedURIMethods {
public static final Logger LOG = LogManager.getLogger(PresignedURIMethods.class);
//method for downloading objects
public static URL generatePresignedUrlGetObject(AmazonS3Client amazonS3Client, String bucketName, String objectKey, S3PresignedUrlExpiration s3PresignedUrlExpirationTime) {
LOG.debug("generate presigned url get object " + objectKey + " from bucket " + bucketName);
Date expirationDate = s3PresignedUrlExpirationTime.getExprationDate();
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey).withMethod(HttpMethod.GET).withExpiration(expirationDate);
URL url = amazonS3Client.generatePresignedUrl(generatePresignedUrlRequest);
LOG.debug("generated presigned url " + url.toString());
return url;
}
//method for uploading / update objects
public static URL generatePresignedUrlPutObject(AmazonS3Client amazonS3Client, String bucketName, String filePath, boolean overwriteIfAlreadyExist)
throws FileAlreadyExistsException {
LOG.debug("generate presigned url put object to path " + filePath + " in bucket " + bucketName);
Date expirationDate = S3PresignedUrlExpiration.PURI_UPLOAD_FILE.getExprationDate();
if(!overwriteIfAlreadyExist && amazonS3Client.doesObjectExist(bucketName, filePath)) {
LOG.warn("Bucket fie, exists and should not be overwritten: " + filePath);
throw new FileAlreadyExistsException(MessageConstants.S3_FILE_EXISTS_MESSAGE);
}
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, filePath).withMethod(HttpMethod.PUT).withExpiration(expirationDate);
return amazonS3Client.generatePresignedUrl(generatePresignedUrlRequest);
}
public static boolean doesDocumentExistInS3(AmazonS3Client amazonS3Client, String bucketName, String objectKey) {
return amazonS3Client.doesObjectExist(bucketName, objectKey);
}
}
import java.util.Date;
public enum S3PresignedUrlExpiration {
PURI_UPLOAD_FILE(1000 * 2), PURI_READ_FILE(1000 * 1);
private final long expirationTime;
S3PresignedUrlExpiration(long expirationTime) {
this.expirationTime = expirationTime;
}
public long getExpirationTime() {
return expirationTime;
}
public Date getExprationDate(){
Date date = new Date();
long expirationTimeLong = date.getTime() + expirationTime;
date.setTime(expirationTimeLong);
return date;
}
public long getExprationTimestampAsSeconds(){
Date date = new Date();
long expirationTimeLong = date.getTime() + expirationTime;
return expirationTimeLong/1000;
}
}
@andrisro
Copy link
Author

andrisro commented May 8, 2019

S3 Maven Import
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3 --> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-s3</artifactId> <version>1.11.483</version> </dependency>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment