Usage

S3

Pre-signed URLs

The link_to function for the S3 backend creates a temporary, pre-signed URL that can be used for uploads or downloads.

Uploads

  • PUT request required
  • Must have a header of content-type: application/octet-stream set
    • If header doesn’t match the expected value, you will get a 400 error
  • Make sure you have permissions to the key you are creating
    • The SDK will happily generate pre-signed URLs that are not available to the generating user
  • Body is file contents

JavaScript example:

const resp = await axios.default.put(storageUrl, file, {
    headers: { "content-type": "application/octet-stream" },
});

StorageOperations wrapper/mixin

class keg_storage.StorageOperations[source]

Ops wrapper for storage operations that will typically occur in a flask app.

Assumes the storage plugin is being used and configured with storage profiles.

Class properties storage_location and storage_profile may be assigned defaults in a subclass direct any of the operations to that folder path or configured interface. storage_location is expected to be an Enum.

Each method will also take storage_location and storage_profile, so they can be provided directly for one-offs. So, this class can be used directly or as a mixin.

classmethod storage_delete_file(filename, storage_location=None, storage_profile=None)[source]

Remove file data from storage.

classmethod storage_download_file(filename, storage_location=None, storage_profile=None)[source]

Pull file data from storage, return BytesIO stream.

classmethod storage_duplicate_file(filename, storage_location=None, storage_profile=None)[source]

Copy file data already in storage to a new file object. Generates the new filename using a UUID.

static storage_generate_filename(filename)[source]

Generate a UUID-based filename for an object, typically for upload to prevent path collisions. If the provided original filename has an extension, honor that extension.

Generate an expiring download link to pass to client for a stored object.

classmethod storage_get_profile(storage_profile=None)[source]

Get configured storage interface. Either specify which interface via the storage_profile kwarg, or it will fall back to the first defined profile.

Generate an expiring upload link to pass to client for data to be stored.

static storage_prefix_path(location, filename)[source]

Join the location path with the filename to get the full object path

classmethod storage_upload_file(file_object, filename, preserve_filename=False, storage_location=None, storage_profile=None)[source]

Push file data to storage. A UUID-based filename will be generated to prevent path collisions unless preserve_filename is set.

classmethod storage_upload_form_file(form_field: str, storage_location=None, storage_profile=None)[source]

Shortcut to push file data from posted form to storage.