Search in sources :

Example 66 with Bucket

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

the class KMSEncryptionExample method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    <objectName> <bucketName> <objectPath> <outPath> <keyId>\n\n" + "Where:\n" + "    objectName - the name of the object. \n\n" + "    bucketName - the Amazon S3 bucket name that contains the object (for example, bucket1). \n" + "    objectPath - the path to a TXT file to encrypt and place into a Amazon S3 bucket (for example, C:/AWS/test.txt).\n" + "    outPath - the path where a text file is written to after it's decrypted (for example, C:/AWS/testPlain.txt).\n" + "    keyId - the id of the AWS KMS key to use to encrpt/decrypt the data. You can obtain the key ID value from the AWS Management Console.\n";
    if (args.length != 5) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String objectName = args[0];
    String bucketName = args[1];
    String objectPath = args[2];
    String outPath = args[3];
    String keyId = args[4];
    Region region = Region.US_WEST_2;
    S3Client s3 = S3Client.builder().region(region).build();
    putEncryptData(s3, objectName, bucketName, objectPath, keyId);
    getEncryptedData(s3, bucketName, objectName, outPath, keyId);
    s3.close();
}
Also used : Region(software.amazon.awssdk.regions.Region) S3Client(software.amazon.awssdk.services.s3.S3Client)

Example 67 with Bucket

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

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

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

the class ListMultipartUploads method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    <bucketName> \n\n" + "Where:\n" + "    bucketName - the name of the Amazon S3 bucket where an in-progress multipart upload is occurring.\n\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucketName = args[0];
    Region region = Region.US_WEST_2;
    S3Client s3 = S3Client.builder().region(region).build();
    listUploads(s3, bucketName);
    s3.close();
}
Also used : Region(software.amazon.awssdk.regions.Region) S3Client(software.amazon.awssdk.services.s3.S3Client)

Example 70 with Bucket

use of software.amazon.awssdk.services.s3.model.Bucket 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)60 S3Client (software.amazon.awssdk.services.s3.S3Client)53 Region (software.amazon.awssdk.regions.Region)47 Bucket (com.amazonaws.services.s3.model.Bucket)32 ArrayList (java.util.ArrayList)24 Test (org.junit.Test)22 IOException (java.io.IOException)18 GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)18 S3Object (software.amazon.awssdk.services.s3.model.S3Object)17 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)14 HeadObjectResponse (software.amazon.awssdk.services.s3.model.HeadObjectResponse)14 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)14 ListObjectsV2Response (software.amazon.awssdk.services.s3.model.ListObjectsV2Response)11 AmazonS3 (com.amazonaws.services.s3.AmazonS3)10 ListObjectsV2Request (software.amazon.awssdk.services.s3.model.ListObjectsV2Request)10 CompleteMultipartUploadRequest (software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest)9 S3TestUtils.buildMockedS3FileSystem (org.apache.beam.sdk.io.aws2.s3.S3TestUtils.buildMockedS3FileSystem)8 MatchResult (org.apache.beam.sdk.io.fs.MatchResult)8 Date (java.util.Date)7 CompletedPart (software.amazon.awssdk.services.s3.model.CompletedPart)7