Java Code Examples for com.amazonaws.services.s3.AmazonS3#generatePresignedUrl()

The following examples show how to use com.amazonaws.services.s3.AmazonS3#generatePresignedUrl() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AmazonS3Storage.java    From thunderbit with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public F.Promise<Result> getDownload(String key, String name) {
    GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key);
    ResponseHeaderOverrides responseHeaders = new ResponseHeaderOverrides();
    responseHeaders.setContentDisposition("attachment; filename="+name);
    generatePresignedUrlRequest.setResponseHeaders(responseHeaders);

    AmazonS3 amazonS3 = new AmazonS3Client(credentials);

    try {
        URL url = amazonS3.generatePresignedUrl(generatePresignedUrlRequest);

        return F.Promise.pure(redirect(url.toString()));
    } catch (AmazonClientException ace) {
        logAmazonClientException (ace);
        return F.Promise.pure(internalServerError(error.render()));
    }
}
 
Example 2
Source File: S3PresignUrlStep.java    From pipeline-aws-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected Object run() throws Exception {
	final String bucket = this.step.getBucket();
	final String key = this.step.getKey();

	Preconditions.checkArgument(bucket != null && !bucket.isEmpty(), "Bucket must not be null or empty");
	Preconditions.checkArgument(key != null && !key.isEmpty(), "Key must not be null or empty");

	EnvVars envVars = this.getContext().get(EnvVars.class);
	AmazonS3 s3 = AWSClientFactory.create(this.step.createS3ClientOptions().createAmazonS3ClientBuilder(), envVars);
	Date expiration = DateTime.now().plusSeconds(this.step.getDurationInSeconds()).toDate();
	URL url = s3.generatePresignedUrl(bucket, key, expiration, this.step.getHttpMethod());
	return url.toString();
}
 
Example 3
Source File: GeneratePresignedPutUrl.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    final String USAGE = "\n" +
            "To run this example, supply the name of an S3 bucket and a file to\n" +
            "upload to it.\n" +
            "\n" +
            "Ex: GeneratePresignedPutUrl <bucketname> <filename>\n";

    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String bucket_name = args[0];
    String key_name = args[1];

    System.out.format("Creating a pre-signed URL for uploading %s to S3 bucket %s...\n", key_name, bucket_name);
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();

    // Set the pre-signed URL to expire after 12 hours.
    java.util.Date expiration = new java.util.Date();
    long expirationInMs = expiration.getTime();
    expirationInMs += 1000 * 60 * 60 * 12;
    expiration.setTime(expirationInMs);

    try {
        GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucket_name, key_name)
                .withMethod(HttpMethod.PUT)
                .withExpiration(expiration);
        URL url = s3.generatePresignedUrl(generatePresignedUrlRequest);
        //print URL
        System.out.println("\n\rGenerated URL: " + url.toString());
        //Print curl command to consume URL
        System.out.println("\n\rExample command to use URL for file upload: \n\r");
        System.out.println("curl --request PUT --upload-file /path/to/" + key_name + " '" + url.toString() + "' -# > /dev/null");
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
}
 
Example 4
Source File: Acl.java    From cloudExplorer with GNU General Public License v3.0 5 votes vote down vote up
String setACLurl(String object, String access_key, String secret_key, String endpoint, String bucket) {
    String URL = null;
    try {
        AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key);
        AmazonS3 s3Client = new AmazonS3Client(credentials,
                new ClientConfiguration());
         if (endpoint.contains("amazonaws.com")) {
            String aws_endpoint = s3Client.getBucketLocation(new GetBucketLocationRequest(bucket));
            if (aws_endpoint.contains("US")) {
                s3Client.setEndpoint("https://s3.amazonaws.com");
            } else if (aws_endpoint.contains("us-west")) {
                s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
            } else if (aws_endpoint.contains("eu-west")) {
                s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
            } else if (aws_endpoint.contains("ap-")) {
                s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
            } else if (aws_endpoint.contains("sa-east-1")) {
                s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
            } else {
                s3Client.setEndpoint("https://s3." + aws_endpoint + ".amazonaws.com");
            }
        } else {
            s3Client.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build());
            s3Client.setEndpoint(endpoint);
        }
        java.util.Date expiration = new java.util.Date();
        long milliSeconds = expiration.getTime();
        milliSeconds += 1000 * 60 * 1000; // Add 1 hour.
        expiration.setTime(milliSeconds);
        GeneratePresignedUrlRequest generatePresignedUrlRequest
                = new GeneratePresignedUrlRequest(bucket, object);
        generatePresignedUrlRequest.setMethod(HttpMethod.GET);
        generatePresignedUrlRequest.setExpiration(expiration);
        URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
        URL = ("Pre-Signed URL = " + url.toString());
        StringSelection stringSelection = new StringSelection(url.toString());
    } catch (Exception setACLpublic) {
    }
    return URL;
}
 
Example 5
Source File: S3OperationsImpl.java    From herd with Apache License 2.0 4 votes vote down vote up
@Override
public URL generatePresignedUrl(GeneratePresignedUrlRequest generatePresignedUrlRequest, AmazonS3 s3Client)
{
    return s3Client.generatePresignedUrl(generatePresignedUrlRequest);
}
 
Example 6
Source File: S3ClientHelperTest.java    From crate with Apache License 2.0 4 votes vote down vote up
@Test
public void testWithCredentials() throws Exception {
    AmazonS3 s3Client = s3ClientHelper.client(new URI("s3://user:password@host/path"));
    URL url = s3Client.generatePresignedUrl("bucket", "key", new Date(0L));
    assertThat(url.toString(), is("https://bucket.s3.amazonaws.com/key?AWSAccessKeyId=user&Expires=0&Signature=o5V2voSQbVEErsUXId6SssCq9OY%3D"));
}