Search in sources :

Example 11 with GetObjectResponse

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

the class MovieLensDatasetProvider method getObjectBytes.

// Checks to see if the dataset is already uploaded to s3.
public static boolean getObjectBytes(S3Client s3Client, String bucketName, String keyName) {
    try {
        GetObjectRequest objectRequest = GetObjectRequest.builder().key(keyName).bucket(bucketName).build();
        ResponseBytes<GetObjectResponse> objectBytes = s3Client.getObjectAsBytes(objectRequest);
        byte[] data = objectBytes.asByteArray();
        return data.length > 0;
    } catch (NoSuchKeyException | NoSuchBucketException ex) {
        return false;
    } catch (S3Exception s3Exception) {
        System.err.println(s3Exception.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return false;
}
Also used : NoSuchKeyException(software.amazon.awssdk.services.s3.model.NoSuchKeyException) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) NoSuchBucketException(software.amazon.awssdk.services.s3.model.NoSuchBucketException) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest)

Example 12 with GetObjectResponse

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

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

the class S3AsyncStreamOps method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    S3AsyncStreamOps <bucketName> <objectKey> <path>\n\n" + "Where:\n" + "    bucketName - the name of the Amazon S3 bucket (for example, bucket1). \n\n" + "    objectKey - the name of the object (for example, book.pdf). \n" + "    path - the local path to the file (for example, C:/AWS/book.pdf). \n";
    if (args.length != 3) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucketName = args[0];
    String objectKey = args[1];
    String path = args[2];
    Region region = Region.US_WEST_2;
    S3AsyncClient client = S3AsyncClient.builder().region(region).build();
    GetObjectRequest objectRequest = GetObjectRequest.builder().bucket(bucketName).key(objectKey).build();
    CompletableFuture<GetObjectResponse> futureGet = client.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
            client.close();
        }
    });
    futureGet.join();
}
Also used : GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) Region(software.amazon.awssdk.regions.Region) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient)

Aggregations

GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)13 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)13 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)12 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 IOException (java.io.IOException)3 OutputStream (java.io.OutputStream)3 S3Client (software.amazon.awssdk.services.s3.S3Client)3 ChunkAlreadyExistsException (io.pravega.segmentstore.storage.chunklayer.ChunkAlreadyExistsException)1 ChunkNotFoundException (io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException)1 ChunkStorageException (io.pravega.segmentstore.storage.chunklayer.ChunkStorageException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 lombok.val (lombok.val)1 Region (software.amazon.awssdk.regions.Region)1 S3AsyncClient (software.amazon.awssdk.services.s3.S3AsyncClient)1 NoSuchBucketException (software.amazon.awssdk.services.s3.model.NoSuchBucketException)1 NoSuchKeyException (software.amazon.awssdk.services.s3.model.NoSuchKeyException)1