Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Being able to set 'content-type' to a resource before uploading to an S3 bucket #262

Closed
yngwuoso opened this issue Sep 23, 2017 · 3 comments · Fixed by #706
Closed

Being able to set 'content-type' to a resource before uploading to an S3 bucket #262

yngwuoso opened this issue Sep 23, 2017 · 3 comments · Fixed by #706
Labels
component: s3 S3 integration related issue status: waiting-for-triage An issue we've not yet triaged type: enhancement A general enhancement

Comments

@yngwuoso
Copy link

I see that org.springframework.cloud.aws.core.io.s3.SimpleStorageResource.SimpleStorageOutputStream.finishSimpleUpload() doesn't take into account the content-type of the content.

Can you take it into account and find a solution?

Regards.

@pacey
Copy link

pacey commented Jun 25, 2018

This would be super handy. All of the objects I've been uploading have the content-type application/octet-stream which isn't great

@pacey
Copy link

pacey commented Jun 26, 2018

From digging into the aws java sdk it would appear that for a simple put object request it will actually set the correct content-type metadata on the object for you. I've seen this behaviour using it from spring-cloud-aws too. However, the multipart upload api doesn't do this behaviour and I've raised an issue to discuss adding this aws/aws-sdk-java#1648.

I think it would still be nice to set the content-type on the SimpleStorageResource and not rely on the aws s3 sdk to do this. I would be happy to contribute for this change, if one of the main contributors could point me in the right direction.

One possible, albeit a bit ugly, solution to this would be to have a setContentType method on the SimpleStorageOutputStream class. But granted, that isn't encapsulated very well. The content type could be set on the SimpleStorageResource, however this might be confusing if you "get" a resource and then change it's content type it wouldn't actually be updated in S3.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 1, 2018
@spencergibb spencergibb added help wanted type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 29, 2019
@paulcwarren
Copy link

paulcwarren commented Feb 26, 2019

This would be useful for me too and, in fact, I would be happy to have a shot at implementing it but I dont think the setContentType on SimpleStorageOutputStream would be accepted by the Spring team honestly. The problem I forsee with this approach is that WritableResource.getOutputStream returns OutputStream, not SimpleStorageOutputStream and therefore a consumer would have to know the resource they have in hand and cast it in order to set the content type.

So, what about modifying SimpleStorageProtocolResolver as follows:

    @Override
    public Resource resolve(String location, ResourceLoader resourceLoader) {
        if (SimpleStorageNameUtils.isSimpleStorageResource(location)) {
            return new SimpleStorageResource(this.amazonS3, SimpleStorageNameUtils.getBucketNameFromLocation(location),
                    SimpleStorageNameUtils.getObjectNameFromLocation(location), this.taskExecutor,
                    SimpleStorageNameUtils.getVersionIdFromLocation(location), SimpleStorageNameUtils.getContentTypeFromLocation(location));
        } else {
            return null;
        }
    }

and SimpleStorageResource as follows:

SimpleStorageResource(AmazonS3 amazonS3, String bucketName, String objectName, TaskExecutor taskExecutor, String versionId, String contentType)

i.e. infer the content-type from the file extension of the resource.

This has the advantages that it requires no changes to the "API". Works in a way often used by other web and file-based resources and, in fact, how AWS SDK's putObject works when you give it a java.io.File instead of an InputStream.

The downside here is that the object name (and therefore s3 key) would also include a file extension. We could also modify getObjectNameFromLocation to remove the extension in much the same way it removes the version id. But again, I wonder what the Spring team would think about this. To me, it seems like it might be viewed as undesirable complexity/a backwards compatibility issue.

@maciejwalkowiak maciejwalkowiak added component: s3 S3 integration related issue status: waiting-for-triage An issue we've not yet triaged and removed help wanted labels May 29, 2020
maciejwalkowiak pushed a commit to maciejwalkowiak/spring-cloud-aws that referenced this issue Oct 15, 2020
maciejwalkowiak added a commit to awspring/spring-cloud-aws that referenced this issue Dec 19, 2020
…aws#706)

Sets content type to files uploaded to S3.

Fixes spring-attic/spring-cloud-aws#262
Closes spring-attic/spring-cloud-aws#419

Co-authored-by: Paul Warren <paul.warren@emc.com>
juho9000 pushed a commit to juho9000/spring-cloud-aws that referenced this issue Apr 29, 2021
…aws#706)

Sets content type to files uploaded to S3.

Fixes spring-attic/spring-cloud-aws#262
Closes spring-attic/spring-cloud-aws#419

Co-authored-by: Paul Warren <paul.warren@emc.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
component: s3 S3 integration related issue status: waiting-for-triage An issue we've not yet triaged type: enhancement A general enhancement
Development

Successfully merging a pull request may close this issue.

6 participants