Working with your project’s media storage in Python applications¶

Introduction¶

Default file storage on Divio Cloud projects is handled by dedicated storage systems entirelyseparate from the application.

In our architecture, the same site may be running as several different instances, on severaldifferent application hosts (this is one reason why Divio Cloud projects can be scaled, because newapplication instances can be created to meet increasing demand).

Although each of those instances will have its own local file storage, this will be independent ofeach of the others, and it won’t persist - once that instance ceases to exist, so will the files.That storage will also be inaccessible to any other instances of the application.

This means a project applications can’t expect to save files to its local storage, and then expectto find them again.

Our storage service providers¶

Instead, the applications must use our storage services. These are Amazon Web Services’s S3service, or a generic S3 hosting service via another provider.Currently, most projects use Amazon’s own S3 service, with the exception of projects in our Swissregion.

Working with our storage backends in Django¶

For most Django applications, this won’t require any additional work. Django is able to usemultiple storage backends, all addressed through a common API. This is the safe and correct way tohandle files in Django, so that applications can abstract from details of the storageimplementation, and simply not need to know about it.

As long as an application uses Django’s storage API, rather than attempting to manipulate PythonFile objects directly, it doesn’t need to do anything differently.

Similarly, an application should not rely on knowing or manipulating a File object’s file path.

Use DEFAULT_FILE_STORAGE¶

Django’s file storage system relies on the DEFAULT_FILE_STORAGE setting. Ideally, third-partyapplications in your project should respect this for their own file handling.

This is not always the case however. In some cases the application may need to be configuredexplicitly. More problematically, some applications may have hard-coded expectations for the filestorage system, and these will need to be rewritten.

Using Easy Thumbnails¶

Easy Thumbnails is the most widely-used image processing application in the Django ecosystem.

On Divio Cloud, THUMBNAIL_DEFAULT_STORAGE for Easy Thumbnails needs to be set explicitly, evenif DEFAULT_FILE_STORAGE has been set.

In most projects on Divio Cloud, Django Filer is installed. This takes care of theTHUMBNAIL_DEFAULT_STORAGE - if Django Filer is installed, you don’t need to do anything else touse Easy Thumbnails correctly.

In the cases where it’s not, it’s necessary to do the same thing manually in the settings.py:

  1. # If the DEFAULT_FILE_STORAGE has been set to a value known by
  2. # aldryn-django, then use that as THUMBNAIL_DEFAULT_STORAGE as well.
  3.  
  4. from aldryn_django import storage
  5.  
  6. for storage_backend in storage.SCHEMES.values():
  7. if storage_backend == DEFAULT_FILE_STORAGE:
  8. THUMBNAIL_DEFAULT_STORAGE = storage_backend
  9. break

Loading media files into your applications’ pages¶

Sometimes an application in your project will need to load media files using JavaScript.

Since your media files are held on a server under a different domain from the application,browsers may refuse to allow this cross-domain loading for security reasons.

There are two solutions to this.

Load media from static¶

One is to make sure that all files you need to load are in your site’s static files,rather than media. (The static files are served from the same domain as the application itself, sobrowsers will be able to load files using JavaScript without complaint).

This has the advantage of not running into the possibility of using JavaScript to loaduser-submitted material (which could include material uploaded by untrusted users).

Enable CORS headers¶

The other solution to enable CORS (“Cross-origin resource sharing”) headers on the media.

This must be done by our infrastructure team, on a per-website basis. Please contact Divio support to request this.

Storage speed and performance¶

Note that if you need to make many read/write operations to file storage, or are working with verylarge objects, that the speed you experience on the cloud can be considerably less than what youexperience in the local development environment.

The local development environment has the advantage of locally-attached storage, and should notnecessarily be taken as a guide to performance on the cloud.

In most cases, this won’t actually matter. However, if your code works very intensively withstorage, it can be more efficient and faster to do all that work on the application instance’s ownlocal filesystem, in a temporary directory, and then send the finished work to the remote storage.

原文: http://docs.divio.com/en/latest/reference/work-media-storage.html