Skip to content

Commit

Permalink
allow adding encoding to content type in s3
Browse files Browse the repository at this point in the history
  • Loading branch information
cjnoyescrisp committed Jul 18, 2012
1 parent 19a529a commit 4d29dd4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions S3/Config.py
Expand Up @@ -40,6 +40,7 @@ class Config(object):
proxy_port = 3128
encrypt = False
dry_run = False
add_encoding_ext = []
preserve_attrs = True
preserve_attrs_list = [
'uname', # Verbose owner Name (e.g. 'root')
Expand Down
13 changes: 13 additions & 0 deletions S3/S3.py
Expand Up @@ -339,6 +339,14 @@ def website_delete(self, uri, bucket_location = None):

return response

def add_encoding(self, filename):
parts = filename.rsplit('.',2)
ext = parts[1]
if ext in self.config.add_encoding_ext:
return True
else:
return False

def object_put(self, filename, uri, extra_headers = None, extra_label = ""):
# TODO TODO
# Make it consistent with stream-oriented object_get()
Expand All @@ -363,6 +371,11 @@ def object_put(self, filename, uri, extra_headers = None, extra_label = ""):
content_type = mime_magic(filename)
if not content_type:
content_type = self.config.default_mime_type

## add charset to content type
if self.add_encoding(filename):
content_type = content_type + ";charset=" + self.config.encoding.upper()

debug("Content-Type set to '%s'" % content_type)
headers["content-type"] = content_type

Expand Down
1 change: 1 addition & 0 deletions s3cmd
Expand Up @@ -1524,6 +1524,7 @@ def main():
optparser.add_option( "--add-header", dest="add_header", action="append", metavar="NAME:VALUE", help="Add a given HTTP header to the upload request. Can be used multiple times. For instance set 'Expires' or 'Cache-Control' headers (or both) using this options if you like.")

optparser.add_option( "--encoding", dest="encoding", metavar="ENCODING", help="Override autodetected terminal and filesystem encoding (character set). Autodetected: %s" % preferred_encoding)
optparser.add_option( "--add-encoding-ext", dest="add_encoding_ext", action="append", metavar="EXTENSION", help="Add encoding to this ext when uploading to S3, may be used multiple times")
optparser.add_option( "--verbatim", dest="urlencoding_mode", action="store_const", const="verbatim", help="Use the S3 name as given on the command line. No pre-processing, encoding, etc. Use with caution!")

optparser.add_option( "--disable-multipart", dest="enable_multipart", action="store_false", help="Disable multipart upload on files bigger than --multipart-chunk-size-mb")
Expand Down

1 comment on commit 4d29dd4

@cjnoyes
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For extensions set with --add-encoding-ext add the encoding charset to content type i.e. if content type is text/html and encoding was utf-8 content type would be text/html;charset=UTF-8 in s3 when downloaded

Please sign in to comment.