Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

OpenStack Swift storage backend for Django

License

NotificationsYou must be signed in to change notification settings

dennisv/django-storage-swift

Repository files navigation

https://travis-ci.org/dennisv/django-storage-swift.svg?branch=master

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

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

Features

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

Usage

You can install django-storage-swift through pip. To store your mediafiles on swift, add the following line to your settings.py orlocal_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.

OptionDefaultDescription
SWIFT_AUTH_URLRequiredThe URL for the auth server, e.g.http://127.0.0.1:5000/v2.0
SWIFT_USERNAMERequiredThe username to use to authenticate.
SWIFT_KEY/SWIFT_PASSWORDRequiredThe key (password) to use to authenticate.
SWIFT_AUTH_VERSIONNoneThe version of the authentication protocol to use. If no auth version is defined, a version will be guessed based on auth parameters.
SWIFT_TENANT_NAME/SWIFT_PROJECT_NAMENone(v2 and v3 auth) The tenant/project name to use when authenticating.
SWIFT_TENANT_ID/SWIFT_PROJECT_IDNone(v2 and v3 auth) The tenant/project id to use when authenticating.
SWIFT_USER_DOMAIN_NAMENone(v3 auth only) The domain name we authenticate to
SWIFT_USER_DOMAIN_IDNone(v3 auth only) The domain id we authenticate to
SWIFT_PROJECT_DOMAIN_NAMENone(v3 auth only) The domain name our project is located in
SWIFT_PROJECT_DOMAIN_IDNone(v3 auth only) The domain id our project is located in
SWIFT_REGION_NAMENoneOpenStack region if needed. Check with your provider.
SWIFT_CONTAINER_NAMENoneThe container in which to store the files. (DEFAULT_FILE_STORAGE)
SWIFT_STATIC_CONTAINER_NAMENoneAlternate container for storing staticfiles. (STATICFILES_STORAGE)
SWIFT_AUTO_CREATE_CONTAINERFalseShould the container be created if it does not exist?
SWIFT_AUTO_CREATE_CONTAINER_PUBLICFalseSet the auto created container as public on creation
SWIFT_AUTO_CREATE_CONTAINER_ALLOW_ORIGINNoneSet the container's X-Container-Meta-Access-Control-Allow-Origin value, to support CORS requests.
SWIFT_AUTO_BASE_URLTrueQuery the authentication server for the base URL.
SWIFT_BASE_URLNoneThe base URL from which the files can be retrieved, e.g.http://127.0.0.1:8080/.
SWIFT_NAME_PREFIXNonePrefix that gets added to all filenames.
SWIFT_USE_TEMP_URLSFalseGenerate temporary URLs for file access (allows files to be accessed without a permissive ACL).
SWIFT_TEMP_URL_KEYNoneTemporary URL key --- seethe OpenStack documentation.
SWIFT_TEMP_URL_DURATION30*60How 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_AUTO_BASE_URLTrueQuery the authentication server for the static base URL.
SWIFT_STATIC_BASE_URLNoneThe base URL from which the static files can be retrieved, e.g.http://127.0.0.1:8080/.
SWIFT_STATIC_NAME_PREFIXNonePrefix that gets added to all static filenames.
SWIFT_CONTENT_TYPE_FROM_FDFalseDetermine the files mimetypes from the actual content rather than from their filename (default).
SWIFT_FULL_LISTINGTrueEnsures to get whole directory contents (by default swiftclient limits it to 10000 entries)
SWIFT_AUTH_TOKEN_DURATION60*60*23How long a token is expected to be valid in seconds.
SWIFT_LAZY_CONNECTFalseIfTrue swift connection will be obtained on first use, ifFalse it will be obtained during storage instantiation. This can decreasestartup time if you use many fields that use non-default swift storage.
SWIFT_GZIP_CONTENT_TYPES[]List of content type that will be compressed eg. ['text/plain', 'application/json']
SWIFT_GZIP_COMPRESSION_LEVEL4Gzip compression level from 0 to 9. 0 = no compression, 9 = max compression
SWIFT_GZIP_UNKNOWN_CONTENT_TYPEFalseIf set to True and the content-type can't be guessed, gzip anyway
SWIFT_CACHE_HEADERSFalseHeaders cache on/off switcher

SWIFT_BASE_URL

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

Sometimes you want to override the server and port (for example ifyou're developing usingdevstack insideVagrant). This can be accomplished withSWIFT_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 returnshttp://10.0.2.2:8080/v1/AUTH_012345abcd/ and you haveSWIFT_BASE_URL="http://127.0.0.1:8888/foo", theurl functionwill a path based onhttp://127.0.0.1:8888/foo/v1/AUTH_012345abcd/.

Temporary URLs

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

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

Use

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

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

fromdjango.core.files.storageimportdefault_storagedefault_storage.http_conn

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 notusing temporary URLs, you may need to make the container publicallyreadable. Seethis helpfuldiscussion.If you are using temporary URLs, verify that your key is setcorrectly.
  • I'm getting empty or truncated file uploads: Issues with some contenttypes may cause an incorrect content_length header to be sent with fileuploads, resulting in 0 byte or truncated files. To avoid this, setSWIFT_CONTENT_LENGTH_FROM_FD: True.

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 containersswiftpost-r".r:*"djangoswiftpost-r".r:*"django-static# A virtualenv to keep installation separatedvirtualenvsampleenvsourcesampleenv/bin/activatepipinstalldjango-storage-swiftpipinstalldjango# Create a sample projectdjango-adminstartprojectsampleprojexportDJANGO_SETTINGS_MODULE=sampleproj.settingscdsampleproj/# A few required settings, using SAIO defaultscat<<EOF>>sampleproj/settings.pyDEFAULT_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 datapythonmanage.pymigrate# This uploads static files to Swiftpythonmanage.pycollectstatic--noinput# Now open http://127.0.0.1:8000/admin/ in your browser# Static files like CSS are served by Swiftpythonmanage.pyrunserver

About

OpenStack Swift storage backend for Django

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors32

Languages


[8]ページ先頭

©2009-2025 Movatter.jp