15

I have a file on amazon S3 that has the suffix ".bin.gz". I want web browsers to treat it as a gzipped HTML file. So, I am able to go into the Amazon S3 web console (https://console.aws.amazon.com/s3/home) and navigate to the file and select it. There, under properties, I can go to the Metadata tab and add the following directives:

Content-Type: text/html
Content-Encoding: gzip

This works as expected. That's the easy part.

Now, I would like to do the same thing with hundreds (or possibly millions) of files at the time that they are PUT on S3.

I have tried using s3cmd with the --add-header option, however that gives me a signature error when I try to set the Content-Type. Further, I am pretty sure that doing that will only affect the headers that are sent at the time of the PUT operation, and not the metadata that is stored with the document.

So, I am looking for a way to do this, ideally with s3cmd. If that is not possible, I would appreciate if somebody could suggest a python library that is capable of applying metadata to a file on s3.

There must be a way to do this without having to manually set it in the console.

chaimp
  • 16,897
  • 16
  • 53
  • 86

1 Answers1

23

Use -m for setting the MIME type (which will be sent as the Content-Type header on subsequent requests:

s3cmd -m text/html --add-header='Content-Encoding: gzip' put [files] s3://...
Paul
  • 139,544
  • 27
  • 275
  • 264
  • 1
    Thanks for the quick answer. I tried that and the content-type of the uploaded file remains as "application/octet-stream" – chaimp Jun 08 '12 at 02:11
  • 1
    Got it, thank you! I just needed to change `guess_mime_type = False` in my s3 config file – chaimp Jun 08 '12 at 02:17
  • @jeffp Yeah I guess I already have that setting as `False`. You can probably specify a flag to not guess the MIME type every time you run the command too if you want to leave that setting on by default. – Paul Jun 08 '12 at 02:22
  • I checked for such a flag, wanting to do exactly that and not have to modify the settings, but it looks like you can only set it to true with the `--guess-mime-type` flag, however there is no way to set it to false. I would expect the `-m` option to override, but apparently that's the case. The s3cmd documentation says that the `-m` option would be used as a fallback if it fails to guess. – chaimp Jun 08 '12 at 02:31
  • @jeffp That's interesting because `-m` which says it sets the default doesn't change your config, it just sets it for the single request. I think one of those options needs to be changed a little to make it more suitable for all use cases. – Paul Jun 08 '12 at 02:37
  • hey I have the following command aws s3 sync ./test s3://test --content-encoding "gzip" --content-type "text/html" --cache-control "max-age=0, no-cache" --exclude "*" --include "index.html" --profile stage, how do I convert this to use s3cmd sync? – Chris Hansen Nov 05 '15 at 17:45