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:awslabs.lab21.SolutionCode.java

License:Open Source License

@Override
public String generatePreSignedUrl(AmazonS3 s3Client, String bucketName, String key) {
    Date nowPlusOneHour = new Date(System.currentTimeMillis() + 3600000L);

    // Construct a GeneratePresignedUrlRequest object for the provided object.
    GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key);
    // Set the expiration value in the request to the nowPlusOneHour object 
    // (this specifies a time one hour from now). 
    generatePresignedUrlRequest.setExpiration(nowPlusOneHour);

    // Submit the request using the generatePresignedUrl method of the s3Client object.
    URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
    // Return the URL as a string.
    return url.toString();
}

From source file:awslabs.lab51.SolutionCode.java

License:Open Source License

@Override
public String getUrlForItem(AmazonS3Client s3Client, String key, String bucket) {
    Date nowPlusTwoMinutes = new Date(System.currentTimeMillis() + 120000L);

    // Construct a GeneratePresignedUrlRequest object for the provided object.
    GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucket, key);
    // Set the expiration value in the request to the nowPlusOneHour object
    // (this specifies a time one hour from now).
    generatePresignedUrlRequest.setExpiration(nowPlusTwoMinutes);

    // Submit the request using the generatePresignedUrl method of the s3Client object.
    URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
    // Return the URL as a string.
    return url.toString();
}

From source file:cloudExplorer.Acl.java

License:Open Source License

String setACLurl(String object, String access_key, String secret_key, String endpoint, String bucket) {
    String URL = null;//from  ww  w  . ja v  a  2s  .c  o  m
    try {
        AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key);
        AmazonS3 s3Client = new AmazonS3Client(credentials,
                new ClientConfiguration().withSignerOverride("S3SignerType"));
        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());
        Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
        clpbrd.setContents(stringSelection, null);
    } catch (Exception setACLpublic) {
        mainFrame.jTextArea1.append("\nException occured in ACL");
    }
    return URL;
}

From source file:com.adobe.people.jedelson.rugsinlambda.GenerateHandler.java

License:Apache License

@Override
protected GenerationResultDTO handleRequest(GenerationRequestDTO input, Context context, Rugs rugs) {
    String generatorName = (String) input.getGeneratorName();
    log.info("Using {} as generator name from {}.", generatorName, input);

    Optional<ProjectGenerator> opt = asJavaCollection(rugs.generators()).stream()
            .filter(g -> g.name().equals(input.getGeneratorName())).findFirst();
    if (opt.isPresent()) {
        ProjectGenerator generator = opt.get();
        ParameterValues paramValues = input.toParameterValues();
        if (!generator.areValid(paramValues)) {
            GenerationResultDTO result = new GenerationResultDTO(false);
            asJavaCollection(generator.findInvalidParameterValues(paramValues)).forEach(p -> {
                result.addInvalidParameter(p);
            });/*from ww w .j a v  a2 s .co  m*/
            asJavaCollection(generator.findMissingParameters(paramValues)).forEach(p -> {
                result.addMissingParameter(p);
            });
            return result;
        } else {
            String projectName = input.getParams().get("project_name");
            TempProjectManagement tpm = new TempProjectManagement(context.getAwsRequestId());
            tpm.generate(generator, paramValues, projectName);

            GenerationResultDTO result = new GenerationResultDTO(true);

            for (EditRequestDTO edit : input.getEditors()) {
                String editorName = edit.getName();
                log.info("Editing with {} using params {}.", editorName, edit.getParams());
                Optional<ProjectEditor> editorOpt = asJavaCollection(rugs.editors()).stream()
                        .filter(g -> g.name().equals(editorName)).findFirst();
                if (editorOpt.isPresent()) {
                    ProjectEditor editor = editorOpt.get();
                    ParameterValues editorParams = edit.toParameterValues(input.getParams());
                    if (!editor.areValid(editorParams)) {
                        asJavaCollection(generator.findInvalidParameterValues(paramValues)).forEach(p -> {
                            result.addInvalidParameter(editorName, p);
                        });
                        asJavaCollection(generator.findMissingParameters(paramValues)).forEach(p -> {
                            result.addMissingParameter(editorName, p);
                        });
                    } else {
                        tpm.edit(editor, editorParams, projectName);
                    }
                }
            }

            File zipFile = tpm.createZipFile();
            log.info("zip file is at {} length is {}.", zipFile.getAbsolutePath(), zipFile.length());

            AmazonS3Client s3Client = new AmazonS3Client();
            String keyName = context.getAwsRequestId() + "/project.zip";
            s3Client.putObject(BUCKET_NAME, keyName, zipFile);

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

            GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(
                    BUCKET_NAME, keyName);
            generatePresignedUrlRequest.setMethod(HttpMethod.GET);
            generatePresignedUrlRequest.setExpiration(expiration);

            URL presignedUrl = s3Client.generatePresignedUrl(generatePresignedUrlRequest);

            result.setUrl(presignedUrl.toString());
            return result;
        }
    } else {
        throw new NoSuchGeneratorException(input.getGeneratorName());
    }
}

From source file:com.amazon.photosharing.utils.ContentHelper.java

License:Open Source License

public URL getSignedUrl(String p_s3_bucket, String p_s3_file, Date p_exires) {

    GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(p_s3_bucket,
            p_s3_file);/*from  w w  w  . ja v  a2 s .co  m*/
    generatePresignedUrlRequest.setMethod(HttpMethod.GET); // Default.
    generatePresignedUrlRequest.setExpiration(p_exires);

    return s3Client.generatePresignedUrl(generatePresignedUrlRequest);
}

From source file:com.amediamanager.service.VideoServiceImpl.java

License:Apache License

@Override
public Video generateExpiringUrl(Video video, long expirationInMillis) {

    Date expiration = new java.util.Date();
    long msec = expiration.getTime();
    msec += expirationInMillis;//from  w  w  w.ja v  a2  s. c  om
    expiration.setTime(msec);

    // Expiring URL for original video
    GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(video.getBucket(),
            video.getOriginalKey());
    generatePresignedUrlRequest.setMethod(HttpMethod.GET);
    generatePresignedUrlRequest.setExpiration(expiration);
    video.setExpiringUrl(s3Client.generatePresignedUrl(generatePresignedUrlRequest));

    // Expiring URL for preview video
    if (video.getPreviewKey() != null) {
        generatePresignedUrlRequest = new GeneratePresignedUrlRequest(video.getBucket(), video.getPreviewKey());
        generatePresignedUrlRequest.setMethod(HttpMethod.GET);
        generatePresignedUrlRequest.setExpiration(expiration);
        video.setExpiringPreviewKey(s3Client.generatePresignedUrl(generatePresignedUrlRequest));
    }

    // Expiring URL for original video
    if (video.getThumbnailKey() != null) {
        generatePresignedUrlRequest = new GeneratePresignedUrlRequest(video.getBucket(),
                video.getThumbnailKey());
        generatePresignedUrlRequest.setMethod(HttpMethod.GET);
        generatePresignedUrlRequest.setExpiration(expiration);
        video.setExpiringThumbnailKey(s3Client.generatePresignedUrl(generatePresignedUrlRequest));
    }

    return video;
}

From source file:com.climate.oada.dao.impl.S3ResourceDAO.java

License:Open Source License

@Override
public List<FileResource> getFileUrls(Long userId, String type) {
    List<FileResource> retval = new ArrayList<FileResource>();
    long validfor = new Long(validHours).longValue() * HOURS_TO_MILLISECONDS;
    try {/* w ww. j a  v  a  2 s . c  o m*/
        AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
        String prefix = userId.toString() + S3_SEPARATOR + type;

        LOG.debug("Listing objects from bucket " + bucketName + " with prefix " + prefix);

        ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName)
                .withPrefix(prefix);
        ObjectListing objectListing;
        do {
            objectListing = s3client.listObjects(listObjectsRequest);
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                LOG.debug(" - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");

                Date expiration = new Date();
                long milliSeconds = expiration.getTime();
                milliSeconds += validfor;
                expiration.setTime(milliSeconds);

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

                FileResource res = new FileResource();
                res.setFileURL(s3client.generatePresignedUrl(generatePresignedUrlRequest));
                retval.add(res);
            }
            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());
    } catch (AmazonServiceException ase) {
        logAWSServiceException(ase);
    } catch (AmazonClientException ace) {
        logAWSClientException(ace);
    } catch (Exception e) {
        LOG.error("Unable to retrieve S3 file URLs " + e.getMessage());
    }
    return retval;
}

From source file:com.emc.vipr.s3.sample._07_PresignedURL.java

License:Open Source License

public static void main(String[] args) throws Exception {
    // create the ViPR S3 Client
    ViPRS3Client s3 = ViPRS3Factory.getS3Client();

    // retrieve the key value from user
    System.out.println("Enter the object key:");
    String key = new BufferedReader(new InputStreamReader(System.in)).readLine();

    // retrieve the expiration time for the object from user
    System.out.print("How many hours should this URL be valid? ");
    String hours = new BufferedReader(new InputStreamReader(System.in)).readLine();

    // convert hours to a date
    Date expiration = new Date();
    long curTime_msec = expiration.getTime();
    long nHours = Long.valueOf(hours);
    curTime_msec += 60 * 60 * 1000 * nHours;
    expiration.setTime(curTime_msec);//from w ww.  j  av  a 2  s . c  o m

    // generate the object's pre-signed URL
    GeneratePresignedUrlRequest presignedUrl = new GeneratePresignedUrlRequest(ViPRS3Factory.S3_BUCKET, key);
    presignedUrl.setMethod(HttpMethod.GET);
    presignedUrl.setExpiration(expiration);

    URL url = s3.generatePresignedUrl(presignedUrl);

    // print object's pre-signed URL
    System.out.println(
            String.format("object [%s/%s] pre-signed URL: [%s]", ViPRS3Factory.S3_BUCKET, key, url.toString()));
}

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  av 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:com.github.abhinavmishra14.aws.s3.service.impl.AwsS3IamServiceImpl.java

License:Open Source License

@Override
public URL generateObjectURL(final String bucketName, final String fileName, final Date expires)
        throws AmazonClientException, AmazonServiceException {
    LOGGER.info("generateObjectURL invoked, bucketName: {}, fileName: {} and expires: {}", bucketName, fileName,
            expires);/*from   ww  w  .  j  av  a  2 s  . co  m*/
    final GeneratePresignedUrlRequest presignedUrlReq = new GeneratePresignedUrlRequest(bucketName, fileName);
    presignedUrlReq.setExpiration(expires);
    return s3client.generatePresignedUrl(presignedUrlReq);
}