Example usage for com.amazonaws.services.s3.model S3ObjectInputStream read

List of usage examples for com.amazonaws.services.s3.model S3ObjectInputStream read

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model S3ObjectInputStream read.

Prototype

@Override
    public int read() throws IOException 

Source Link

Usage

From source file:io.minio.awssdk.tests.S3TestUtils.java

License:Apache License

void downloadObject(String bucketName, String keyName, SSECustomerKey sseKey, String expectedMD5, int start,
        int length) throws Exception, IOException {
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, keyName).withSSECustomerKey(sseKey);

    if (start >= 0 && length >= 0) {
        getObjectRequest.setRange(start, start + length - 1);
    }/* w  w w .  j  a v  a2 s.  co  m*/

    S3Object s3Object = s3Client.getObject(getObjectRequest);

    int size = 0;
    int c;

    S3ObjectInputStream input = s3Object.getObjectContent();

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    String data = "";
    while ((c = input.read()) != -1) {
        output.write((byte) c);
        size++;
    }

    if (length >= 0 && size != length) {
        throw new Exception(
                "downloaded object has unexpected size, expected: " + length + ", received: " + size);
    }

    String calculatedMD5 = Utils.getBufferMD5(output.toByteArray());

    if (!expectedMD5.equals("") && !calculatedMD5.equals(expectedMD5)) {
        throw new Exception("downloaded object has unexpected md5sum, expected: " + expectedMD5 + ", found: "
                + calculatedMD5);

    }
}