Yao Chen Yao Chen - 1 month ago 17
Java Question

How to set the content type and content encoding when uploading to S3?

I am uploading some files to the S3 buckets. One zip file, one .json file.

When I upload them, both files will have:

Content-Type: application/octet-stream


I need the zip files to have:

Content-Type: text/plain
Content-Encoding: gzip


What do I have to do to make sure that these files are uploaded with the desired type/encoding?

Below is some of my code:

Saving the file:

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName)));
bw.write(content);
bw.close();


Uploading the file:

AWSCredentials myCredentials = new BasicAWSCredentials(ACCESS_KEY,SECRET_KEY);
TransferManager tx = new TransferManager(myCredentials);
Upload uploading = tx.upload(new PutObjectRequest(bucket, awsFileKey, file));


What I am trying to accomplish:

I am working on an auto-suggest feature. If I upload plain json file, it is over 1.5MB, which is terrible. So next best thing is to upload the ZIP file(it reduces the file to 300kb, decent) then when user loads the page, browser should unzip. Some reason only setting the content type/encoding to those specified works, everything else fails.

Answer

ObjectMetadata. Make new then set the metadata you want.

PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, awsFileKey, file);

if (metadata != null) {
    putObjectRequest.setMetadata(metadata);
}
Comments