Example usage for com.amazonaws.services.s3.model GeneratePresignedUrlRequest setExpiration

List of usage examples for com.amazonaws.services.s3.model GeneratePresignedUrlRequest setExpiration

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model GeneratePresignedUrlRequest setExpiration.

Prototype

public void setExpiration(Date expiration) 

Source Link

Document

Sets the expiration date at which point the new pre-signed URL will no longer be accepted by Amazon S3.

Usage

From source file:com.handywedge.binarystore.store.aws.BinaryStoreManagerImpl.java

License:MIT License

private URL getPresignedUrl(AmazonS3 s3client, String buckrtName, String key) {
    logger.info("getPresignedUrl start.");

    java.util.Date expiration = new java.util.Date();
    long milliSeconds = expiration.getTime();
    milliSeconds += Long.parseLong(PropertiesUtil.get("aws.presignedurl.expiration"));
    expiration.setTime(milliSeconds);/*from  w ww .  j  ava  2s .c  om*/

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

    URL PresignedUrl = s3client.generatePresignedUrl(generatePresignedUrlRequest);

    logger.info("getPresignedUrl: end. url={}", PresignedUrl);
    return PresignedUrl;
}

From source file:com.jeet.s3.AmazonS3ClientWrapper.java

License:Open Source License

public String generatePresignedURLForContent(String key) {
    try {//  www .  j a  v a2 s.  c o  m
        java.util.Date expiration = new java.util.Date();
        long msec = expiration.getTime();
        msec += 1000 * 60 * 60; // 1 hour.
        expiration.setTime(msec);
        GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(
                Constants.BUCKET_NAME, key);
        generatePresignedUrlRequest.setMethod(HttpMethod.GET); // Default.
        generatePresignedUrlRequest.setExpiration(expiration);

        URL s = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
        return s.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";

}

From source file:com.kirana.utils.GeneratePresignedUrlAndUploadObject.java

public static void main(String[] args) throws IOException {

    System.setProperty(SDKGlobalConfiguration.ENABLE_S3_SIGV4_SYSTEM_PROPERTY, "true");
    System.setProperty(SDKGlobalConfiguration.ACCESS_KEY_ENV_VAR, "AKIAJ666LALJZHA6THGQ");
    System.setProperty(SDKGlobalConfiguration.SECRET_KEY_ENV_VAR, "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");
    //      AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider("kirana"));

    ProfilesConfigFile profile = new ProfilesConfigFile("AwsCredentials.properties");
    AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
    s3client.setRegion(Region.getRegion(Regions.AP_SOUTHEAST_1));

    try {//from w  ww . ja va 2 s . co m
        System.out.println("Generating pre-signed URL.");
        java.util.Date expiration = new java.util.Date();
        long milliSeconds = expiration.getTime();
        milliSeconds += 1000 * 60 * 60; // Add 1 hour.
        expiration.setTime(milliSeconds);

        GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName,
                objectKey);
        generatePresignedUrlRequest.setMethod(HttpMethod.PUT);
        generatePresignedUrlRequest.setExpiration(expiration);

        //                        s3client.putObject(bucketName, objectKey, null);

        URL url = s3client.generatePresignedUrl(generatePresignedUrlRequest);
        UploadObject(url);

        System.out.println("Pre-Signed URL = " + url.toString());
    } catch (AmazonServiceException exception) {
        System.out.println("Caught an AmazonServiceException, " + "which means your request made it "
                + "to Amazon S3, but was rejected with an error response " + "for some reason.");
        System.out.println("Error Message: " + exception.getMessage());
        System.out.println("HTTP  Code: " + exception.getStatusCode());
        System.out.println("AWS Error Code:" + exception.getErrorCode());
        System.out.println("Error Type:    " + exception.getErrorType());
        System.out.println("Request ID:    " + exception.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, " + "which means the client encountered "
                + "an internal error while trying to communicate" + " with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:hudson.plugins.ec2.EC2Cloud.java

License:Open Source License

/**
 * Computes the presigned URL for the given S3 resource.
 *
 * @param path/*from  ww w . java  2s .c o m*/
 *      String like "/bucketName/folder/folder/abc.txt" that represents the resource to request.
 */
public URL buildPresignedURL(String path) throws AmazonClientException {
    AWSCredentials credentials = awsCredentialsProvider.getCredentials();
    long expires = System.currentTimeMillis() + 60 * 60 * 1000;
    GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(path, credentials.getAWSSecretKey());
    request.setExpiration(new Date(expires));
    AmazonS3 s3 = new AmazonS3Client(credentials);
    return s3.generatePresignedUrl(request);
}

From source file:io.konig.camel.aws.s3.DeleteObjectProducer.java

License:Apache License

private void createDownloadLink(AmazonS3 s3Client, Exchange exchange) {
    String bucketName = exchange.getIn().getHeader(S3Constants.BUCKET_NAME, String.class);
    if (ObjectHelper.isEmpty(bucketName)) {
        bucketName = getConfiguration().getBucketName();
    }/*from w ww. j  a va  2 s  .  com*/

    if (bucketName == null) {
        throw new IllegalArgumentException("AWS S3 Bucket name header is missing.");
    }

    String key = exchange.getIn().getHeader(S3Constants.KEY, String.class);
    if (key == null) {
        throw new IllegalArgumentException("AWS S3 Key header is missing.");
    }

    Date expiration = new Date();
    long milliSeconds = expiration.getTime();

    Long expirationMillis = exchange.getIn().getHeader(S3Constants.DOWNLOAD_LINK_EXPIRATION, Long.class);
    if (expirationMillis != null) {
        milliSeconds += expirationMillis;
    } else {
        milliSeconds += 1000 * 60 * 60; // Default: Add 1 hour.
    }

    expiration.setTime(milliSeconds);

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

    URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);

    Message message = getMessageForResponse(exchange);
    message.setHeader(S3Constants.DOWNLOAD_LINK, url.toString());
}

From source file:io.stallion.services.S3StorageService.java

License:Open Source License

public String getSignedDownloadUrl(String bucket, String fileKey) {
    GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucket, fileKey);
    req.setExpiration(Date.from(utcNow().plusMinutes(60).toInstant()));
    req.setMethod(HttpMethod.GET);/*from w w w.j  a va2 s .  c  o m*/
    return client.generatePresignedUrl(req).toString();
}

From source file:io.stallion.services.S3StorageService.java

License:Open Source License

public String getSignedUploadUrl(String bucket, String fileKey, String contentType, Map headers) {
    GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucket, fileKey);
    if (!empty(contentType)) {
        req.setContentType(contentType);
    }//from  w w  w .  ja va 2 s  .c o  m
    if (headers != null) {
        for (Object key : headers.keySet())
            req.addRequestParameter(key.toString(), headers.get(key).toString());
    }
    req.setExpiration(Date.from(utcNow().plusDays(2).toInstant()));
    req.setMethod(HttpMethod.PUT);
    return client.generatePresignedUrl(req).toString();
}

From source file:org.alanwilliamson.amazon.s3.GetUrl.java

License:Open Source License

@Override
public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {

    AmazonKey amazonKey = getAmazonKey(_session, argStruct);
    AmazonS3 s3Client = getAmazonS3(amazonKey);

    String bucket = getNamedStringParam(argStruct, "bucket", null);
    String key = getNamedStringParam(argStruct, "key", null);

    if (bucket == null)
        throwException(_session, "Please specify a bucket");

    if (key == null)
        throwException(_session, "Please specify a key");

    if (key.charAt(0) == '/')
        key = key.substring(1);/*from  ww  w.  j  a  va 2s .  c o m*/

    cfData expired = getNamedParam(argStruct, "expiration", null);
    try {
        Date expirationDate;
        if (expired != null) {
            if (expired.getDataType() == cfData.CFDATEDATA)
                expirationDate = new Date(expired.getLong());
            else
                expirationDate = new Date(System.currentTimeMillis() + (expired.getLong() * DateUtil.SECS_MS));
        } else {
            expirationDate = new Date(System.currentTimeMillis() + DateUtil.DAY_MS * 7);
        }

        GeneratePresignedUrlRequest g = new GeneratePresignedUrlRequest(bucket, key);
        g.setExpiration(expirationDate);

        return new cfStringData(s3Client.generatePresignedUrl(g).toString());
    } catch (Exception e) {
        throwException(_session, "AmazonS3: " + e.getMessage());
        return cfBooleanData.FALSE;
    }
}

From source file:org.apache.flink.cloudsort.io.aws.AwsCurlInput.java

License:Apache License

@Override
public void open(String objectName) throws IOException {
    Preconditions.checkNotNull(bucket);/*from   w  w  w  .  j  av  a 2  s .  c  o  m*/

    AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider());

    java.util.Date expiration = new java.util.Date();
    long msec = expiration.getTime();
    msec += 1000 * 60 * 60; // Add 1 hour.
    expiration.setTime(msec);

    GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucket,
            objectName);
    generatePresignedUrlRequest.setMethod(HttpMethod.GET);
    generatePresignedUrlRequest.setContentType(CONTENT_TYPE);
    generatePresignedUrlRequest.setExpiration(expiration);

    String url = s3Client.generatePresignedUrl(generatePresignedUrlRequest).toExternalForm();

    downloader = new ProcessBuilder("curl", "-s", "-X", "GET", "-H", "Content-Type: " + CONTENT_TYPE, url)
            .redirectError(Redirect.INHERIT).start();
}

From source file:org.apache.flink.cloudsort.io.aws.AwsCurlOutput.java

License:Apache License

@Override
public Process open(String filename, String taskId) throws IOException {
    Preconditions.checkNotNull(bucket);//from www.j a  va2  s. co m
    Preconditions.checkNotNull(prefix);
    Preconditions.checkNotNull(filename);
    Preconditions.checkNotNull(taskId);

    String objectName = prefix + taskId;

    AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider());

    java.util.Date expiration = new java.util.Date();
    long msec = expiration.getTime();
    msec += 1000 * 60 * 60; // Add 1 hour.
    expiration.setTime(msec);

    GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucket,
            objectName);
    generatePresignedUrlRequest.setMethod(HttpMethod.PUT);
    generatePresignedUrlRequest.setContentType(CONTENT_TYPE);
    generatePresignedUrlRequest.setExpiration(expiration);

    String url = s3Client.generatePresignedUrl(generatePresignedUrlRequest).toExternalForm();

    return new ProcessBuilder("curl", "-s", "--max-time", "60", "--retry", "1024", "-X", "PUT", "-H",
            "Content-Type: " + CONTENT_TYPE, "--data-binary", "@" + filename, url)
                    .redirectError(Redirect.INHERIT).redirectOutput(Redirect.INHERIT).start();
}