Git Product home page Git Product logo

django-storage-swift's Introduction

django-storage-swift: a storage layer for OpenStack Swift

django-storage-swift allows Django applications to use OpenStack Swift as a file storage layer.

Features

  • Reads/writes files into/out of Swift.
  • Automatically derives the correct URL to allow files to be accessed through a web browser based on information returned from the authorisation server.
    • Allows you to override the host, port and path as necessary.
    • Supports the generation of temporary URLs to restrict access to files.

Usage

You can install django-storage-swift through pip. To store your media files on swift, add the following line to your settings.py or local_settings.py:

DEFAULT_FILE_STORAGE='swift.storage.SwiftStorage'

To store your static files on swift, add the following line:

STATICFILES_STORAGE ='swift.storage.StaticSwiftStorage'

This will use another container.

Configuring

django-storage-swift recognises the following options.

Option Default Description
SWIFT_AUTH_URL None The URL for the auth server, e.g. http://127.0.0.1:5000/v2.0
SWIFT_USERNAME None The username to use to authenticate.
SWIFT_KEY None The key (password) to use to authenticate.
SWIFT_AUTH_VERSION 1 The version of the authentication protocol to use.
SWIFT_TENANT_NAME None The tenant name to use when authenticating.
SWIFT_TENANT_ID None The tenant id to use when authenticating.
SWIFT_CONTAINER_NAME None The container in which to store the files.
SWIFT_STATIC_CONTAINER_NAME None Alternate container used by StaticSwiftStorage.
SWIFT_AUTO_CREATE_CONTAINER False Should the container be created if it does not exist?
SWIFT_AUTO_CREATE_CONTAINER_ALLOW_ORIGIN None Set the container's X-Container-Meta-Access-Control-Allow-Origin value, to support CORS requests.
SWIFT_AUTO_BASE_URL True Query the authentication server for the base URL.
SWIFT_BASE_URL None The base URL from which the files can be retrieved, e.g. http://127.0.0.1:8080/.
SWIFT_NAME_PREFIX None Prefix that gets added to all filenames.
SWIFT_USE_TEMP_URLS False Generate temporary URLs for file access (allows files to be accessed without a permissive ACL).
SWIFT_TEMP_URL_KEY None Temporary URL key --- see the OpenStack documentation.
SWIFT_TEMP_URL_DURATION 30*60 How long a temporary URL remains valid, in seconds.
SWIFT_EXTRA_OPTIONS {} Extra options, eg. { "endpoint_type": "adminURL" }, which will return adminURL instead publicURL.
SWIFT_STATIC_BASE_URL None The base URL from which the static files can be retrieved, e.g. http://127.0.0.1:8080/.
SWIFT_STATIC_NAME_PREFIX None Prefix that gets added to all static filenames.

SWIFT_BASE_URL

django-swift-storage will automatically query the authentication server for the URL where your files can be accessed, which takes the form http://server:port/v1/AUTH_token/.

Sometimes you want to override the server and port (for example if you're developing using devstack inside Vagrant). This can be accomplished with SWIFT_BASE_URL.

The provided value is parsed, and:

  • host and port override any automatically derived values
  • any path component is put before derived path components.

So if your auth server returns http://10.0.2.2:8080/v1/AUTH_012345abcd/ and you have SWIFT_BASE_URL="http://127.0.0.1:8888/foo", the url function will a path based on http://127.0.0.1:8888/foo/v1/AUTH_012345abcd/.

Temporary URLs

Temporary URLs provide a means to grant a user permission to access a file for a limited time only and without making the entire container public.

Temporary URLs work as described in the Swift documentation. (The code to generate the signatures is heavily based on their implementation.) They require setup of a key for signing: the process is described in the OpenStack documentation.

Use

Once installed and configured, use of django-storage-swift should be automatic and seamless.

You can verify that swift is indeed being used by running, inside python manage.py shell:

from django.core.files.storage import default_storage
default_storage.connection

The result should be <<swiftclient.client.Connection object ...>>

Openstack Keystone/Identity v3

To authenticate with a swift installation using Keystone AUTH and the Identity v3 API, you must also specify either the domain ID or name that your user and project (tenant) belongs to.

SWIFT_AUTH_URL='https://keystoneserver/v3'
SWIFT_AUTH_VERSION='3'
SWIFT_USERNAME='<<USERNAME>>'
SWIFT_KEY='<<PASSWORD>>'
SWIFT_TENANT_NAME='<<TENANT_NAME>>'
SWIFT_USER_DOMAIN_NAME='<<DOMAIN_NAME>>'
SWIFT_PROJECT_DOMAIN_NAME='<<DOMAIN_NAME>>'

Troubleshooting

  • I'm getting permission errors accessing my files: If you are not using temporary URLs, you may need to make the container publically readable. See this helpful discussion. If you are using temporary URLs, verify that your key is set correctly.

Quickstart

# This was executed on a VM running a SAIO, for example with
# https://github.com/swiftstack/vagrant-swift-all-in-one

# Create two world-readable containers
swift post -r ".r:*" django
swift post -r ".r:*" django-static

# A virtualenv to keep installation separated
virtualenv sampleenv
source sampleenv/bin/activate
pip install django-storage-swift
pip install django

# Create a sample project
django-admin startproject sampleproj
export DJANGO_SETTINGS_MODULE=sampleproj.settings
cd sampleproj/

# A few required settings, using SAIO defaults
cat <<EOF >> sampleproj/settings.py
DEFAULT_FILE_STORAGE='swift.storage.SwiftStorage'
STATICFILES_STORAGE ='swift.storage.StaticSwiftStorage'
SWIFT_AUTH_URL='http://127.0.0.1:8080/auth/v1.0'
SWIFT_USERNAME='test:tester'
SWIFT_KEY='testing'
SWIFT_CONTAINER_NAME='django'
SWIFT_STATIC_CONTAINER_NAME='django-static'
EOF

# Create the initial DB data
python manage.py migrate

# This uploads static files to Swift
python manage.py collectstatic --noinput

# Now open http://127.0.0.1:8000/admin/ in your browser
# Static files like CSS are served by Swift
python manage.py runserver

django-storage-swift's People

Contributors

bkawula avatar daxtens avatar dennisv avatar morpheu avatar nick011 avatar stefan8 avatar stenstad avatar tossmilestone avatar urbaniak avatar vlastv avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.