Search in sources :

Example 16 with S3Exception

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

the class DisplayFacesFrame method getObjectBytes.

public static byte[] getObjectBytes(S3Client s3, String bucketName, String keyName) {
    try {
        GetObjectRequest objectRequest = GetObjectRequest.builder().key(keyName).bucket(bucketName).build();
        ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
        byte[] data = objectBytes.asByteArray();
        return data;
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return null;
}
Also used : GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest)

Example 17 with S3Exception

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

the class GeneratePresignedUrlAndUploadObject method signBucket.

// snippet-start:[presigned.java2.generatepresignedurl.main]
public static void signBucket(S3Presigner presigner, String bucketName, String keyName) {
    try {
        PutObjectRequest objectRequest = PutObjectRequest.builder().bucket(bucketName).key(keyName).contentType("text/plain").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", "text/plain");
        connection.setRequestMethod("PUT");
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        out.write("This text was uploaded as an object by using a presigned URL.");
        out.close();
        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) OutputStreamWriter(java.io.OutputStreamWriter) 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 18 with S3Exception

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

the class GetAcl method getBucketACL.

// snippet-start:[s3.java2.get_acl.main]
public static String getBucketACL(S3Client s3, String objectKey, String bucketName) {
    try {
        GetObjectAclRequest aclReq = GetObjectAclRequest.builder().bucket(bucketName).key(objectKey).build();
        GetObjectAclResponse aclRes = s3.getObjectAcl(aclReq);
        List<Grant> grants = aclRes.grants();
        String grantee = "";
        for (Grant grant : grants) {
            System.out.format("  %s: %s\n", grant.grantee().id(), grant.permission());
            grantee = grant.grantee().id();
        }
        return grantee;
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return "";
}
Also used : Grant(software.amazon.awssdk.services.s3.model.Grant) GetObjectAclRequest(software.amazon.awssdk.services.s3.model.GetObjectAclRequest) GetObjectAclResponse(software.amazon.awssdk.services.s3.model.GetObjectAclResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception)

Example 19 with S3Exception

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

the class GetObjectPresignedUrl method getPresignedUrl.

// snippet-start:[presigned.java2.getobjectpresigned.main]
public static void getPresignedUrl(S3Presigner presigner, String bucketName, String keyName) {
    try {
        GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket(bucketName).key(keyName).build();
        GetObjectPresignRequest getObjectPresignRequest = GetObjectPresignRequest.builder().signatureDuration(Duration.ofMinutes(10)).getObjectRequest(getObjectRequest).build();
        // Generate the presigned request
        PresignedGetObjectRequest presignedGetObjectRequest = presigner.presignGetObject(getObjectPresignRequest);
        // Log the presigned URL
        System.out.println("Presigned URL: " + presignedGetObjectRequest.url());
        HttpURLConnection connection = (HttpURLConnection) presignedGetObjectRequest.url().openConnection();
        presignedGetObjectRequest.httpRequest().headers().forEach((header, values) -> {
            values.forEach(value -> {
                connection.addRequestProperty(header, value);
            });
        });
        // Send any request payload that the service needs (not needed when isBrowserExecutable is true)
        if (presignedGetObjectRequest.signedPayload().isPresent()) {
            connection.setDoOutput(true);
            try (InputStream signedPayload = presignedGetObjectRequest.signedPayload().get().asInputStream();
                OutputStream httpOutputStream = connection.getOutputStream()) {
                IoUtils.copy(signedPayload, httpOutputStream);
            }
        }
        // Download the result of executing the request
        try (InputStream content = connection.getInputStream()) {
            System.out.println("Service returned response: ");
            IoUtils.copy(content, System.out);
        }
    } catch (S3Exception e) {
        e.getStackTrace();
    } catch (IOException e) {
        e.getStackTrace();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) PresignedGetObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest) OutputStream(java.io.OutputStream) IOException(java.io.IOException) PresignedGetObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) GetObjectPresignRequest(software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest)

Example 20 with S3Exception

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

the class ListObjects method listBucketObjects.

// snippet-start:[s3.java2.list_objects.main]
public static void listBucketObjects(S3Client s3, String bucketName) {
    try {
        ListObjectsRequest listObjects = ListObjectsRequest.builder().bucket(bucketName).build();
        ListObjectsResponse res = s3.listObjects(listObjects);
        List<S3Object> objects = res.contents();
        for (ListIterator iterVals = objects.listIterator(); iterVals.hasNext(); ) {
            S3Object myValue = (S3Object) iterVals.next();
            System.out.print("\n The name of the key is " + myValue.key());
            System.out.print("\n The object is " + calKb(myValue.size()) + " KBs");
            System.out.print("\n The owner is " + myValue.owner());
        }
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : ListObjectsRequest(software.amazon.awssdk.services.s3.model.ListObjectsRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse) S3Object(software.amazon.awssdk.services.s3.model.S3Object) ListIterator(java.util.ListIterator)

Aggregations

S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)56 ArrayList (java.util.ArrayList)13 GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)12 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)11 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)9 IOException (java.io.IOException)7 S3Client (software.amazon.awssdk.services.s3.S3Client)7 S3Object (software.amazon.awssdk.services.s3.model.S3Object)6 S3Waiter (software.amazon.awssdk.services.s3.waiters.S3Waiter)5 OutputStream (java.io.OutputStream)4 HttpURLConnection (java.net.HttpURLConnection)4 URL (java.net.URL)4 ListObjectsRequest (software.amazon.awssdk.services.s3.model.ListObjectsRequest)4 ListObjectsResponse (software.amazon.awssdk.services.s3.model.ListObjectsResponse)4 Tag (software.amazon.awssdk.services.s3.model.Tag)4 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 HashMap (java.util.HashMap)3 CreateBucketRequest (software.amazon.awssdk.services.s3.model.CreateBucketRequest)3 HeadBucketRequest (software.amazon.awssdk.services.s3.model.HeadBucketRequest)3