Search in sources :

Example 26 with S3Client

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

the class S3Scenario method anotherListExample.

public static void anotherListExample(S3Client s3, String bucketName) {
    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()));
    // Helper method to work with paginated collection of items directly
    listRes.contents().stream().forEach(content -> System.out.println(" Key: " + content.key() + " size = " + content.size()));
    for (S3Object content : listRes.contents()) {
        System.out.println(" Key: " + content.key() + " size = " + content.size());
    }
}
Also used : S3Client(software.amazon.awssdk.services.s3.S3Client) Random(java.util.Random) S3Waiter(software.amazon.awssdk.services.s3.waiters.S3Waiter) ResponseBytes(software.amazon.awssdk.core.ResponseBytes) ByteBuffer(java.nio.ByteBuffer) StandardCharsets(java.nio.charset.StandardCharsets) URLEncoder(java.net.URLEncoder) java.io(java.io) ListObjectsV2Iterable(software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable) software.amazon.awssdk.services.s3.model(software.amazon.awssdk.services.s3.model) RequestBody(software.amazon.awssdk.core.sync.RequestBody) WaiterResponse(software.amazon.awssdk.core.waiters.WaiterResponse) Region(software.amazon.awssdk.regions.Region) ListObjectsV2Iterable(software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable)

Example 27 with S3Client

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

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

the class S3Scenario method createBucket.

// Create a bucket by using a S3Waiter object
public static void createBucket(S3Client s3Client, String bucketName) {
    try {
        S3Waiter s3Waiter = s3Client.waiter();
        CreateBucketRequest bucketRequest = CreateBucketRequest.builder().bucket(bucketName).build();
        s3Client.createBucket(bucketRequest);
        HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder().bucket(bucketName).build();
        // Wait until the bucket is created and print out the response.
        WaiterResponse<HeadBucketResponse> waiterResponse = s3Waiter.waitUntilBucketExists(bucketRequestWait);
        waiterResponse.matched().response().ifPresent(System.out::println);
        System.out.println(bucketName + " is ready");
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : S3Waiter(software.amazon.awssdk.services.s3.waiters.S3Waiter)

Example 29 with S3Client

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

the class SetWebsiteConfiguration method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage: " + "   <bucketName> [indexdoc] \n\n" + "Where:\n" + "   bucketName   - the Amazon S3 bucket to set the website configuration on. \n" + "   indexdoc - The index document, ex. 'index.html'\n" + "              If not specified, 'index.html' will be set.\n";
    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucketName = args[0];
    String indexDoc = "index.html";
    Region region = Region.US_EAST_1;
    S3Client s3 = S3Client.builder().region(region).build();
    setWebsiteConfig(s3, bucketName, indexDoc);
    s3.close();
}
Also used : Region(software.amazon.awssdk.regions.Region) S3Client(software.amazon.awssdk.services.s3.S3Client)

Example 30 with S3Client

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

the class DeleteMultiObjects method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "To run this example, supply the name of an Amazon S3 bucket.\n" + "\n" + "Ex: DeleteMultiObjects <bucketName>\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();
    deleteBucketObjects(s3, bucketName);
    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