Created
August 11, 2017 03:26
-
-
Save frankyaorenjie/ddbf00aa704171b25c24664601423d3d to your computer and use it in GitHub Desktop.
aws s3 v4 sign
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.amazonaws.HttpMethod; | |
import com.amazonaws.auth.AWSStaticCredentialsProvider; | |
import com.amazonaws.auth.BasicAWSCredentials; | |
import com.amazonaws.services.s3.AmazonS3; | |
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | |
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; | |
import java.net.URL; | |
public class Test { | |
public static void main(String[] args) { | |
String bjKey = "{bjKey}"; | |
String bjSecret = "{bjSecret}"; | |
String sgKey = "{sgKey}"; | |
String sgSecret = "{sgSecret}"; | |
BasicAWSCredentials creds = new BasicAWSCredentials(sgKey, sgSecret); | |
AmazonS3 s3 = AmazonS3ClientBuilder.standard() | |
.withCredentials(new AWSStaticCredentialsProvider(creds)) | |
.withRegion("ap-northeast-1") | |
.build(); | |
java.util.Date expiration = new java.util.Date(); | |
long milliSeconds = expiration.getTime(); | |
milliSeconds += 1000 * 60 * 60; // Add 1 hour. | |
expiration.setTime(milliSeconds); | |
GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest("{bucketName}", | |
"{objKey}"); | |
urlRequest.setMethod(HttpMethod.GET); | |
urlRequest.setExpiration(expiration); | |
URL url = s3.generatePresignedUrl(urlRequest); | |
System.out.println(url.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment