Search in sources :

Example 6 with S3Presigner

use of software.amazon.awssdk.services.s3.presigner.S3Presigner in project aws-doc-sdk-examples by awsdocs.

the class GeneratePresignedUrlAndUploadObject method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    <bucketName> <keyName> \n\n" + "Where:\n" + "    bucketName - the name of the Amazon S3 bucket. \n\n" + "    keyName - a key name that represents a text file. \n";
    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucketName = args[0];
    String keyName = args[1];
    Region region = Region.US_EAST_1;
    S3Presigner presigner = S3Presigner.builder().region(region).build();
    signBucket(presigner, bucketName, keyName);
    presigner.close();
}
Also used : S3Presigner(software.amazon.awssdk.services.s3.presigner.S3Presigner) Region(software.amazon.awssdk.regions.Region)

Example 7 with S3Presigner

use of software.amazon.awssdk.services.s3.presigner.S3Presigner in project aws-doc-sdk-examples by awsdocs.

the class GeneratePresignedUrlUploadImage method signBucket.

// snippet-start:[presigned.java2.generatepresignedurlimage.main]
public static void signBucket(S3Presigner presigner, String bucketName, String keyName, byte[] pic) {
    try {
        PutObjectRequest objectRequest = PutObjectRequest.builder().bucket(bucketName).key(keyName).contentType("image/png").build();
        PutObjectPresignRequest presignRequest = PutObjectPresignRequest.builder().signatureDuration(Duration.ofMinutes(10)).putObjectRequest(objectRequest).build();
        PresignedPutObjectRequest presignedRequest = presigner.presignPutObject(presignRequest);
        String myURL = presignedRequest.url().toString();
        System.out.println("Presigned URL to upload a file to: " + myURL);
        System.out.println("Which HTTP method needs to be used when uploading a file: " + presignedRequest.httpRequest().method());
        // Upload content to the Amazon S3 bucket by using this URL
        URL url = presignedRequest.url();
        // Create the connection and use it to upload the new object by using the presigned URL
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "image/png");
        connection.setRequestMethod("PUT");
        connection.getOutputStream().write(pic);
        connection.getResponseCode();
        System.out.println("HTTP response code is " + connection.getResponseCode());
    } catch (S3Exception e) {
        e.getStackTrace();
    } catch (IOException e) {
        e.getStackTrace();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) PresignedPutObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) PutObjectPresignRequest(software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest) IOException(java.io.IOException) PresignedPutObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) URL(java.net.URL)

Example 8 with S3Presigner

use of software.amazon.awssdk.services.s3.presigner.S3Presigner in project aws-doc-sdk-examples by awsdocs.

the class GeneratePresignedUrlUploadImage method main.

public static void main(String[] args) throws IOException {
    final String USAGE = "\n" + "Usage:\n" + "    <bucketName> <keyName> <imageLocation> \n\n" + "Where:\n" + "    bucketName - the name of the Amazon S3 bucket. \n\n" + "    keyName - a key name that represents a text file. \n" + "    imageLocation - the location of a PNG file (C:/AWS/Bo.png). \n";
    if (args.length != 3) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucketName = args[0];
    String keyName = args[1];
    String imageLocation = args[2];
    byte[] pic = Files.readAllBytes(Paths.get(imageLocation));
    Region region = Region.US_EAST_1;
    S3Presigner presigner = S3Presigner.builder().region(region).build();
    signBucket(presigner, bucketName, keyName, pic);
    presigner.close();
}
Also used : S3Presigner(software.amazon.awssdk.services.s3.presigner.S3Presigner) Region(software.amazon.awssdk.regions.Region)

Aggregations

IOException (java.io.IOException)4 HttpURLConnection (java.net.HttpURLConnection)4 Region (software.amazon.awssdk.regions.Region)4 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)4 S3Presigner (software.amazon.awssdk.services.s3.presigner.S3Presigner)4 URL (java.net.URL)3 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)3 PresignedPutObjectRequest (software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest)3 PutObjectPresignRequest (software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest)3 OutputStreamWriter (java.io.OutputStreamWriter)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)1 GetObjectPresignRequest (software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest)1 PresignedGetObjectRequest (software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest)1