Search in sources :

Example 51 with S3Exception

use of software.amazon.awssdk.services.s3.model.S3Exception 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 52 with S3Exception

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

the class GetBucketPolicy method getPolicy.

// snippet-start:[s3.java2.get_bucket_policy.main]
public static String getPolicy(S3Client s3, String bucketName) {
    String policyText = "";
    System.out.format("Getting policy for bucket: \"%s\"\n\n", bucketName);
    GetBucketPolicyRequest policyReq = GetBucketPolicyRequest.builder().bucket(bucketName).build();
    try {
        GetBucketPolicyResponse policyRes = s3.getBucketPolicy(policyReq);
        policyText = policyRes.policy();
        return policyText;
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return "";
}
Also used : S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) GetBucketPolicyRequest(software.amazon.awssdk.services.s3.model.GetBucketPolicyRequest) GetBucketPolicyResponse(software.amazon.awssdk.services.s3.model.GetBucketPolicyResponse)

Example 53 with S3Exception

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

the class GetObjectTags method listTags.

// snippet-start:[s3.java2.getobjecttags.main]
public static void listTags(S3Client s3, String bucketName, String keyName) {
    try {
        GetObjectTaggingRequest getTaggingRequest = GetObjectTaggingRequest.builder().key(keyName).bucket(bucketName).build();
        GetObjectTaggingResponse tags = s3.getObjectTagging(getTaggingRequest);
        List<Tag> tagSet = tags.tagSet();
        // Write out the tags
        Iterator<Tag> tagIterator = tagSet.iterator();
        while (tagIterator.hasNext()) {
            Tag tag = (Tag) tagIterator.next();
            System.out.println(tag.key());
            System.out.println(tag.value());
        }
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : GetObjectTaggingRequest(software.amazon.awssdk.services.s3.model.GetObjectTaggingRequest) GetObjectTaggingResponse(software.amazon.awssdk.services.s3.model.GetObjectTaggingResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) Tag(software.amazon.awssdk.services.s3.model.Tag)

Example 54 with S3Exception

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

the class ListMultipartUploads method listUploads.

// snippet-start:[s3.java2.list_multi_uploads.main]
public static void listUploads(S3Client s3, String bucketName) {
    try {
        ListMultipartUploadsRequest listMultipartUploadsRequest = ListMultipartUploadsRequest.builder().bucket(bucketName).build();
        ListMultipartUploadsResponse response = s3.listMultipartUploads(listMultipartUploadsRequest);
        List<MultipartUpload> uploads = response.uploads();
        for (MultipartUpload upload : uploads) {
            System.out.println("Upload in progress: Key = \"" + upload.key() + "\", id = " + upload.uploadId());
        }
    } catch (S3Exception e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : ListMultipartUploadsResponse(software.amazon.awssdk.services.s3.model.ListMultipartUploadsResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) MultipartUpload(software.amazon.awssdk.services.s3.model.MultipartUpload) ListMultipartUploadsRequest(software.amazon.awssdk.services.s3.model.ListMultipartUploadsRequest)

Example 55 with S3Exception

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

the class VPCS3Example method listBucketObjects.

// snippet-start:[s3.java2.vpc.example.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 (S3Object s3Object : objects) {
            System.out.print("\n The name of the key is " + s3Object.key());
            System.out.print("\n The object is " + convertBToKb(s3Object.size()) + " KBs");
            System.out.print("\n The owner is " + s3Object.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)

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