Search code examples
javaamazon-web-servicesobjectamazon-s3deserialization

Get an Amazon S3 object deserialized to a Java object


I have a user defined object, say MyClass, saved in an Amazon S3 bucket inside a folder. The bucket structure looks like the following:

<bucket>
-<folder>
--<my unique numbered file containing MyClass>

I need to read this file and deserialize it to MyClass.

I went through AWS docs and it suggested to use something like the following

Regions clientRegion = Regions.DEFAULT_REGION;
 String bucketName = "*** Bucket name ***";
 String key = "*** Object key ***";

 S3Object fullObject = null, objectPortion = null, headerOverrideObject = null;
 try {
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withRegion(clientRegion)
                    .withCredentials(new ProfileCredentialsProvider())
                    .build();

   fullObject = s3Client.getObject(new GetObjectRequest(bucketName, key));

Example: https://docs.aws.amazon.com/AmazonS3/latest/userguide/download-objects.html

However, I need to deserialize it to MyClass instead of S3Object. I couldn't get an example of how to do this. Can anyone suggest how this is done?


Solution

  • You are looking in the correct Guide - wrong topic for latest Java API to use. The topic you are looking at is old V1 code.

    Look at this topic to use AWS SDK for Java V2 - which is considered best practice. V2 packages start with software.amazon.awssdk.

    https://docs.aws.amazon.com/AmazonS3/latest/userguide/example_s3_GetObject_section.html

    Once you get an object from an S3 bucket, you can convert it into a byte array and from there - do what ever you need to. This V2 example shows how to get a byte array from an object.

    package com.example.s3;
    
    // snippet-start:[s3.java2.getobjectdata.import]
    import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
    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;
    // snippet-end:[s3.java2.getobjectdata.import]
    
    /**
     * Before running this Java V2 code example, set up your development environment, including your credentials.
     *
     * For more information, see the following 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" +
                    "    <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 = args[0];
            String keyName = args[1];
            String path = args[2];
    
            ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create();
            Region region = Region.US_EAST_1;
            S3Client s3 = S3Client.builder()
                    .region(region)
                    .credentialsProvider(credentialsProvider)
                    .build();
    
            getObjectBytes(s3,bucketName,keyName, path);
            s3.close();
        }
    
        // snippet-start:[s3.java2.getobjectdata.main]
        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);
            }
        }
        // snippet-end:[s3.java2.getobjectdata.main]
    }