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

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

Introduction

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

Prototype

public void setMethod(HttpMethod method) 

Source Link

Document

Sets the HTTP method (GET, PUT, DELETE, HEAD) to be used in this request.

Usage

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;// w  w  w .  j  av  a  2s .  co  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);
            });//  www . jav a 2  s  . c  o  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  .j a  v  a  2  s .c o 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;/*  w  w w  .j  ava  2s . c o  m*/
    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 {//from   w w w . ja  v a  2 s .  c om
        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);/*  w w  w.ja  va2  s  .  co  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.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  ww  w .  j a  v a  2  s .co  m

    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 {//from   w w w  .jav 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.jeet.s3.AmazonS3ClientWrapper.java

License:Open Source License

public String generatePresignedURL(String key) {
    try {/*from  w  w w.  ja v  a2s.c om*/
        GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(
                Constants.BUCKET_NAME, key);
        generatePresignedUrlRequest.setMethod(HttpMethod.GET); // Default.
        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 {/*w  ww .  j a  v a 2  s .com*/
        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());
    }
}