Search in sources :

Example 21 with S3Exception

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

the class AbortMultipartUpload method abortUploads.

// snippet-start:[s3.java2.abort_upload.main]
public static void abortUploads(S3Client s3, String bucketName, String accountId) {
    try {
        ListMultipartUploadsRequest listMultipartUploadsRequest = ListMultipartUploadsRequest.builder().bucket(bucketName).build();
        ListMultipartUploadsResponse response = s3.listMultipartUploads(listMultipartUploadsRequest);
        List<MultipartUpload> uploads = response.uploads();
        AbortMultipartUploadRequest abortMultipartUploadRequest = null;
        for (MultipartUpload upload : uploads) {
            abortMultipartUploadRequest = AbortMultipartUploadRequest.builder().bucket(bucketName).key(upload.key()).expectedBucketOwner(accountId).uploadId(upload.uploadId()).build();
            s3.abortMultipartUpload(abortMultipartUploadRequest);
        }
    } 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) AbortMultipartUploadRequest(software.amazon.awssdk.services.s3.model.AbortMultipartUploadRequest) MultipartUpload(software.amazon.awssdk.services.s3.model.MultipartUpload) ListMultipartUploadsRequest(software.amazon.awssdk.services.s3.model.ListMultipartUploadsRequest)

Example 22 with S3Exception

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

the class GetObjectData method getObjectBytes.

// snippet-start:[s3.java2.getobjectdata.main]
public static void getObjectBytes(S3Client s3, String bucketName, String keyName, String path) {
    try {
        GetObjectRequest objectRequest = GetObjectRequest.builder().key(keyName).bucket(bucketName).build();
        ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
        byte[] data = objectBytes.asByteArray();
        // Write the data to a local file
        File myFile = new File(path);
        OutputStream os = new FileOutputStream(myFile);
        os.write(data);
        System.out.println("Successfully obtained bytes from an S3 object");
        os.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) File(java.io.File)

Example 23 with S3Exception

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

the class KMSEncryptionExample method getEncryptedData.

// Obtain the encrypted data, decrypt it, and write the data to a text file
public static void getEncryptedData(S3Client s3, String bucketName, String objectName, String path, String keyId) {
    try {
        GetObjectRequest objectRequest = GetObjectRequest.builder().key(objectName).bucket(bucketName).build();
        // Get the byte[] from the Amazon S3 bucket
        ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
        byte[] data = objectBytes.asByteArray();
        // Decrypt the data by using the AWS Key Management Service
        byte[] unEncryptedData = decryptData(data, keyId);
        // Write the data to a local file
        File myFile = new File(path);
        OutputStream os = new FileOutputStream(myFile);
        os.write(unEncryptedData);
        System.out.println("Successfully obtained and decrypted bytes from the Amazon S3 bucket");
        // Close the file
        os.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) File(java.io.File)

Example 24 with S3Exception

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

the class KMSEncryptionExample method putEncryptData.

// snippet-start:[s3.java2.kms.main]
// Encrypt data and place the encrypted data into an Amazon S3 bucket
public static void putEncryptData(S3Client s3, String objectName, String bucketName, String objectPath, String keyId) {
    try {
        PutObjectRequest objectRequest = PutObjectRequest.builder().bucket(bucketName).key(objectName).build();
        byte[] myData = getObjectFile(objectPath);
        // Encrypt the data by using the AWS Key Management Service
        byte[] encryptData = encryptData(keyId, myData);
        s3.putObject(objectRequest, RequestBody.fromBytes(encryptData));
    } catch (S3Exception e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest)

Example 25 with S3Exception

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

the class ManagingObjectTags method updateObjectTags.

public static void updateObjectTags(S3Client s3, String bucketName, String objectKey) {
    try {
        // Retrieve the object's tags.
        GetObjectTaggingRequest taggingRequest = GetObjectTaggingRequest.builder().bucket(bucketName).key(objectKey).build();
        GetObjectTaggingResponse getTaggingRes = s3.getObjectTagging(taggingRequest);
        // Write out the tags.
        List<Tag> obTags = getTaggingRes.tagSet();
        for (Tag sinTag : obTags) {
            System.out.println("The tag key is: " + sinTag.key());
            System.out.println("The tag value is: " + sinTag.value());
        }
        // Replace the object's tags with two new tags.
        Tag tag3 = Tag.builder().key("Tag 3").value("This is tag 3").build();
        Tag tag4 = Tag.builder().key("Tag 4").value("This is tag 4").build();
        List<Tag> tags = new ArrayList<Tag>();
        tags.add(tag3);
        tags.add(tag4);
        Tagging updatedTags = Tagging.builder().tagSet(tags).build();
        PutObjectTaggingRequest taggingRequest1 = PutObjectTaggingRequest.builder().bucket(bucketName).key(objectKey).tagging(updatedTags).build();
        s3.putObjectTagging(taggingRequest1);
        // Write out the modified tags.
        GetObjectTaggingResponse getTaggingRes2 = s3.getObjectTagging(taggingRequest);
        List<Tag> modTags = getTaggingRes2.tagSet();
        for (Tag sinTag : modTags) {
            System.out.println("The tag key is: " + sinTag.key());
            System.out.println("The tag value is: " + sinTag.value());
        }
    } catch (S3Exception e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : GetObjectTaggingRequest(software.amazon.awssdk.services.s3.model.GetObjectTaggingRequest) PutObjectTaggingRequest(software.amazon.awssdk.services.s3.model.PutObjectTaggingRequest) GetObjectTaggingResponse(software.amazon.awssdk.services.s3.model.GetObjectTaggingResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ArrayList(java.util.ArrayList) Tagging(software.amazon.awssdk.services.s3.model.Tagging) Tag(software.amazon.awssdk.services.s3.model.Tag)

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