mirror of
https://github.com/wassname/flask-s3.git
synced 2026-07-18 12:20:30 +08:00
Allow GZIP compression of assets
This commit is contained in:
+13
-2
@@ -3,6 +3,7 @@ import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import zlib
|
||||
from collections import defaultdict
|
||||
|
||||
import boto3
|
||||
@@ -175,6 +176,7 @@ def _static_folder_path(static_url, static_folder, static_asset):
|
||||
def _write_files(s3, app, static_url_loc, static_folder, files, bucket,
|
||||
ex_keys=None, hashes=None):
|
||||
""" Writes all the files inside a static folder to S3. """
|
||||
should_gzip = app.config.get('S3_GZIP')
|
||||
new_hashes = []
|
||||
static_folder_rel = _path_to_relative_url(static_folder)
|
||||
for file_path in files:
|
||||
@@ -206,11 +208,19 @@ def _write_files(s3, app, static_url_loc, static_folder, files, bucket,
|
||||
for header, value in headers.iteritems():
|
||||
h[header] = value
|
||||
|
||||
if should_gzip:
|
||||
h["content-encoding"] = "gzip"
|
||||
|
||||
with open(file_path) as fp:
|
||||
metadata, params = split_metadata_params(merge_two_dicts(app.config['S3_HEADERS'], h))
|
||||
if should_gzip:
|
||||
data = zlib.compress(fp.read())
|
||||
else:
|
||||
data = fp.read()
|
||||
|
||||
s3.put_object(Bucket=bucket,
|
||||
Key=key_name,
|
||||
Body=fp.read(),
|
||||
Body=data,
|
||||
ACL="public-read",
|
||||
Metadata=metadata,
|
||||
**params)
|
||||
@@ -370,7 +380,8 @@ class FlaskS3(object):
|
||||
('S3_HEADERS', {}),
|
||||
('S3_FILEPATH_HEADERS', {}),
|
||||
('S3_ONLY_MODIFIED', False),
|
||||
('S3_URL_STYLE', 'host')]
|
||||
('S3_URL_STYLE', 'host'),
|
||||
('S3_GZIP', False)]
|
||||
|
||||
for k, v in defaults:
|
||||
app.config.setdefault(k, v)
|
||||
|
||||
Reference in New Issue
Block a user