Search in sources :

Example 6 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 7 with GetObjectResponse

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

Example 8 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) {
    try {
        S3Client s3 = getClient();
        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) S3Client(software.amazon.awssdk.services.s3.S3Client) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest)

Example 9 with GetObjectResponse

use of software.amazon.awssdk.services.s3.model.GetObjectResponse in project pravega by pravega.

the class S3ChunkStorage method doRead.

@Override
protected int doRead(ChunkHandle handle, long fromOffset, int length, byte[] buffer, int bufferOffset) throws ChunkStorageException {
    try {
        GetObjectRequest objectRequest = GetObjectRequest.builder().key(getObjectPath(handle.getChunkName())).range(getRangeWithLength(fromOffset, length)).bucket(config.getBucket()).build();
        ResponseBytes<GetObjectResponse> objectBytes = client.getObjectAsBytes(objectRequest);
        try (val inputStream = objectBytes.asInputStream()) {
            return StreamHelpers.readAll(inputStream, buffer, bufferOffset, length);
        }
    } catch (Exception e) {
        throw convertException(handle.getChunkName(), "doRead", e);
    }
}
Also used : lombok.val(lombok.val) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ChunkStorageException(io.pravega.segmentstore.storage.chunklayer.ChunkStorageException) ChunkNotFoundException(io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException) ChunkAlreadyExistsException(io.pravega.segmentstore.storage.chunklayer.ChunkAlreadyExistsException)

Example 10 with GetObjectResponse

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

the class DetectPPE 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)

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