Home > Net >  How to read a file from an Amazon S3 bucket using the AWS SDK for Java V2
How to read a file from an Amazon S3 bucket using the AWS SDK for Java V2

Time:09-28

I was able to read the file using AmazonS3Client, but now I'm trying read the file using the package software.amazon.awssdk.services.s3.S3Client. I did not find the option to pass the file name to the GetObjectRequest constructoe unlike com.amazonaws.services.s3 package. Here is my sample code.

import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
import software.amazon.awssdk.services.s3.model.S3Object;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;

main(String[] args){
String bucketName = "some-name-s3";
Region region = Region.US_EAST_1;
            S3Client s3client = S3Client
                    .builder()
                    .region(region)
                    .build();
ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder().bucket(bucketName).build();
ListObjectsV2Response listObjectsV2 = s3client.listObjectsV2(listObjectsV2Request);
            List<S3Object> s3Objects = listObjectsV2.contents();
}

Now how to read the each file and process the stream from s3Objects.

CodePudding user response:

You are using the wrong logic to read an object from an Amazon S3 bucket using the AWS SDK for Java V2. You are calling list buckets. You can get metadata about each object calling listObjectsV2. For example, you can invoke the S3Object's key() method to get the key name.

Now to read an object from an Amazon S3 bucket, you need the bucket name and key name and then invoke getObjectAsBytes, as shown in this Java logic which shows how to read a PDF document and write it to a local path:

package com.example.s3;

import software.amazon.awssdk.core.ResponseBytes;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
    
/**
 * To run this AWS code example, ensure that you have setup your development environment, including your AWS credentials.
 *
 * For information, see this documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */

public class GetObjectData {

    public static void main(String[] args) {

     final String USAGE = "\n"  
                "Usage:\n"  
                "    GetObjectData <bucketName> <keyName> <path>\n\n"  
                "Where:\n"  
                "    bucketName - the Amazon S3 bucket name. \n\n" 
                "    keyName - the key name. \n\n" 
                "    path - the path where the file is written to. \n\n";

        if (args.length != 3) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String bucketName = "myBucket";
        String keyName = "book.pdf";
        String path = "C:/AWS/book.pdf";

        Region region = Region.US_EAST_1;
        S3Client s3 = S3Client.builder()
                .region(region)
                .build();

        getObjectBytes(s3,bucketName,keyName, path);
        s3.close();
    }

    
    public static void getObjectBytes (S3Client s3, String bucketName, String keyName, String path ) {

        try {
            GetObjectRequest objectRequest = GetObjectRequest
                    .builder()
                    .key(keyName)
                    .bucket(bucketName)
                    .build();

            ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
            byte[] data = objectBytes.asByteArray();

            // Write the data to a local file
            File myFile = new File(path );
            OutputStream os = new FileOutputStream(myFile);
            os.write(data);
            System.out.println("Successfully obtained bytes from an S3 object");
            os.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (S3Exception e) {
          System.err.println(e.awsErrorDetails().errorMessage());
           System.exit(1);
        }
    }
} 

Find this example and many other Amazon S3 Java V2 code examples in Github here:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2/example_code/s3

  • Related