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:org.apache.usergrid.apm.service.AWSUtil.java

License:Apache License

public static String generatePresignedURLForCrashLog(String fullAppName, String fileName) {
    DeploymentConfig config = DeploymentConfig.geDeploymentConfig();
    AWSCredentials credentials = new BasicAWSCredentials(config.getAccessKey(), config.getSecretKey());
    AmazonS3Client client = new AmazonS3Client(credentials);
    String s3FullFileName = AWSUtil.formS3CrashFileUrl(fullAppName, fileName);
    GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(config.getS3LogBucket(),
            s3FullFileName, HttpMethod.GET);
    request.setExpiration(new Date(System.currentTimeMillis() + (120 * 60 * 1000))); //expires in 2 hour
    return client.generatePresignedUrl(request).toString();
}

From source file:org.apache.usergrid.apm.util.AwsS3Util.java

License:Apache License

public static String generatePresignedURL(String appId, String fileName) {
    DeploymentConfig config = DeploymentConfig.geDeploymentConfig();
    AWSCredentials credentials = new BasicAWSCredentials(config.getAccessKey(), config.getSecretKey());
    AmazonS3Client client = new AmazonS3Client(credentials);
    String env = config.getEnvironment();
    String s3FullFileName = env + "/crashlog/" + appId + "/" + fileName;
    GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(config.getS3LogBucket(),
            s3FullFileName, HttpMethod.GET);

    request.setExpiration(new Date(System.currentTimeMillis() + (120 * 60 * 1000))); //expires in 2 hour
    return client.generatePresignedUrl(request).toString();

}

From source file:org.cloudifysource.esc.driver.provisioning.privateEc2.AmazonS3Uploader.java

License:Open Source License

/**
 * Returns a pre-signed URL for accessing an Amazon S3 resource.
 * /*from   w w  w  .  j  av a  2s .  c  o m*/
 * @param bucketName
 *            The bucket where the resource lies.
 * @param objectKey
 *            The key object.
 * @return A pre-signed URL for accessing an Amazon S3 resource.
 */
public String generatePresignedURL(final String bucketName, final String objectKey) {
    final Date expiration = new Date();
    expiration.setTime(System.currentTimeMillis() + ONE_DAY_IN_MILLIS);

    final GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName,
            objectKey);
    generatePresignedUrlRequest.setMethod(HttpMethod.GET); // Default.
    generatePresignedUrlRequest.setExpiration(expiration);

    URL generatePresignedObjectURL = s3client.generatePresignedUrl(generatePresignedUrlRequest);

    if (logger.isLoggable(Level.FINEST)) {
        logger.finest("Zip uploaded. Limited signed URL: " + generatePresignedObjectURL);
    }
    return generatePresignedObjectURL.toString();
}

From source file:org.finra.herd.dao.impl.S3DaoImpl.java

License:Apache License

@Override
public String generateGetObjectPresignedUrl(String bucketName, String key, Date expiration,
        S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto) {
    GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key,
            HttpMethod.GET);//ww  w  .  j  av a  2  s  . c  o m
    generatePresignedUrlRequest.setExpiration(expiration);
    AmazonS3Client s3 = getAmazonS3(s3FileTransferRequestParamsDto);
    try {
        return s3Operations.generatePresignedUrl(generatePresignedUrlRequest, s3).toString();
    } finally {
        s3.shutdown();
    }
}

From source file:org.icgc.dcc.storage.server.repository.s3.S3URLGenerator.java

License:Open Source License

@Override
public String getUploadPartUrl(String bucketName, ObjectKey objectKey, String uploadId, Part part,
        Date expiration) {//from www .  j av  a2s .  c om
    GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, objectKey.getKey(),
            HttpMethod.PUT);
    req.setExpiration(expiration);

    req.addRequestParameter("partNumber", String.valueOf(part.getPartNumber()));
    req.addRequestParameter("uploadId", uploadId);

    return s3Client.generatePresignedUrl(req).toString();
}

From source file:org.icgc.dcc.storage.server.repository.s3.S3URLGenerator.java

License:Open Source License

@Override
public String getDownloadPartUrl(String bucketName, ObjectKey objectKey, Part part, Date expiration) {
    GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, objectKey.getKey(),
            HttpMethod.GET);/*from w  ww .j  av a2s . c o  m*/
    req.setExpiration(expiration);

    req.putCustomRequestHeader(HttpHeaders.RANGE, Parts.getHttpRangeValue(part));
    return s3Client.generatePresignedUrl(req).toString();
}

From source file:org.icgc.dcc.storage.server.repository.s3.S3URLGenerator.java

License:Open Source License

@Override
public String getDownloadUrl(String bucketName, ObjectKey objectKey, Date expiration) {
    GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, objectKey.getKey(),
            HttpMethod.GET);//  w  ww  .ja  v  a 2s .c  o  m
    req.setExpiration(expiration);

    return s3Client.generatePresignedUrl(req).toString();
}

From source file:org.mobicents.servlet.restcomm.amazonS3.S3AccessTool.java

License:Open Source License

public URI uploadFile(final String fileToUpload) {
    AWSCredentials credentials = new BasicAWSCredentials(accessKey, securityKey);
    AmazonS3 s3client = new AmazonS3Client(credentials);
    try {//from w  ww.  j a  v  a 2s . co m
        StringBuffer bucket = new StringBuffer();
        bucket.append(bucketName);
        if (folder != null && !folder.isEmpty())
            bucket.append("/").append(folder);
        URI fileUri = URI.create(fileToUpload);
        logger.info("File to upload to S3: " + fileUri.toString());
        File file = new File(fileUri);
        //            while (!file.exists()){}
        //            logger.info("File exist: "+file.exists());
        //First generate the Presigned URL, buy some time for the file to be written on the disk
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if (daysToRetainPublicUrl > 0) {
            cal.add(Calendar.DATE, daysToRetainPublicUrl);
        } else {
            //By default the Public URL will be valid for 180 days
            cal.add(Calendar.DATE, 180);
        }
        date = cal.getTime();
        GeneratePresignedUrlRequest generatePresignedUrlRequestGET = new GeneratePresignedUrlRequest(
                bucket.toString(), file.getName());
        generatePresignedUrlRequestGET.setMethod(HttpMethod.GET);
        generatePresignedUrlRequestGET.setExpiration(date);

        URL downloadUrl = s3client.generatePresignedUrl(generatePresignedUrlRequestGET);

        //Second upload the file to S3
        //            while (!file.exists()){}
        while (!FileUtils.waitFor(file, 30)) {
        }
        if (file.exists()) {
            PutObjectRequest putRequest = new PutObjectRequest(bucket.toString(), file.getName(), file);
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentType(new MimetypesFileTypeMap().getContentType(file));
            putRequest.setMetadata(metadata);
            if (reducedRedundancy)
                putRequest.setStorageClass(StorageClass.ReducedRedundancy);
            s3client.putObject(putRequest);

            if (removeOriginalFile) {
                removeLocalFile(file);
            }
            return downloadUrl.toURI();
        } else {
            logger.error("Timeout waiting for the recording file: " + file.getAbsolutePath());
            return null;
        }
    } catch (AmazonServiceException ase) {
        logger.error("Caught an AmazonServiceException");
        logger.error("Error Message:    " + ase.getMessage());
        logger.error("HTTP Status Code: " + ase.getStatusCode());
        logger.error("AWS Error Code:   " + ase.getErrorCode());
        logger.error("Error Type:       " + ase.getErrorType());
        logger.error("Request ID:       " + ase.getRequestId());
        return null;
    } catch (AmazonClientException ace) {
        logger.error("Caught an AmazonClientException, which ");
        logger.error("Error Message: " + ace.getMessage());
        return null;
    } catch (URISyntaxException e) {
        logger.error("URISyntaxException: " + e.getMessage());
        return null;
    }
}

From source file:org.nuxeo.ecm.core.storage.sql.S3BinaryManager.java

License:Apache License

@Override
protected URI getRemoteUri(String digest, ManagedBlob blob, HttpServletRequest servletRequest)
        throws IOException {
    String key = bucketNamePrefix + digest;
    Date expiration = new Date();
    expiration.setTime(expiration.getTime() + directDownloadExpire * 1000);
    GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.GET);
    request.addRequestParameter("response-content-type", getContentTypeHeader(blob));
    request.addRequestParameter("response-content-disposition", getContentDispositionHeader(blob, null));
    request.setExpiration(expiration);
    URL url = amazonS3.generatePresignedUrl(request);
    try {/* w w  w .j  a  v a 2s  .  co  m*/
        return url.toURI();
    } catch (URISyntaxException e) {
        throw new IOException(e);
    }
}

From source file:org.nuxeo.s3utils.S3HandlerImpl.java

License:Apache License

@Override
public String buildPresignedUrl(String inBucket, String inKey, int durationInSeconds, String contentType,
        String contentDisposition) throws NuxeoException {

    if (StringUtils.isBlank(inBucket)) {
        inBucket = currentBucket;/*from   w  w w. j a  va 2 s . c om*/
    }
    if (StringUtils.isBlank(inBucket)) {
        throw new NuxeoException("No bucket provided");
    }

    if (durationInSeconds <= 0) {
        durationInSeconds = signedUrlDuration;
    }
    if (durationInSeconds <= 0) {
        throw new IllegalArgumentException("duration of " + durationInSeconds + " is invalid.");
    }

    Date expiration = new Date();
    expiration.setTime(expiration.getTime() + (durationInSeconds * 1000));

    GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(currentBucket, inKey, HttpMethod.GET);

    if (StringUtils.isNotBlank(contentType)) {
        request.addRequestParameter("response-content-type", contentType);
    }
    if (StringUtils.isNotBlank(contentDisposition)) {
        request.addRequestParameter("response-content-disposition", contentDisposition);
    }

    request.setExpiration(expiration);
    URL url = s3.generatePresignedUrl(request);

    try {
        URI uri = url.toURI();
        return uri.toString();
    } catch (URISyntaxException e) {
        throw new NuxeoException(e);
    }

}