Skip to content

S3 PutObjectRequest - Using uploaded file head information to set metadata like ContentType automatically #442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
BarisGece opened this issue Mar 15, 2018 · 3 comments
Labels
1.x Parity feature-request A feature should be added or improved.

Comments

@BarisGece
Copy link

When I upload files without Metadata( like ContentType) in aws java sdk 1.x.x, files which are uploaded on s3 get correct ContentType value such as .html => ContentType = text/html or .js => ContentType = application/x-javascript, but when we upload files without Metadata in aws java sdk 2, all files which are uploaded on s3 get ContentType = binary/octet-stream.

When I set the ContentType value in aws2 as below, all files are getting same ContentType value as normally.

Is it possible like in sdk 1.x.x to set ContentType value from file head automatically

Sample Code

AWS SDK 1.x.x

final PutObjectRequest putRequest = new PutObjectRequest(s3bucket, targetDirectory + "/" + file.getName(), file).withCannedAcl(CannedAccessControlList.PublicRead);
final Upload upload = transferManager.upload(putRequest);

without ContentType - AWS SDK 2.0

final S3Client s3client;
s3client.putObject(PutObjectRequest.builder().bucket(s3bucket).key(targetDirectory + FileSystems.getDefault().getSeparator() + file.getFileName()).acl(ObjectCannedACL.PUBLIC_READ).build(),RequestBody.of(file));

with ContentType - AWS SDK 2.0

final S3Client s3client;
s3client.putObject(PutObjectRequest.builder().bucket(s3bucket).key(targetDirectory + FileSystems.getDefault().getSeparator() + file.getFileName()).acl(ObjectCannedACL.PUBLIC_READ).contentType("text/html").build(),RequestBody.of(file));

Your Environment

  • AWS Java SDK version used: 2.0.0-preview-8
  • JDK version used: 1.8.0_144
  • Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
  • Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
  • Operating System and version: Ubuntu - 16.04.1, Kernel Version: 4.10.0-38-generic
@BarisGece
Copy link
Author

You can use this workaround until it's implemented

@zoewangg
Copy link
Contributor

Hi @BarisGece, could you double check you are using 2.0.0-preview-8? The changes should be pushed and released in 2.0.0-preview-8, as you can see in unit test and in commit.

I also tried a quick test and the contentType looks correct.

        file = new RandomTempFile("test.js", 10_000);
        s3.putObject(PutObjectRequest.builder()
                                     .bucket(BUCKET)
                                     .key(file.getName())
                                     .build(), file.toPath());
        
        getObjectRequest = GetObjectRequest.builder()
                                           .bucket(BUCKET)
                                           .key(file.getName())
                                           .build();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GetObjectResponse response = s3.getObject(getObjectRequest, StreamingResponseHandler.toOutputStream(baos));
        assertThat(response.contentType()).isEqualTo("application/javascript");

@BarisGece
Copy link
Author

BarisGece commented Mar 23, 2018

Hi @zoewangg, yes u r right. I used 2.0.0-preview-7. It's work fine with 2.0.0-preview-8 or2.0.0-preview-9
Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.x Parity feature-request A feature should be added or improved.
Projects
None yet
Development

No branches or pull requests

4 participants