From a74afe1ddbffca4f08d4278dd0c90f08a5af210f Mon Sep 17 00:00:00 2001 From: Franklyn Tackitt Date: Thu, 8 Oct 2015 13:36:15 -0700 Subject: [PATCH] Fix headers for uploaded files Boto3 uploads specific headers as parameters to put_object, and others through the Metadata parameter. This commit seperates the metadata and keyword parameter headers, and submits them appropriately --- flask_s3.py | 48 +++++++++++++++++++++++++++++++++++++----------- setup.py | 2 +- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/flask_s3.py b/flask_s3.py index 4dd7d53..67dc89f 100644 --- a/flask_s3.py +++ b/flask_s3.py @@ -1,24 +1,51 @@ -import os -import logging import hashlib import json +import logging +import os import re from collections import defaultdict -import re import boto3 import boto3.exceptions from botocore.exceptions import ClientError -from flask import url_for as flask_url_for from flask import current_app +from flask import url_for as flask_url_for logger = logging.getLogger('flask_s3') - import six +# Mapping for Header names to S3 parameters +header_mapping = { + 'cache-control': 'CacheControl', + 'content-disposition': 'ContentDisposition', + 'content-encoding': 'ContentEncoding', + 'content-language': 'ContentLanguage', + 'content-length': 'ContentLength', + 'content-md5': 'ContentMD5', + 'content-type': 'ContentType', + 'expires': 'Expires', +} + + +def split_metadata_params(headers): + """ + Given a dict of headers for s3, seperates those that are boto3 + parameters and those that must be metadata + """ + + params = {} + metadata = {} + for header_name in headers: + if header_name.lower() in header_mapping: + params[header_mapping[header_name.lower()]] = headers[header_name] + else: + metadata[header_name] = headers[header_name] + return metadata, params + + def merge_two_dicts(x, y): - '''Given two dicts, merge them into a new dict as a shallow copy.''' + """Given two dicts, merge them into a new dict as a shallow copy.""" z = x.copy() z.update(y) return z @@ -153,7 +180,7 @@ def _write_files(s3, app, static_url_loc, static_folder, files, bucket, for file_path in files: asset_loc = _path_to_relative_url(file_path) full_key_name = _static_folder_path(static_url_loc, static_folder_rel, - asset_loc) + asset_loc) key_name = full_key_name.lstrip("/") msg = "Uploading %s to %s as %s" % (file_path, bucket, key_name) logger.debug(msg) @@ -180,14 +207,13 @@ def _write_files(s3, app, static_url_loc, static_folder, files, bucket, h[header] = value with open(file_path) as fp: + metadata, params = split_metadata_params(merge_two_dicts(app.config['S3_HEADERS'], h)) s3.put_object(Bucket=bucket, Key=key_name, Body=fp.read(), ACL="public-read", - Metadata=merge_two_dicts(app.config['S3_HEADERS'], h)) - - - + Metadata=metadata, + **params) return new_hashes diff --git a/setup.py b/setup.py index b1d1245..d4ede4e 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ from setuptools import setup setup( name='Flask-S3', - version='0.2.1', + version='0.2.2', url='http://github.com/e-dard/flask-s3', license='WTFPL', author='Edward Robinson',