Welcome to Flask-GoogleStorage¶
Flask-GoogleStorage is a Flask extension for adding storage capabilities using Google Cloud Storage.
Install¶
Flask-GoogleStorage requires Python >= 3.6.
$ pip install flask-googlestorage
Usage¶
In order to authenticate with Google Cloud Storage, set the GOOGLE_APPLICATION_CREDENTIALS
environment variable to the full path to your service account private key file (see google-auth
documentation for more details). If the authentication fails, Flask-GoogleStorage will storage
and serve uploaded files locally and relative to the configuration variable
GOOGLE_STORAGE_LOCAL_DEST
. Note that this configuration variable is required even in the case of
successfull authentication as Flask-GoogleStorage will use such destination as a temporary
storage.
First create your app as usual:
from datetime import timedelta
from flask import Flask
from flask_googlestorage import GoogleStorage, Bucket
files = Bucket("files")
storage = GoogleStorage(files)
def create_app():
app = Flask(__name__)
app.config.update(
GOOGLE_STORAGE_LOCAL_DEST = app.instance_path,
GOOGLE_STORAGE_SIGNATURE = {"expiration": timedelta(minutes=5)},
GOOGLE_STORAGE_FILES_BUCKET = "files-bucket-id"
)
storage.init_app(app)
return app
Then you can save werkzeug.datastructures.FileStorage
objects using:
filename = files.save(file_storage) # add public=True to make it publicly available
The URL for a given filename can be obtained using:
files.url(filename) # returns the public url
files.signed_url(filename) # returns a signed url valid for 5 minutes
Configuration¶
The following configuration values are available. Flask-GoogleStorage loads these values from your main Flask config which can be populated in various ways.
Configuration Keys¶
A list of configuration keys currently understood by the extension:
|
Local path for either temporary or local storage. |
|
A dictionary specifying the keyword arguments for building the signed url. |
|
A dictionary specifying the keyword arguments
for the |
|
If set to True, Flask-GoogleStorage will resolve name conflicts. Otherwise, existing files will be overwritten. |
|
If set to True, uploaded files are deleted after uploading to Google Cloud Storage (successfully or not). |
|
Overwrite |
|
Overwrite |
|
Overwrite |
|
Overwrite |
|
Bucket id for bucket |
To restrict uploaded files content length you should use the Flask configuration variable
MAX_CONTENT_LENGTH
. See the Configuration Handling section in Flask documentation for more details.