Search in sources :

Example 6 with Delete

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

the class S3ObjectOperations method main.

public static void main(String[] args) throws IOException {
    final String USAGE = "\n" + "Usage:\n" + "    <bucketName> <key>\n\n" + "Where:\n" + "    bucketName - the Amazon S3 bucket to create.\n\n" + "    key - the key to use.\n\n";
    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucketName = args[0];
    String key = args[1];
    // snippet-start:[s3.java2.s3_object_operations.upload]
    Region region = Region.US_WEST_2;
    s3 = S3Client.builder().region(region).build();
    createBucket(s3, bucketName, region);
    PutObjectRequest objectRequest = PutObjectRequest.builder().bucket(bucketName).key(key).build();
    s3.putObject(objectRequest, RequestBody.fromByteBuffer(getRandomByteBuffer(10_000)));
    // snippet-end:[s3.java2.s3_object_operations.upload]
    // Multipart upload example
    String multipartKey = "multiPartKey";
    multipartUpload(bucketName, multipartKey);
    // snippet-start:[s3.java2.s3_object_operations.pagination]
    ListObjectsV2Request listObjectsReqManual = ListObjectsV2Request.builder().bucket(bucketName).maxKeys(1).build();
    boolean done = false;
    while (!done) {
        ListObjectsV2Response listObjResponse = s3.listObjectsV2(listObjectsReqManual);
        for (S3Object content : listObjResponse.contents()) {
            System.out.println(content.key());
        }
        if (listObjResponse.nextContinuationToken() == null) {
            done = true;
        }
        listObjectsReqManual = listObjectsReqManual.toBuilder().continuationToken(listObjResponse.nextContinuationToken()).build();
    }
    // snippet-end:[s3.java2.s3_object_operations.pagination]
    // snippet-start:[s3.java2.s3_object_operations.iterative]
    ListObjectsV2Request listReq = ListObjectsV2Request.builder().bucket(bucketName).maxKeys(1).build();
    ListObjectsV2Iterable listRes = s3.listObjectsV2Paginator(listReq);
    // Process response pages
    listRes.stream().flatMap(r -> r.contents().stream()).forEach(content -> System.out.println(" Key: " + content.key() + " size = " + content.size()));
    // snippet-end:[s3.java2.s3_object_operations.iterative]
    // snippet-start:[s3.java2.s3_object_operations.stream]
    // Helper method to work with paginated collection of items directly
    listRes.contents().stream().forEach(content -> System.out.println(" Key: " + content.key() + " size = " + content.size()));
    // snippet-start:[s3.java2.s3_object_operations.forloop]
    for (S3Object content : listRes.contents()) {
        System.out.println(" Key: " + content.key() + " size = " + content.size());
    }
    // snippet-end:[s3.java2.s3_object_operations.forloop]
    // snippet-start:[s3.java2.s3_object_operations.download]
    GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket(bucketName).key(key).build();
    s3.getObject(getObjectRequest);
    // snippet-end:[s3.java2.s3_object_operations.download]
    // snippet-start:[s3.java2.s3_object_operations.delete]
    DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder().bucket(bucketName).key(key).build();
    s3.deleteObject(deleteObjectRequest);
    // snippet-end:[s3.java2.s3_object_operations.delete]
    // Delete an object
    deleteObjectRequest = DeleteObjectRequest.builder().bucket(bucketName).key(multipartKey).build();
    s3.deleteObject(deleteObjectRequest);
    deleteBucket(s3, bucketName);
    System.out.println("Done");
}
Also used : CompletedMultipartUpload(software.amazon.awssdk.services.s3.model.CompletedMultipartUpload) S3Object(software.amazon.awssdk.services.s3.model.S3Object) CreateMultipartUploadResponse(software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse) Random(java.util.Random) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ListObjectsV2Response(software.amazon.awssdk.services.s3.model.ListObjectsV2Response) ByteBuffer(java.nio.ByteBuffer) HeadBucketResponse(software.amazon.awssdk.services.s3.model.HeadBucketResponse) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) CreateBucketRequest(software.amazon.awssdk.services.s3.model.CreateBucketRequest) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) WaiterResponse(software.amazon.awssdk.core.waiters.WaiterResponse) Region(software.amazon.awssdk.regions.Region) UploadPartRequest(software.amazon.awssdk.services.s3.model.UploadPartRequest) S3Client(software.amazon.awssdk.services.s3.S3Client) DeleteBucketRequest(software.amazon.awssdk.services.s3.model.DeleteBucketRequest) HeadBucketRequest(software.amazon.awssdk.services.s3.model.HeadBucketRequest) IOException(java.io.IOException) S3Waiter(software.amazon.awssdk.services.s3.waiters.S3Waiter) ListObjectsV2Request(software.amazon.awssdk.services.s3.model.ListObjectsV2Request) CompletedPart(software.amazon.awssdk.services.s3.model.CompletedPart) CompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest) DeleteObjectRequest(software.amazon.awssdk.services.s3.model.DeleteObjectRequest) ListObjectsV2Iterable(software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable) CreateBucketConfiguration(software.amazon.awssdk.services.s3.model.CreateBucketConfiguration) RequestBody(software.amazon.awssdk.core.sync.RequestBody) CreateMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest) DeleteObjectRequest(software.amazon.awssdk.services.s3.model.DeleteObjectRequest) ListObjectsV2Request(software.amazon.awssdk.services.s3.model.ListObjectsV2Request) ListObjectsV2Iterable(software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable) Region(software.amazon.awssdk.regions.Region) S3Object(software.amazon.awssdk.services.s3.model.S3Object) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) ListObjectsV2Response(software.amazon.awssdk.services.s3.model.ListObjectsV2Response)

Example 7 with Delete

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

the class S3Scenario method main.

public static void main(String[] args) throws IOException {
    final String usage = "\n" + "Usage:\n" + "    <bucketName> <key> <objectPath> <savePath> <toBucket>\n\n" + "Where:\n" + "    bucketName - the Amazon S3 bucket to create.\n\n" + "    key - the key to use.\n\n" + "    objectPath - the path where the file is located (for example, C:/AWS/book2.pdf). " + "    savePath - the path where the file is saved after it's downloaded (for example, C:/AWS/book2.pdf). " + "    toBucket - an Amazon S3 bucket to where an object is copied to (for example, C:/AWS/book2.pdf). ";
    if (args.length != 5) {
        System.out.println(usage);
        System.exit(1);
    }
    String bucketName = args[0];
    String key = args[1];
    String objectPath = args[2];
    String savePath = args[3];
    String toBucket = args[4];
    Region region = Region.US_EAST_1;
    S3Client s3 = S3Client.builder().region(region).build();
    // Create an Amazon S3 bucket.
    createBucket(s3, bucketName);
    // Update a local file to the Amazon S3 bucket.
    uploadLocalFile(s3, bucketName, key, objectPath);
    // Download the object to another local file.
    getObjectBytes(s3, bucketName, key, savePath);
    // Perform a multipart upload.
    String multipartKey = "multiPartKey";
    multipartUpload(s3, toBucket, multipartKey);
    // List all objects located in the Amazon S3 bucket.
    // Show 2 ways
    listAllObjects(s3, bucketName);
    anotherListExample(s3, bucketName);
    // Copy the object to another Amazon S3 bucket
    copyBucketObject(s3, bucketName, key, toBucket);
    // Delete the object from the Amazon S3 bucket.
    deleteObjectFromBucket(s3, bucketName, key);
    // Delete the Amazon S3 bucket
    deleteBucket(s3, bucketName);
    System.out.println("All Amazon S3 operations were successfully performed");
    s3.close();
}
Also used : Region(software.amazon.awssdk.regions.Region) S3Client(software.amazon.awssdk.services.s3.S3Client)

Example 8 with Delete

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

the class DeleteMultiObjects method deleteBucketObjects.

// snippet-start:[s3.java2.delete_many_objects.main]
public static void deleteBucketObjects(S3Client s3, String bucketName) {
    // Upload three sample objects to the specfied Amazon S3 bucket.
    ArrayList<ObjectIdentifier> keys = new ArrayList<>();
    PutObjectRequest putOb = null;
    ObjectIdentifier objectId = null;
    for (int i = 0; i < 3; i++) {
        String keyName = "delete object example " + i;
        objectId = ObjectIdentifier.builder().key(keyName).build();
        putOb = PutObjectRequest.builder().bucket(bucketName).key(keyName).build();
        s3.putObject(putOb, RequestBody.fromString(keyName));
        keys.add(objectId);
    }
    System.out.println(keys.size() + " objects successfully created.");
    // Delete multiple objects in one request.
    Delete del = Delete.builder().objects(keys).build();
    try {
        DeleteObjectsRequest multiObjectDeleteRequest = DeleteObjectsRequest.builder().bucket(bucketName).delete(del).build();
        s3.deleteObjects(multiObjectDeleteRequest);
        System.out.println("Multiple objects are deleted!");
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : Delete(software.amazon.awssdk.services.s3.model.Delete) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ArrayList(java.util.ArrayList) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) ObjectIdentifier(software.amazon.awssdk.services.s3.model.ObjectIdentifier) DeleteObjectsRequest(software.amazon.awssdk.services.s3.model.DeleteObjectsRequest)

Example 9 with Delete

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

the class DeleteObjects method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "To run this example, supply the name of an S3 bucket and at least\n" + "one object name (key) to delete.\n" + "\n" + "Ex: <bucketName> <objectName>\n";
    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucketName = args[0];
    String objectName = args[1];
    System.out.println("Deleting an object from the Amazon S3 bucket: " + bucketName);
    Region region = Region.US_WEST_2;
    S3Client s3 = S3Client.builder().region(region).build();
    deleteBucketObjects(s3, bucketName, objectName);
    s3.close();
}
Also used : Region(software.amazon.awssdk.regions.Region) S3Client(software.amazon.awssdk.services.s3.S3Client)

Example 10 with Delete

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

the class DeleteBucketPolicy method deleteS3BucketPolicy.

// snippet-start:[s3.java2.delete_bucket_policy.main]
// Delete the bucket policy
public static void deleteS3BucketPolicy(S3Client s3, String bucketName) {
    DeleteBucketPolicyRequest delReq = DeleteBucketPolicyRequest.builder().bucket(bucketName).build();
    try {
        s3.deleteBucketPolicy(delReq);
        System.out.println("Done!");
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : DeleteBucketPolicyRequest(software.amazon.awssdk.services.s3.model.DeleteBucketPolicyRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception)

Aggregations

S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)10 S3Client (software.amazon.awssdk.services.s3.S3Client)9 ArrayList (java.util.ArrayList)8 Region (software.amazon.awssdk.regions.Region)7 DeleteObjectsRequest (software.amazon.awssdk.services.s3.model.DeleteObjectsRequest)6 ObjectIdentifier (software.amazon.awssdk.services.s3.model.ObjectIdentifier)5 ListObjectsV2Request (software.amazon.awssdk.services.s3.model.ListObjectsV2Request)4 ListObjectsV2Response (software.amazon.awssdk.services.s3.model.ListObjectsV2Response)4 S3Object (software.amazon.awssdk.services.s3.model.S3Object)4 IOException (java.io.IOException)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Test (org.junit.Test)2 CompleteMultipartUploadRequest (software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest)2 CompletedMultipartUpload (software.amazon.awssdk.services.s3.model.CompletedMultipartUpload)2 CompletedPart (software.amazon.awssdk.services.s3.model.CompletedPart)2 CreateMultipartUploadRequest (software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest)2 CreateMultipartUploadResponse (software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse)2 Delete (software.amazon.awssdk.services.s3.model.Delete)2 DeleteBucketRequest (software.amazon.awssdk.services.s3.model.DeleteBucketRequest)2