Java Code Examples for com.amazonaws.services.s3.model.GeneratePresignedUrlRequest#setExpiration()

The following examples show how to use com.amazonaws.services.s3.model.GeneratePresignedUrlRequest#setExpiration() . 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: ContentHelper.java    From aws-photosharing-example with Apache License 2.0 5 votes vote down vote up
public URL getSignedUrl(String p_s3_bucket, String p_s3_file, Date p_exires) {    	   
  	
  	GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(p_s3_bucket, p_s3_file);
generatePresignedUrlRequest.setMethod(HttpMethod.GET); // Default.
generatePresignedUrlRequest.setExpiration(p_exires);
      
return s3Client.generatePresignedUrl(generatePresignedUrlRequest); 
  }
 
Example 2
Source File: S3UrlGenerator.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
@NotNull
URL generateUrl(@NotNull String bucketName, @NotNull String objectKey, int expirationHours) {
    try {
        LOGGER.info("Generating pre-signed URL for bucket: {}\tobject: {}\texpirationTime: {} hours",
                bucketName,
                objectKey,
                expirationHours);
        long millisNow = System.currentTimeMillis();
        long expirationMillis = millisNow + 1000 * 60 * 60 * expirationHours;
        Date expiration = new java.util.Date(expirationMillis);
        GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey);
        generatePresignedUrlRequest.setMethod(HttpMethod.GET);
        generatePresignedUrlRequest.setExpiration(expiration);

        return s3Client().generatePresignedUrl(generatePresignedUrlRequest);
    } catch (AmazonServiceException exception) {
        LOGGER.error("Error Message: {}", exception.getMessage());
        LOGGER.error("HTTP  Code: {}", exception.getStatusCode());
        LOGGER.error("Error Code: {}", exception.getErrorCode());
        LOGGER.error("Error Type:   {}", exception.getErrorType());
        LOGGER.error("Request ID:   {}", exception.getRequestId());
    } catch (AmazonClientException ace) {
        LOGGER.error("The client encountered an internal error while trying to communicate with S3");
        LOGGER.error("Error Message: {}", ace.getMessage());
    }
    throw new IllegalStateException("Failed to create URL based on the S3 input configurations.");
}
 
Example 3
Source File: S3DaoImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
@Override
public String generateGetObjectPresignedUrl(String bucketName, String key, Date expiration, S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto)
{
    GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.GET);
    generatePresignedUrlRequest.setExpiration(expiration);
    AmazonS3Client s3 = getAmazonS3(s3FileTransferRequestParamsDto);
    try
    {
        return s3Operations.generatePresignedUrl(generatePresignedUrlRequest, s3).toString();
    }
    finally
    {
        s3.shutdown();
    }
}
 
Example 4
Source File: S3Storage.java    From digdag with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<DirectDownloadHandle> getDirectDownloadHandle(String key)
{
    final long secondsToExpire = config.get("direct_download_expiration", Long.class, 10L*60);

    GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucket, key);
    req.setExpiration(Date.from(Instant.now().plusSeconds(secondsToExpire)));

    String url = client.generatePresignedUrl(req).toString();

    return Optional.of(DirectDownloadHandle.of(url));
}
 
Example 5
Source File: S3Storage.java    From digdag with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<DirectUploadHandle> getDirectUploadHandle(String key)
{
    final long secondsToExpire = config.get("direct_upload_expiration", Long.class, 10L*60);

    GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucket, key);
    req.setMethod(HttpMethod.PUT);
    req.setExpiration(Date.from(Instant.now().plusSeconds(secondsToExpire)));

    String url = client.generatePresignedUrl(req).toString();

    return Optional.of(DirectUploadHandle.of(url));
}
 
Example 6
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 7
Source File: AmazonS3Manager.java    From carina with Apache License 2.0 5 votes vote down vote up
/**
 * Method to generate pre-signed object URL to s3 object
 * 
 * @param bucketName AWS S3 bucket name
 * @param key (example: android/apkFolder/ApkName.apk)
 * @param ms espiration time in ms, i.e. 1 hour is 1000*60*60
 * @return url String pre-signed URL
 */
public URL generatePreSignUrl(final String bucketName, final String key, long ms) {

    java.util.Date expiration = new java.util.Date();
    long msec = expiration.getTime();
    msec += ms;
    expiration.setTime(msec);

    GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key);
    generatePresignedUrlRequest.setMethod(HttpMethod.GET);
    generatePresignedUrlRequest.setExpiration(expiration);

    URL url = s3client.generatePresignedUrl(generatePresignedUrlRequest);
    return url;
}
 
Example 8
Source File: AwsSdkTest.java    From s3proxy with Apache License 2.0 4 votes vote down vote up
@Test
public void testAwsV2UrlSigningWithOverrideParameters() throws Exception {
    client = AmazonS3ClientBuilder.standard()
            .withClientConfiguration(V2_SIGNER_CONFIG)
            .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
            .withEndpointConfiguration(s3EndpointConfig).build();

    String blobName = "foo";
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(BYTE_SOURCE.size());
    client.putObject(containerName, blobName, BYTE_SOURCE.openStream(),
            metadata);

    GeneratePresignedUrlRequest generatePresignedUrlRequest =
            new GeneratePresignedUrlRequest(containerName, blobName);
    generatePresignedUrlRequest.setMethod(HttpMethod.GET);

    ResponseHeaderOverrides headerOverride = new ResponseHeaderOverrides();

    headerOverride.setContentDisposition("attachment; " + blobName);
    headerOverride.setContentType("text/plain");
    generatePresignedUrlRequest.setResponseHeaders(headerOverride);

    Date expiration = new Date(System.currentTimeMillis() +
            TimeUnit.HOURS.toMillis(1));
    generatePresignedUrlRequest.setExpiration(expiration);

    URL url = client.generatePresignedUrl(generatePresignedUrlRequest);
    URLConnection connection =  url.openConnection();
    try (InputStream actual = connection.getInputStream();
         InputStream expected = BYTE_SOURCE.openStream()) {

        String value = connection.getHeaderField("Content-Disposition");
        assertThat(value).isEqualTo(headerOverride.getContentDisposition());

        value = connection.getHeaderField("Content-Type");
        assertThat(value).isEqualTo(headerOverride.getContentType());

        assertThat(actual).hasContentEqualTo(expected);
    }
}