Ommadawn
Ommadawn

Reputation: 2730

Set object metadata in AWS S3 Java SDK

Using the AWS SDK version 2 for Java, I'm trying to set the Content-Type metadata for the objects I upload.

I used this code for my image uploads:

S3Client s3Client = S3Client.builder().
                        region(MY_REGION).
                        credentialsProvider(MY_CREDENTIALS).
                        build();

Map<String, String> image_metadata = new HashMap<>();
image_metadata.put("Content-Type", "image/jpeg");

PutObjectRequest request = PutObjectRequest.builder().
                    bucket(MY_BUCKET).
                    key(remoteDir + remoteFileName).
                    acl(ObjectCannedACL.PUBLIC_READ).
                    storageClass(StorageClass.STANDARD).
                    metadata(image_metadata).
                    build();

If I don't set metadata method, the object content-type will be guessed by AWS, and I've found every image appear as "application/octet-stream" instead of "image/jpeg".

But when the metadata method is set, the object appears with 2 metadata:

Content-Type=application/octet-stream
x-amz-meta-content-type=image/jpeg

So... How could I set the metadata to achieve just one metadata like?:

Content-Type=image/jpeg

Thanks!

Upvotes: 2

Views: 9688

Answers (1)

Ommadawn
Ommadawn

Reputation: 2730

Simplier as it seemed:

PutObjectRequest request = PutObjectRequest.builder().
                    bucket(MY_BUCKET).
                    key(remoteDir + remoteFileName).
                    acl(ObjectCannedACL.PUBLIC_READ).
                    storageClass(StorageClass.STANDARD).
                    contentType("image/jpeg").
                    build();

Upvotes: 7

Related Questions