user3201500
user3201500

Reputation: 1618

Set content-type to application/octet-stream for all files in AWS S3

I am new to AWS S3 bucket I am trying to set permission of all the files to directly download. I don't want my video or images to open in browser. I just want it to be directly download.

Here is how my file permissions are set.

{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid": "AllowPublicRead",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::mybucket/*"
        }
    ]
}

Is there any way to set Content-Type : application/octet-stream for all the files.

Thank you!

Upvotes: 1

Views: 4108

Answers (2)

Jiju Thomas Mathew
Jiju Thomas Mathew

Reputation: 11

As far as I came to know either you have no control over the files that are coming in or making modifications to the code or service which pushes content to s3 might not be that easy. Whatever, my choice would be to implement a lambda which is triggered by s3:object created which just uses the AWS.S3:copyobject with source and destination same, but metadata as response-content-type application/octect-stream and response-content-disposition attachment. Once the lambda is in place, should use the AWS CLI to recursively copy existing files with the new metadata on existing files.

Better refers the AWS documents for lambda and corresponding SDK references. Check out the AWS repository on git and you may find something which goes in the same track.

Upvotes: 0

Sergey Kovalev
Sergey Kovalev

Reputation: 9411

Response headers have nothing to do with permissions.

If you want specific Content-Type or Content-Disposition headers for your files you can specify them in objects metadata like this:

enter image description here

See full tutorial from AWS here: https://docs.aws.amazon.com/AmazonS3/latest/user-guide/add-object-metadata.html

When you upload your files, for example with AWS CLI, you can specify Content-Type at the same time:

aws s3api put-object [--acl ] [--body ] --bucket [--cache-control ] [--content-disposition ] [--content-encoding ] [--content-language ] [--content-length ] [--content-md5 ] [--content-type ] ...

SDKs provide similar functionality.

Upvotes: 2

Related Questions