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

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

Introduction

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

Prototype

public GeneratePresignedUrlRequest(String bucketName, String key, HttpMethod method) 

Source Link

Document

<p> Creates a new request for generating a pre-signed URL that can be used as part of an HTTP request to access the specified Amazon S3 resource.

Usage

From source file:com.eucalyptus.imaging.manifest.DownloadManifestFactory.java

License:Open Source License

/**
 * Generates download manifest based on bundle manifest and puts in into system owned bucket
 * @param baseManifestLocation location of the base manifest file
 * @param keyToUse public key that used for encryption
 * @param manifestName name for generated manifest file
 * @param expirationHours expiration policy in hours for pre-signed URLs
 * @param manifestType what kind of manifest 
 * @return pre-signed URL that can be used to download generated manifest
 * @throws DownloadManifestException/*from   w w  w  .  j a  v a2 s.c o m*/
 */
public static String generateDownloadManifest(final ImageManifestFile baseManifest, final PublicKey keyToUse,
        final String manifestName, int expirationHours) throws DownloadManifestException {
    try {
        //prepare to do pre-signed urls
        EucaS3Client s3Client = EucaS3ClientFactory.getEucaS3Client(getDownloadManifestS3User());

        Date expiration = new Date();
        long msec = expiration.getTime() + 1000 * 60 * 60 * expirationHours;
        expiration.setTime(msec);

        // check if download-manifest already exists
        if (objectExist(s3Client, DOWNLOAD_MANIFEST_BUCKET_NAME, DOWNLOAD_MANIFEST_PREFIX + manifestName)) {
            LOG.debug("Manifest '" + (DOWNLOAD_MANIFEST_PREFIX + manifestName)
                    + "' is alredy created and has not expired. Skipping creation");
            URL s = s3Client.generatePresignedUrl(DOWNLOAD_MANIFEST_BUCKET_NAME,
                    DOWNLOAD_MANIFEST_PREFIX + manifestName, expiration, HttpMethod.GET);
            return String.format("%s://imaging@%s%s?%s", s.getProtocol(), s.getAuthority(), s.getPath(),
                    s.getQuery());
        }

        UrlValidator urlValidator = new UrlValidator();

        final String manifest = baseManifest.getManifest();
        if (manifest == null) {
            throw new DownloadManifestException("Can't generate download manifest from null base manifest");
        }
        final Document inputSource;
        final XPath xpath;
        Function<String, String> xpathHelper;
        DocumentBuilder builder = XMLParser.getDocBuilder();
        inputSource = builder.parse(new ByteArrayInputStream(manifest.getBytes()));
        if (!"manifest".equals(inputSource.getDocumentElement().getNodeName())) {
            LOG.error("Expected image manifest. Got " + nodeToString(inputSource, false));
            throw new InvalidBaseManifestException("Base manifest does not have manifest element");
        }

        StringBuilder signatureSrc = new StringBuilder();
        Document manifestDoc = builder.newDocument();
        Element root = (Element) manifestDoc.createElement("manifest");
        manifestDoc.appendChild(root);
        Element el = manifestDoc.createElement("version");
        el.appendChild(manifestDoc.createTextNode("2014-01-14"));
        signatureSrc.append(nodeToString(el, false));
        root.appendChild(el);
        el = manifestDoc.createElement("file-format");
        el.appendChild(manifestDoc.createTextNode(baseManifest.getManifestType().getFileType().toString()));
        root.appendChild(el);
        signatureSrc.append(nodeToString(el, false));

        xpath = XPathFactory.newInstance().newXPath();
        xpathHelper = new Function<String, String>() {
            @Override
            public String apply(String input) {
                try {
                    return (String) xpath.evaluate(input, inputSource, XPathConstants.STRING);
                } catch (XPathExpressionException ex) {
                    return null;
                }
            }
        };

        // extract keys
        //TODO: move this?
        if (baseManifest.getManifestType().getFileType() == FileType.BUNDLE) {
            String encryptedKey = xpathHelper.apply("/manifest/image/ec2_encrypted_key");
            String encryptedIV = xpathHelper.apply("/manifest/image/ec2_encrypted_iv");
            String size = xpathHelper.apply("/manifest/image/size");
            EncryptedKey encryptKey = reEncryptKey(new EncryptedKey(encryptedKey, encryptedIV), keyToUse);
            el = manifestDoc.createElement("bundle");
            Element key = manifestDoc.createElement("encrypted-key");
            key.appendChild(manifestDoc.createTextNode(encryptKey.getKey()));
            Element iv = manifestDoc.createElement("encrypted-iv");
            iv.appendChild(manifestDoc.createTextNode(encryptKey.getIV()));
            el.appendChild(key);
            el.appendChild(iv);
            Element sizeEl = manifestDoc.createElement("unbundled-size");
            sizeEl.appendChild(manifestDoc.createTextNode(size));
            el.appendChild(sizeEl);
            root.appendChild(el);
            signatureSrc.append(nodeToString(el, false));
        }

        el = manifestDoc.createElement("image");
        String bundleSize = xpathHelper.apply(baseManifest.getManifestType().getSizePath());
        if (bundleSize == null) {
            throw new InvalidBaseManifestException("Base manifest does not have size element");
        }
        Element size = manifestDoc.createElement("size");
        size.appendChild(manifestDoc.createTextNode(bundleSize));
        el.appendChild(size);

        Element partsEl = manifestDoc.createElement("parts");
        el.appendChild(partsEl);
        //parts
        NodeList parts = (NodeList) xpath.evaluate(baseManifest.getManifestType().getPartsPath(), inputSource,
                XPathConstants.NODESET);
        if (parts == null) {
            throw new InvalidBaseManifestException("Base manifest does not have parts");
        }

        for (int i = 0; i < parts.getLength(); i++) {
            Node part = parts.item(i);
            String partIndex = part.getAttributes().getNamedItem("index").getNodeValue();
            String partKey = ((Node) xpath.evaluate(baseManifest.getManifestType().getPartUrlElement(), part,
                    XPathConstants.NODE)).getTextContent();
            String partDownloadUrl = partKey;
            if (baseManifest.getManifestType().signPartUrl()) {
                GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(
                        baseManifest.getBaseBucket(), partKey, HttpMethod.GET);
                generatePresignedUrlRequest.setExpiration(expiration);
                URL s = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
                partDownloadUrl = s.toString();
            } else {
                // validate url per EUCA-9144
                if (!urlValidator.isEucalyptusUrl(partDownloadUrl))
                    throw new DownloadManifestException(
                            "Some parts in the manifest are not stored in the OS. Its location is outside Eucalyptus:"
                                    + partDownloadUrl);
            }
            Element aPart = manifestDoc.createElement("part");
            Element getUrl = manifestDoc.createElement("get-url");
            getUrl.appendChild(manifestDoc.createTextNode(partDownloadUrl));
            aPart.setAttribute("index", partIndex);
            aPart.appendChild(getUrl);
            partsEl.appendChild(aPart);
        }
        root.appendChild(el);
        signatureSrc.append(nodeToString(el, false));
        String signatureData = signatureSrc.toString();
        Element signature = manifestDoc.createElement("signature");
        signature.setAttribute("algorithm", "RSA-SHA256");
        signature.appendChild(manifestDoc
                .createTextNode(Signatures.SHA256withRSA.trySign(Eucalyptus.class, signatureData.getBytes())));
        root.appendChild(signature);
        String downloadManifest = nodeToString(manifestDoc, true);
        //TODO: move this ?
        createManifestsBucket(s3Client);
        putManifestData(s3Client, DOWNLOAD_MANIFEST_BUCKET_NAME, DOWNLOAD_MANIFEST_PREFIX + manifestName,
                downloadManifest, expiration);
        // generate pre-sign url for download manifest
        URL s = s3Client.generatePresignedUrl(DOWNLOAD_MANIFEST_BUCKET_NAME,
                DOWNLOAD_MANIFEST_PREFIX + manifestName, expiration, HttpMethod.GET);
        return String.format("%s://imaging@%s%s?%s", s.getProtocol(), s.getAuthority(), s.getPath(),
                s.getQuery());
    } catch (Exception ex) {
        LOG.error("Got an error", ex);
        throw new DownloadManifestException("Can't generate download manifest");
    }
}

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.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);//from  w w  w.  j a v 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 ww  w.j  a va 2  s.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);/*w w w  . ja  va2s . c om*/
    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  w w .  ja  v  a 2s . c  om
    req.setExpiration(expiration);

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

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);//from w w  w.  ja v  a 2s  .  com
    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;//from   w w  w . jav a2 s . 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>/*www.j a v a  2s.  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);
    }

}