API Reference

This part of the documentation covers all the public classes in Depot.

Application Support

class depot.manager.DepotManager

Takes care of managing the whole Depot environment for the application.

DepotManager tracks the created depots, the current default depot, and the WSGI middleware in charge of serving files for local depots.

While this is used to create the default depot used by the application it can also create additional depots using the new() method.

In case you need to migrate your application to a different storage while keeping compatibility with previously stored file simply change the default depot through set_default() all previously stored file will continue to work on the old depot while new files will be uploaded to the new default one.

classmethod configure(name, config, prefix='depot.')

Configures an application depot.

This configures the application wide depot from a settings dictionary. The settings dictionary is usually loaded from an application configuration file where all the depot options are specified with a given prefix.

The default prefix is depot., the minimum required setting is depot.backend which specified the required backend for files storage. Additional options depend on the choosen backend.

classmethod from_config(config, prefix='depot.')

Creates a new depot from a settings dictionary.

Behaves like the configure() method but instead of configuring the application depot it creates a new one each time.

classmethod get(name=None)

Gets the application wide depot instance.

Might return None if configure() has not been called yet.

classmethod get_default()

Retrieves the current application default depot

classmethod get_file(path)

Retrieves a file by storage name and fileid in the form of a path

Path is expected to be storage_name/fileid.

classmethod make_middleware(app, **options)

Creates the application WSGI middleware in charge of serving local files.

A Depot middleware is required if your application wants to serve files from storages that don’t directly provide and HTTP interface like depot.io.local.LocalFileStorage and depot.io.gridfs.GridFSStorage

classmethod set_default(name)

Replaces the current application default depot

classmethod url_for(path)

Given path of a file uploaded on depot returns the url that serves it

Path is expected to be storage_name/fileid.

class depot.middleware.DepotMiddleware(app, mountpoint='/depot', cache_max_age=604800, replace_wsgi_filewrapper=False)

WSGI Middleware in charge of serving Depot files.

Usually created using depot.manager.DepotManager.make_middleware(), it’s a WSGI middleware that serves files stored inside depots that do not provide a public HTTP url. For depot that provide a public url the request is redirected to the public url.

In case you have issues serving files with your WSGI server your can try to set replace_wsgi_filewrapper=True which forces DEPOT to use its own internal FileWrapper instead of the one provided by your WSGI server.

Database Support

class depot.fields.sqlalchemy.UploadedFileField(filters=(), upload_type=<class 'depot.fields.upload.UploadedFile'>, *args, **kw)

Provides support for storing attachments to SQLAlchemy models.

UploadedFileField can be used as a Column type to store files into the model. The actuall file itself will be uploaded to the default Depot, and only the depot.fields.upload.UploadedFile informations will be stored on the database.

The UploadedFileField is transaction aware, so it will delete every uploaded file whenever the transaction is rolled back and will delete any old file whenever the transaction is committed. This is the reason you should never associate the same depot.fields.upload.UploadedFile to two different UploadedFileField, otherwise you might delete a file already used by another model. It is usually best to just set the file or bytes as content of the column and let the UploadedFileField create the depot.fields.upload.UploadedFile by itself whenever it’s content is set.

impl

alias of Unicode

class depot.fields.ming.UploadedFileProperty(filters=(), upload_type=<class 'depot.fields.upload.UploadedFile'>)
class depot.fields.interfaces.DepotFileInfo(content, depot_name=None)

Keeps informations on a content related to a specific depot.

By itself the DepotFileInfo does nothing, it is required to implement a process_content() method that actually saves inside the file info the information related to the content. The only information which is saved by default is the depot name itself.

It is a specialized dictionary that provides also attribute style access, the dictionary parent permits easy encoding/decoding to most marshalling systems.

process_content(content, filename=None, content_type=None)

Process content in the given depot.

This is implemented by subclasses to provide some kind of behaviour on the content in the related Depot. The default implementation is provided by depot.fields.upload.UploadedFile which stores the content into the depot.

class depot.fields.interfaces.FileFilter

Interface that must be implemented by file filters.

File filters get executed whenever a file is stored on the database using one of the supported fields. Can be used to add additional data to the stored file or change it. When file filters are run the file has already been stored.

on_save(uploaded_file)

Filters are required to provide their own implementation

class depot.fields.upload.UploadedFile(content, depot_name=None)

Simple depot.fields.interfaces.DepotFileInfo implementation that stores files.

Takes a file as content and uploads it to the depot while saving around most file informations. Pay attention that if the file gets replaced through the depot the UploadedFile will continue to have the old data.

Also provides support for encoding/decoding using JSON for storage inside databases as a plain string.

Default attributes provided for all UploadedFile include:
  • filename - This is the name of the uploaded file

  • file_id - This is the ID of the uploaded file

  • path - This is a depot_name/file_id path which can

    be used with DepotManager.get_file() to retrieve the file

  • content_type - This is the content type of the uploaded file

  • uploaded_at - This is the upload date in YYYY-MM-DD HH:MM:SS format

  • url - Public url of the uploaded file

  • file - The depot.io.interfaces.StoredFile instance of the stored file

process_content(content, filename=None, content_type=None)

Standard implementation of DepotFileInfo.process_content()

This is the standard depot implementation of files upload, it will store the file on the default depot and will provide the standard attributes.

Subclasses will need to call this method to ensure the standard set of attributes is provided.

Storing Files

class depot.io.interfaces.StoredFile(file_id, filename=None, content_type=None, last_modified=None, content_length=None)

Interface for already saved files.

It provides metadata on the stored file through following properties:
  • file_id
  • filename
  • content_type
  • last_modified
  • content_length

Already stored files can only be read back, so they are required to only provide read(self, n=-1), close() methods and closed property so that they can be read.

To replace/overwrite a file content do not try to call the write method, instead use the storage backend to replace the file content.

close(*args, **kwargs)

Closes the file.

After closing the file it won’t be possible to read from it anymore. Some implementation might not do anything when closing the file, but they still are required to prevent further reads from a closed file.

closed

Returns if the file has been closed.

When closed return True it won’t be possible to read anoymore from this file.

fileno()

Returns underlying file descriptor if one exists.

An IOError is raised if the IO object does not use a file descriptor.

flush()

Flush write buffers, if applicable.

This is not implemented for read-only and non-blocking streams.

isatty()

Return whether this is an ‘interactive’ stream.

Return False if it can’t be determined.

name

This is the filename of the saved file

If a filename was not available when the file was created this will return “unknown” as filename.

next
public_url

The public HTTP url from which file can be accessed.

When supported by the storage this will provide the public url to which the file content can be accessed. In case this returns None it means that the file can only be served by the DepotMiddleware itself.

read(n=-1)

Reads n bytes from the file.

If n is not specified or is -1 the whole file content is read in memory and returned

readable()

Returns if the stored file is readable or not

Usually all stored files are readable

readline()

Read and return a line from the stream.

If limit is specified, at most limit bytes will be read.

The line terminator is always b’n’ for binary files; for text files, the newlines argument to open can be used to select the line terminator(s) recognized.

readlines()

Return a list of lines from the stream.

hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds hint.

seek()

Change stream position.

Change the stream position to the given byte offset. The offset is interpreted relative to the position indicated by whence. Values for whence are:

  • 0 – start of stream (the default); offset should be zero or positive
  • 1 – current stream position; offset may be negative
  • 2 – end of stream; offset is usually negative

Return the new absolute position.

seekable()

Returns if the stored file is seekable or not

By default stored files are not seekable

tell()

Return current stream position.

truncate()

Truncate file to size bytes.

File pointer is left unchanged. Size defaults to the current IO position as reported by tell(). Returns the new size.

writable()

Returns if the stored file is writable or not

Stored files are not writable, you should rely on the relative FileStorage to overwrite their content

class depot.io.interfaces.FileStorage

Interface for storage providers.

The FileStorage base class declares a standard interface for storing and retrieving files in an underlying storage system.

Each storage system implementation is required to provide this interface to correctly work with filedepot.

create(content, filename=None, content_type=None)

Saves a new file and returns the ID of the newly created file.

content parameter can either be bytes, another file object or a cgi.FieldStorage. When filename and content_type parameters are not provided they are deducted from the content itself.

delete(file_or_id)

Deletes a file. If the file didn’t exist it will just do nothing.

exists(file_or_id)

Returns if a file or its ID still exist.

static fileid(file_or_id)

Gets the ID of a given StoredFile

If the given parameter is already a StoredFile id it will directly return it.

static fileinfo(fileobj, filename=None, content_type=None)

Tries to extract from the given input the actual file object, filename and content_type

This is used by the create and replace methods to correctly deduce their parameters from the available informations when possible.

get(file_or_id)

Opens the file given by its unique id.

This operation is guaranteed to return a StoredFile instance or should raise IOError if the file is not found.

replace(file_or_id, content, filename=None, content_type=None)

Replaces an existing file, an IOError is raised if the file didn’t already exist.

Given a StoredFile or its ID it will replace the current content with the provided content value. If filename and content_type are provided or can be deducted by the content itself they will also replace the previous values, otherwise the current values are kept.

class depot.io.local.LocalFileStorage(storage_path)

depot.io.interfaces.FileStorage implementation that stores files locally.

All the files are stored inside a directory specified by the storage_path parameter.

class depot.io.gridfs.GridFSStorage(mongouri, collection='filedepot')

depot.io.interfaces.FileStorage implementation that stores files on MongoDB.

All the files are stored using GridFS to the database pointed by the mongouri parameter into the collection named collection.

class depot.io.awss3.S3Storage(access_key_id, secret_access_key, bucket=None)

depot.io.interfaces.FileStorage implementation that stores files on S3.

All the files are stored inside a bucket named bucket to which Depot connects to using access_key_id and secret_access_key.

Utilities