Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for setting Content-Encoding header where it can be guess…

…ed, in particular Content-Encoding: gzip is handled.
  • Loading branch information...
commit 0ee2758dd041c6f7113bb68fde1c744bed476960 1 parent 7877640
@ksperling authored
Showing with 20 additions and 5 deletions.
  1. +20 −5 S3/S3.py
View
25 S3/S3.py
@@ -28,18 +28,30 @@
from S3Uri import S3Uri
try:
- import magic
+ import magic, gzip
try:
## https://github.com/ahupp/python-magic
magic_ = magic.Magic(mime=True)
- def mime_magic(file):
+ def mime_magic_file(file):
return magic_.from_file(file)
+ def mime_magic_buffer(buffer):
+ return magic_.from_buffer(buffer)
except AttributeError:
## Older python-magic versions
magic_ = magic.open(magic.MAGIC_MIME)
magic_.load()
- def mime_magic(file):
+ def mime_magic_file(file):
return magic_.file(file)
+ def mime_magic_buffer(buffer):
+ return magic_.buffer(buffer)
+
+ def mime_magic(file):
+ type = mime_magic_file(file)
+ if type != "application/x-gzip; charset=binary":
+ return (type, None)
+ else:
+ return (mime_magic_buffer(gzip.open(file).read(8192)), 'gzip')
+
except ImportError:
magic_warned = False
def mime_magic(file):
@@ -350,12 +362,15 @@ def object_put(self, filename, uri, extra_headers = None, extra_label = ""):
headers.update(extra_headers)
headers["content-length"] = size
content_type = self.config.mime_type
+ content_encoding = None
if not content_type and self.config.guess_mime_type:
- content_type = mime_magic(filename)
+ (content_type, content_encoding) = mime_magic(filename)
if not content_type:
content_type = self.config.default_mime_type
- debug("Content-Type set to '%s'" % content_type)
+ debug("Content-Type set to '%s' (encoding %s)" % (content_type, content_encoding))
headers["content-type"] = content_type
+ if content_encoding is not None:
+ headers["content-encoding"] = content_encoding
if self.config.acl_public:
headers["x-amz-acl"] = "public-read"
if self.config.reduced_redundancy:

1 comment on commit 0ee2758

@jamshid

Hi, this change is not correct, it's setting "Content-Encoding: US-ASCII". The header should only set that to values like "gzip". I filed this bug to track s3tools#153. Please let me know if help is needed to fix this before the official release.

Please sign in to comment.
Something went wrong with that request. Please try again.