Example usage for com.amazonaws HttpMethod GET

List of usage examples for com.amazonaws HttpMethod GET

Introduction

In this page you can find the example usage for com.amazonaws HttpMethod GET.

Prototype

HttpMethod GET

To view the source code for com.amazonaws HttpMethod GET.

Click Source Link

Usage

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);
    generatePresignedUrlRequest.setExpiration(expiration);
    AmazonS3Client s3 = getAmazonS3(s3FileTransferRequestParamsDto);
    try {//  w  w  w . java 2s  .c o  m
        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 getDownloadPartUrl(String bucketName, ObjectKey objectKey, Part part, Date expiration) {
    GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, objectKey.getKey(),
            HttpMethod.GET);
    req.setExpiration(expiration);/*w  w w  .  j a va  2  s .c o  m*/

    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);
    req.setExpiration(expiration);/* ww w  .  j  a  va2  s  .c  o m*/

    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 {/*  w  ww  .j  a  v  a  2 s  .  c o  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);/*  www. ja  va  2 s  .  c o  m*/
    URL url = amazonS3.generatePresignedUrl(request);
    try {
        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;/* w w  w  .j  a va  2s.  co  m*/
    }
    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);
    }

}

From source file:org.nuxeo.sheridan.S3TempSignedURLBuilder.java

License:Open Source License

/**
 * Return an url as string. This url is a temporary signed url giving access to the object for
 * <code>expireInSeconds</expireInSeconds> seconds. After this time, the object cannot be accessed anymore with this URL.
 * <p>//from   w w w . j a  va  2  s  .  c o  m
 * Some default values apply:
 * <p>
 * <ul>
 * <li>If <code>bucket</code> is empty (null, "", " ", ....), the bucket defined in the configuration is used.</li>
 * <li>If <code>expireInSeconds</code> is less than 1, the default
 * <code>S3TempSignedURLBuilder.DEFAULT_EXPIRE</code> is used</li> <li><code>contentType</code> and
 * <code>contentDisposition</code> can be null or "", but it is recommended to set them to make sure the is no
 * ambiguity when the URL is used (a key without a file extension for example)</li> </ul>
 * <p>
 * 
 * @param bucket
 * @param objectKey
 * @param expireInSeconds
 * @param contentType
 * @param contentDisposition
 * @return the temporary signed Url
 * @throws IOException
 * @since 7.10
 */
public String build(String bucket, String objectKey, int expireInSeconds, String contentType,
        String contentDisposition) throws IOException {

    if (StringUtils.isBlank(bucket)) {
        bucket = awsBucket;
    }
    if (StringUtils.isBlank(bucket)) {
        throw new NuxeoException(
                "No bucket provided, and configuration key " + CONF_KEY_NAME_BUCKET + " is missing.");
    }

    Date expiration = new Date();
    if (expireInSeconds < 1) {
        expireInSeconds = DEFAULT_EXPIRE;
    }
    expiration.setTime(expiration.getTime() + (expireInSeconds * 1000));

    GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket, objectKey, HttpMethod.GET);

    // Do we need these?
    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 IOException(e);
    }

}

From source file:org.restcomm.connect.commons.amazonS3.S3AccessTool.java

License:Open Source License

public URI getPublicUrl(String fileName) throws URISyntaxException {
    if (s3client == null) {
        s3client = getS3client();//from  www .  j a va  2 s . c o m
    }
    Date date = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.MINUTE, minutesToRetainPublicUrl);
    if (logger.isInfoEnabled()) {
        final String msg = String.format("Prepared amazon s3 public url valid for %s minutes for recording: %s",
                minutesToRetainPublicUrl, fileName);
        logger.info(msg);
    }
    date = cal.getTime();
    String bucket = bucketName;
    if (folder != null && !folder.isEmpty()) {
        bucket = bucket.concat("/").concat(folder);
    }
    GeneratePresignedUrlRequest generatePresignedUrlRequestGET = new GeneratePresignedUrlRequest(bucket,
            fileName);
    generatePresignedUrlRequestGET.setMethod(HttpMethod.GET);
    generatePresignedUrlRequestGET.setExpiration(date);

    return s3client.generatePresignedUrl(generatePresignedUrlRequestGET).toURI();
}

From source file:org.whispersystems.textsecuregcm.controllers.AttachmentController.java

License:Open Source License

@Timed
@GET// w  w w .  j  a  v  a 2s  .c o  m
@Produces(MediaType.APPLICATION_JSON)
@Path("/{attachmentId}")
public AttachmentUri redirectToAttachment(@Auth Account account, @PathParam("attachmentId") long attachmentId,
        @QueryParam("relay") Optional<String> relay) throws IOException {
    try {
        if (!relay.isPresent()) {
            return new AttachmentUri(urlSigner.getPreSignedUrl(attachmentId, HttpMethod.GET));
        } else {
            return new AttachmentUri(
                    federatedClientManager.getClient(relay.get()).getSignedAttachmentUri(attachmentId));
        }
    } catch (NoSuchPeerException e) {
        logger.info("No such peer: " + relay);
        throw new WebApplicationException(Response.status(404).build());
    }
}

From source file:servlet.FileUploadServlet.java

private void putInBucket(String fileName, String id) throws IOException {

    AWSCredentials awsCreds = new PropertiesCredentials(
            new File("/Users/paulamontojo/Desktop/AwsCredentials.properties"));
    AmazonS3 s3client = new AmazonS3Client(awsCreds);

    try {/*from w  w w  . ja  v  a2s. co m*/
        System.out.println("Uploading a new object to S3 from a file\n");
        File file = new File(uploadFileName);
        s3client.putObject(new PutObjectRequest(bucketName, fileName, file));

        GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName,
                fileName);
        generatePresignedUrlRequest.setMethod(HttpMethod.GET); // Default.

        URL s = s3client.generatePresignedUrl(generatePresignedUrlRequest);
        insertUrl(s.toString(), id);
        System.out.println(s);

    } catch (AmazonServiceException ase) {
        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:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.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());
    }

}