Search in sources :

Example 31 with S3Client

use of software.amazon.awssdk.services.s3.S3Client 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 32 with S3Client

use of software.amazon.awssdk.services.s3.S3Client 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 33 with S3Client

use of software.amazon.awssdk.services.s3.S3Client 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 34 with S3Client

use of software.amazon.awssdk.services.s3.S3Client 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 35 with S3Client

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

the class GetObjectUrl method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    <bucketName> <keyName> \n\n" + "Where:\n" + "    bucketName - the Amazon S3 bucket name.\n\n" + "    keyName - a key name that represents the object. \n\n";
    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucketName = args[0];
    String keyName = args[1];
    Region region = Region.US_WEST_2;
    S3Client s3 = S3Client.builder().region(region).build();
    getURL(s3, bucketName, keyName);
    s3.close();
}
Also used : Region(software.amazon.awssdk.regions.Region) S3Client(software.amazon.awssdk.services.s3.S3Client)

Aggregations

S3Client (software.amazon.awssdk.services.s3.S3Client)63 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)53 Region (software.amazon.awssdk.regions.Region)43 ArrayList (java.util.ArrayList)15 GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)12 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)12 S3Object (software.amazon.awssdk.services.s3.model.S3Object)11 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)10 List (java.util.List)7 IOException (java.io.IOException)6 RequestBody (software.amazon.awssdk.core.sync.RequestBody)6 CreateBucketRequest (software.amazon.awssdk.services.s3.model.CreateBucketRequest)6 S3Waiter (software.amazon.awssdk.services.s3.waiters.S3Waiter)6 InputStream (java.io.InputStream)5 Test (org.junit.Test)5 ListObjectsRequest (software.amazon.awssdk.services.s3.model.ListObjectsRequest)5 ListObjectsResponse (software.amazon.awssdk.services.s3.model.ListObjectsResponse)5 SupplierEx (com.hazelcast.function.SupplierEx)3 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3