def put(self, filedata, content_type, remote_path, force=False): now = datetime.datetime.utcnow() then = now + datetime.timedelta(self.expiration_days) expires = then.strftime("%a, %d %b %Y %H:%M:%S GMT") if self.aws_prefix: remote_path = "%s/%s" % (self.aws_prefix, remote_path) (hexdigest, b64digest) = mediasync.checksum(filedata) raw_b64digest = b64digest # store raw b64digest to add as file metadata # create initial set of headers headers = { "x-amz-acl": "public-read", "Content-Type": content_type, "Expires": expires, "Cache-Control": 'max-age=%d, public' % (self.expiration_days * 24 * 3600), } key = self._bucket.get_key(remote_path) if key is None: key = Key(self._bucket, remote_path) key_meta = key.get_metadata('mediasync-checksum') or '' s3_checksum = key_meta.replace(' ', '+') if force or s3_checksum != raw_b64digest: key.set_metadata('mediasync-checksum', raw_b64digest) key.set_contents_from_string(filedata, headers=headers, md5=(hexdigest, b64digest)) # check to see if file should be gzipped based on content_type # also check to see if filesize is greater than 1kb if content_type in TYPES_TO_COMPRESS: key = Key(self._bucket, "%s.gz" % remote_path) filedata = mediasync.compress(filedata) (hexdigest, b64digest) = mediasync.checksum( filedata) # update checksum with compressed data headers[ "Content-Disposition"] = 'inline; filename="%sgz"' % remote_path.split( '/')[-1] headers["Content-Encoding"] = 'gzip' key.set_metadata('mediasync-checksum', raw_b64digest) key.set_contents_from_string(filedata, headers=headers, md5=(hexdigest, b64digest)) return True
def put(self, filedata, content_type, remote_path, force=False): now = datetime.datetime.utcnow() then = now + datetime.timedelta(self.expiration_days) expires = then.strftime("%a, %d %b %Y %H:%M:%S GMT") if self.aws_prefix: remote_path = "%s/%s" % (self.aws_prefix, remote_path) (hexdigest, b64digest) = mediasync.checksum(filedata) raw_b64digest = b64digest # store raw b64digest to add as file metadata # create initial set of headers headers = { "x-amz-acl": "public-read", "Content-Type": content_type, "Expires": expires, "Cache-Control": 'max-age=%d, public' % (self.expiration_days * 24 * 3600), } key = self._bucket.get_key(remote_path) if key is None: key = Key(self._bucket, remote_path) key_meta = key.get_metadata('mediasync-checksum') or '' s3_checksum = key_meta.replace(' ', '+') if force or s3_checksum != raw_b64digest: key.set_metadata('mediasync-checksum', raw_b64digest) key.set_contents_from_string(filedata, headers=headers, md5=(hexdigest, b64digest)) # check to see if file should be gzipped based on content_type # also check to see if filesize is greater than 1kb if content_type in TYPES_TO_COMPRESS: # Use a .gzt extension to avoid issues with Safari on OSX key = Key(self._bucket, "%s.gzt" % remote_path) filedata = mediasync.compress(filedata) (hexdigest, b64digest) = mediasync.checksum(filedata) # update checksum with compressed data headers["Content-Disposition"] = 'inline; filename="%sgzt"' % remote_path.split('/')[-1] headers["Content-Encoding"] = 'gzip' key.set_metadata('mediasync-checksum', raw_b64digest) key.set_contents_from_string(filedata, headers=headers, md5=(hexdigest, b64digest)) return True