mirror of
https://github.com/wassname/flask-s3.git
synced 2026-07-09 00:20:11 +08:00
Add support for Cache-Control header
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
Contributors
|
||||
============
|
||||
|
||||
* Edward Robinson (e-dard)
|
||||
* Rehan Dalal (rehandalal)
|
||||
+49
-22
@@ -2,23 +2,36 @@ Flask-S3
|
||||
********
|
||||
.. module:: flask_s3
|
||||
|
||||
Flask-S3 allows you to easily serve all your `Flask`_ application's static assets from `Amazon S3`_, without having to modify your templates.
|
||||
Flask-S3 allows you to easily serve all your `Flask`_ application's static
|
||||
assets from `Amazon S3`_, without having to modify your templates.
|
||||
|
||||
.. _Amazon S3: http://aws.amazon.com/s3
|
||||
.. _Flask: http://flask.pocoo.org/
|
||||
|
||||
|
||||
How it works
|
||||
============
|
||||
|
||||
Flask-S3 has two main functions:
|
||||
|
||||
1. Walk through your application's static folders, gather all your static assets together, and upload them to a bucket of your choice on S3;
|
||||
1. Walk through your application's static folders, gather all your static
|
||||
assets together, and upload them to a bucket of your choice on S3;
|
||||
|
||||
2. Replace the URLs that Flask's :func:`flask.url_for` function would insert into your templates, with URLs that point to the static assets in your S3 bucket.
|
||||
2. Replace the URLs that Flask's :func:`flask.url_for` function would insert
|
||||
into your templates, with URLs that point to the static assets in your S3
|
||||
bucket.
|
||||
|
||||
The process of gathering and uploading your static assets to S3 need only be done once, and your application does not need to be running for it to work. The location of the S3 bucket can be inferred from Flask-S3 `settings`_ specified in your Flask application, therefore when your application is running there need not be any communication between the Flask application and Amazon S3.
|
||||
The process of gathering and uploading your static assets to S3 need only be
|
||||
done once, and your application does not need to be running for it to work. The
|
||||
location of the S3 bucket can be inferred from Flask-S3 `settings`_ specified in
|
||||
your Flask application, therefore when your application is running there need
|
||||
not be any communication between the Flask application and Amazon S3.
|
||||
|
||||
Internally, every time ``url_for`` is called in one of your application's templates, `flask_s3.url_for` is instead invoked. If the endpoint provided is deemed to refer to static assets, then the S3 URL for the asset specified in the `filename` argument is instead returned. Otherwise, `flask_s3.url_for` passes the call on to `flask.url_for`.
|
||||
Internally, every time ``url_for`` is called in one of your application's
|
||||
templates, `flask_s3.url_for` is instead invoked. If the endpoint provided is
|
||||
deemed to refer to static assets, then the S3 URL for the asset specified in the
|
||||
`filename` argument is instead returned. Otherwise, `flask_s3.url_for` passes
|
||||
the call on to `flask.url_for`.
|
||||
|
||||
|
||||
Installation
|
||||
@@ -46,6 +59,7 @@ only supports applications that use the `jinja2`_ templating system.
|
||||
.. _boto: http://docs.pythonboto.org/en/latest/
|
||||
.. _jinja2: http://jinja.pocoo.org/docs/
|
||||
|
||||
|
||||
Using Flask-S3
|
||||
==============
|
||||
|
||||
@@ -64,8 +78,8 @@ about your :class:`flask.Flask` application object.
|
||||
|
||||
In many cases, however, one cannot expect a Flask instance to be ready at
|
||||
import time, and a common pattern is to return a Flask instance from within a
|
||||
function only after other configuration details have been taken care of. In these
|
||||
cases, Flask-S3 provides a simple function, ``init_app``, which takes your
|
||||
function only after other configuration details have been taken care of. In
|
||||
these cases, Flask-S3 provides a simple function, ``init_app``, which takes your
|
||||
application as an argument.
|
||||
|
||||
.. code-block:: python
|
||||
@@ -81,12 +95,17 @@ application as an argument.
|
||||
return app
|
||||
|
||||
In terms of getting your application to use external Amazon S3 URLs when
|
||||
referring to your application's static assets, passing your ``Flask`` object to the ``FlaskS3`` object is all that needs to be done. Once your app is running, any templates that contained relative static asset locations, will instead contain hosted counterparts on Amazon S3.
|
||||
referring to your application's static assets, passing your ``Flask`` object to
|
||||
the ``FlaskS3`` object is all that needs to be done. Once your app is running,
|
||||
any templates that contained relative static asset locations, will instead
|
||||
contain hosted counterparts on Amazon S3.
|
||||
|
||||
Uploading your Static Assets
|
||||
----------------------------
|
||||
|
||||
You only need to upload your static assets to Amazon S3 once. Of course, if you add or modify your existing assets then you will need to repeat the uploading process.
|
||||
You only need to upload your static assets to Amazon S3 once. Of course, if you
|
||||
add or modify your existing assets then you will need to repeat the uploading
|
||||
process.
|
||||
|
||||
Uploading your static assets from a Python console is as simple as follows.
|
||||
|
||||
@@ -106,7 +125,9 @@ Amazon S3 bucket.
|
||||
Static Asset URLs
|
||||
~~~~~~~~~~~~~~~~~
|
||||
|
||||
Within your bucket on S3, Flask-S3 replicates the static file hierarchy defined in your application object and any registered blueprints. URLs generated by Flask-S3 will look like the following:
|
||||
Within your bucket on S3, Flask-S3 replicates the static file hierarchy defined
|
||||
in your application object and any registered blueprints. URLs generated by
|
||||
Flask-S3 will look like the following:
|
||||
|
||||
``/static/foo/style.css`` becomes
|
||||
``https://mybucketname.s3.amazonaws.com/static/foo/style.css``,
|
||||
@@ -136,27 +157,39 @@ not present, some will need to be provided when uploading assets to S3.
|
||||
**Default:** ``u's3.amazonaws.com'``
|
||||
`S3_BUCKET_NAME` The desired name for your Amazon S3 bucket. Note:
|
||||
the name will be visible in all your assets' URLs.
|
||||
`S3_USE_HTTPS` Specifies whether or not to serve your assets
|
||||
`S3_CACHE_CONTROL` This sets the value of the Cache-Control header that
|
||||
is set in the metadata when `S3_USE_CACHE_CONTRL` is
|
||||
set to `True`.
|
||||
`S3_USE_CACHE_CONTROL` Specifies whether or not to set the metadata for the
|
||||
Cache-Control headers.
|
||||
**Default:** `False`
|
||||
`S3_USE_HTTPS` Specifies whether or not to serve your assets
|
||||
stored in S3 over HTTPS.
|
||||
**Default:** `True`
|
||||
`USE_S3` This setting allows you to toggle whether Flask-S3
|
||||
is active or not. When set to `False` your
|
||||
application's templates will revert to including static asset locations determined by `flask.url_for`.
|
||||
application's templates will revert to including
|
||||
static asset locations determined by
|
||||
`flask.url_for`.
|
||||
**Default:** `True`
|
||||
`USE_S3_DEBUG` By default, Flask-S3 will be switched off when
|
||||
running your application in `debug`_ mode, so that
|
||||
your templates include static asset locations specified by `flask.url_for`. If you wish to enable Flask-S3 in debug mode, set this value to `True`. **Note**: if `USE_S3` is set to `False` then templates will always include asset locations specified by `flask.url_for`.
|
||||
your templates include static asset locations
|
||||
specified by `flask.url_for`. If you wish to enable
|
||||
Flask-S3 in debug mode, set this value to `True`.
|
||||
**Note**: if `USE_S3` is set to `False` then
|
||||
templates will always include asset locations
|
||||
specified by `flask.url_for`.
|
||||
=========================== ===================================================
|
||||
|
||||
.. _debug: http://flask.pocoo.org/docs/config/#configuration-basics
|
||||
|
||||
|
||||
|
||||
|
||||
API Documentation
|
||||
=================
|
||||
|
||||
Flask-S3 is a very simple extension. The few exposed objects, methods and functions are as follows.
|
||||
Flask-S3 is a very simple extension. The few exposed objects, methods and
|
||||
functions are as follows.
|
||||
|
||||
The FlaskS3 Object
|
||||
------------------
|
||||
@@ -164,14 +197,8 @@ The FlaskS3 Object
|
||||
|
||||
.. automethod:: init_app
|
||||
|
||||
|
||||
S3 Interaction
|
||||
--------------
|
||||
.. autofunction:: create_all
|
||||
|
||||
.. autofunction:: url_for
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
+8
-1
@@ -94,7 +94,13 @@ def _write_files(static_url_loc, static_folder, files, bucket, ex_keys=None):
|
||||
if ex_keys and key_name in ex_keys:
|
||||
logger.debug("%s excluded from upload" % key_name)
|
||||
else:
|
||||
app = current_app
|
||||
k = Key(bucket=bucket, name=key_name)
|
||||
if (app.config['S3_USE_CACHE_CONTROL']
|
||||
and 'S3_CACHE_CONTROL' in app.config):
|
||||
k.metadata.update({
|
||||
'Cache-Control': app.config['S3_CACHE_CONTROL']
|
||||
})
|
||||
k.set_contents_from_filename(file_path)
|
||||
k.make_public()
|
||||
|
||||
@@ -186,7 +192,8 @@ class FlaskS3(object):
|
||||
defaults = [('S3_USE_HTTPS', True),
|
||||
('USE_S3', True),
|
||||
('USE_S3_DEBUG', False),
|
||||
('S3_BUCKET_DOMAIN', 's3.amazonaws.com')]
|
||||
('S3_BUCKET_DOMAIN', 's3.amazonaws.com'),
|
||||
('S3_USE_CACHE_CONTROL', False)]
|
||||
for k, v in defaults:
|
||||
app.config.setdefault(k, v)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user