Search in sources :

Example 31 with S3Exception

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

the class MovieLensDatasetProvider 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 : HeadBucketRequest(software.amazon.awssdk.services.s3.model.HeadBucketRequest) CreateBucketRequest(software.amazon.awssdk.services.s3.model.CreateBucketRequest) HeadBucketResponse(software.amazon.awssdk.services.s3.model.HeadBucketResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) S3Waiter(software.amazon.awssdk.services.s3.waiters.S3Waiter)

Example 32 with S3Exception

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

the class S3Service method getObjectBytes.

public byte[] getObjectBytes(String bucketName, String keyName) {
    S3Client s3 = getClient();
    try {
        GetObjectRequest objectRequest = GetObjectRequest.builder().key(keyName).bucket(bucketName).build();
        // Return the byte[] from this object.
        ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
        return objectBytes.asByteArray();
    } 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) S3Client(software.amazon.awssdk.services.s3.S3Client) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest)

Example 33 with S3Exception

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

the class S3Service method listBucketObjects.

// Returns the names of all images in the given bucket.
public List<String> listBucketObjects(String bucketName) {
    S3Client s3 = getClient();
    String keyName;
    List<String> keys = new ArrayList<>();
    try {
        ListObjectsRequest listObjects = ListObjectsRequest.builder().bucket(bucketName).build();
        ListObjectsResponse res = s3.listObjects(listObjects);
        List<S3Object> objects = res.contents();
        for (S3Object myValue : objects) {
            keyName = myValue.key();
            keys.add(keyName);
        }
        return keys;
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return null;
}
Also used : ListObjectsRequest(software.amazon.awssdk.services.s3.model.ListObjectsRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ArrayList(java.util.ArrayList) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse) S3Object(software.amazon.awssdk.services.s3.model.S3Object) S3Client(software.amazon.awssdk.services.s3.S3Client)

Example 34 with S3Exception

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

the class S3Service method tagAssets.

// tag assets with labels in the given list.
public void tagAssets(List myList, String bucketName) {
    try {
        S3Client s3 = getClient();
        int len = myList.size();
        String assetName = "";
        String labelName = "";
        String labelValue = "";
        // tag all the assets in the list.
        for (Object o : myList) {
            // Need to get the WorkItem from each list.
            List innerList = (List) o;
            for (Object value : innerList) {
                WorkItem workItem = (WorkItem) value;
                assetName = workItem.getKey();
                labelName = workItem.getName();
                labelValue = workItem.getConfidence();
                tagExistingObject(s3, bucketName, assetName, labelName, labelValue);
            }
        }
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) S3Object(software.amazon.awssdk.services.s3.model.S3Object) ArrayList(java.util.ArrayList) List(java.util.List) S3Client(software.amazon.awssdk.services.s3.S3Client)

Example 35 with S3Exception

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

the class GetObjectDataAsync method getObject.

public static void getObject(S3AsyncClient s3AsyncClient, String bucketName, String keyName, String path) {
    try {
        GetObjectRequest objectRequest = GetObjectRequest.builder().key(keyName).bucket(bucketName).build();
        CompletableFuture<GetObjectResponse> futureGet = s3AsyncClient.getObject(objectRequest, AsyncResponseTransformer.toFile(Paths.get(path)));
        futureGet.whenComplete((resp, err) -> {
            try {
                if (resp != null) {
                    System.out.println("Object downloaded. Details: " + resp);
                } else {
                    err.printStackTrace();
                }
            } finally {
                // Only close the client when you are completely done with it
                s3AsyncClient.close();
            }
        });
        futureGet.join();
    } 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) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest)

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