10

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.)

NikiC
  • 95,987
  • 31
  • 182
  • 219
Garrett
  • 447
  • 2
  • 4
  • 19

3 Answers3

11

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.

brasofilo
  • 23,940
  • 15
  • 86
  • 168
joschua011
  • 3,897
  • 4
  • 18
  • 25
  • I'm guessing the code responsible for the upload is here or thereabouts (not a PHP guy... so I'm just taking a stab at it): https://code.google.com/p/wordpress-s3/source/browse/trunk/tantan-s3/wordpress-s3/lib.s3.php#216 – Garrett Nov 06 '13 at 21:50
1

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 :)

Hue Man
  • 129
  • 1
  • 12
0

Yes, you can set default Content-Disposition header for your each and every upcoming uploading file in your S3 bucket using Bucket Explorer's Bucket Default feature.

For existing files, you can use Update Metadata option that update metadata on every file exist in your bucket in batch.

You just need to -

Select Key as : Content-Disposition
Add Value as : attachment;filename={$file_name_without_path_$$}

Then update metadata on the files.

See this page to set Content-Disposition on your file.

More references:

Thanks

Neelam Sharma
  • 2,485
  • 4
  • 25
  • 57