Search code examples
javaamazon-web-servicesspring-bootamazon-s3jpeg

Get content Type from AWS S3 Image in Java


I have an image stored in AWS S3 Bucket

URL url = new URL(urlString);
        URI dataUri = null;
        if (null != url) {
            try (InputStream inStreamGuess = url.openStream();
                    InputStream inStreamConvert = url.openStream();
                    ByteArrayOutputStream os = new ByteArrayOutputStream()) {
                String contentType = URLConnection.guessContentTypeFromStream(inStreamGuess);
                if (null != contentType) {
...
}

but content Type is null


Solution

  • To get the content type of an Object in an Amazon S3 bucket using Java, use the AWS SDK for Java V2.

    https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/s3/S3Client.html#headObject-

    Use the headObject method that returns a HeadObjectResponse object. Then invoke: https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/s3/model/HeadObjectResponse.html#contentType-- method.

    Java V2 example that works

    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3Client;
    import software.amazon.awssdk.services.s3.model.*;
    
    /**
     * 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 GetObjectContentType {
    
        public static void main(String[] args) {
    
            final String USAGE = "\n" +
                    "Usage:\n" +
                    "    GetObjectContentType <bucketName> <keyName>>\n\n" +
                    "Where:\n" +
                    "    bucketName - the Amazon S3 bucket name. \n\n"+
                    "    keyName - the key name. \n\n";
    
            if (args.length != 2) {
                System.out.println(USAGE);
                System.exit(1);
            }
    
            String bucketName = args[0];
            String keyName = args[1];
    
            Region region = Region.US_WEST_2;
            S3Client s3 = S3Client.builder()
                    .region(region)
                    .build();
    
            getContentType(s3,bucketName,keyName);
            s3.close();
        }
    
        
        public static void getContentType (S3Client s3, String bucketName, String keyName) {
    
            try {
                HeadObjectRequest objectRequest = HeadObjectRequest.builder()
                        .key(keyName)
                        .bucket(bucketName)
                        .build();
    
                HeadObjectResponse objectHead = s3.headObject(objectRequest);
                String type = objectHead.contentType();
                System.out.println("The object content type is "+type);
    
            } catch (S3Exception e) {
                System.err.println(e.awsErrorDetails().errorMessage());
                System.exit(1);
            }
        }
    }