Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Content-Disposition Headers as a default on Amazon S3 Bucket

The problem I have is that I need the Content-Disposition: attachment header to be present on EVERY file that hits my bucket.

In Wordpress, I can just use .htaccess to cover the filetypes in question (videos), but those rules do not extend to my S3 downloads which browsers are simply trying to open, instead of download.

I need an automated/default solution, since I am not the only one that uploads these files (our staff uploads through Wordpress, and the uploads all are stored on our S3 bucket). So using Cloudberry or other browsers is not useful for this situation. I can't adjust the files on a per-file basis (the uploads are too frequent).

Is there a way to do this?

(Other information: I'm using the "Amazon S3 and Cloudfront" plugin on Wordpress that is responsible for linking the two together. Unfortunately, the site is not public, so I cannot link to it.)

like image 782
Garrett Avatar asked Oct 22 '13 03:10

Garrett


2 Answers

Unfortunately there is no way to set this for an entire bucket in S3, and also Cloudfront can only set Cache-Headers

But you can set The Content-disposition parameter when uploading files to S3.

For existing files, you must change the Header, so loop through every object in the Bucket, and copy it to itself using the new headers.

All I can say now, please post the code that uploads the file to S3.

like image 175
joschua011 Avatar answered Sep 27 '22 18:09

joschua011


First, you need to locate the code that puts the object in the bucket. You can use notepad++ to search for "putObject" within the php files of whatever plugin you are using. An example code from another WP plugin that stores files to S3 is as follows:

$this->s3->putObject( array(
            'Bucket'     => $bucket,
            'Key'        => $file['name'],
            'SourceFile' => $file['file'],
        ) );

Now, simply add ContentDisposition' => 'attachment' like so:

$this->s3->putObject( array(
            'Bucket'     => $bucket,
            'Key'        => $file['name'],
            'SourceFile' => $file['file'],
            'ContentDisposition' => 'attachment',
        ) );

Thats it :)

like image 20
Hue Man Avatar answered Sep 27 '22 17:09

Hue Man