apicrud.media.storage

storage.py

Storage access

Files, photos and videos are stored as objects in a cloud vendor’s storage service (Amazon S3 or equivalent). The backend server here acts as a broker for authenticating uploads and storing/retrieving metadata descriptors for each object. Metadata is stored in a redis cache.

created 25-jan-2020 by richb@instantlinux.net

Classes

StorageAPI([credential_ttl, redis_conn, …])

Storage API

StorageBackblaze(model[, db_session])

Vendor-specific storage interface: Backblaze B2.

StorageS3([credential_ttl, credentials, …])

Vendor-specific storage interface: Amazon S3

class apicrud.media.storage.StorageAPI(credential_ttl=None, redis_conn=None, uid=None, db_session=None)

Storage API

credential_ttl

how long until temp credential expires

Type

int

redis_conn

connection to redis

Type

obj

uid

User ID

Type

str

db_session

existing db session

Type

obj

del_file_meta(file_id, meta)

Remove a metadata item from cache

Parameters
  • file_id (str) – ID in file model

  • meta (dict) – keys to update

del_object(storage_id, storage_key)

Remove a file from a storage bucket

Parameters
  • storage_id (str) – ID in storage model

  • storage_key (str) – pathname within storage bucket

fetch_album_meta(album_id, thumbnail_height)

Retrieve metadata of a media album in format suitable for react-image-gallery

Parameters
  • album_id – ID in album model

  • thumbnail_height (int) – which scaled image to choose

Returns

a set of image descriptors suitable for display

Return type

dict

get_file_meta(file_id)

Retrieve metadata from redis cache

Parameters

file_id (str) – ID in file model

Returns

fid, uid, sid, pid, path, name

Return type

dict

get_object(storage_id, storage_key)

Get object from bucket

Parameters
  • storage_id (str) – ID in storage model

  • storage_key (str) – pathname within storage bucket

get_upload_url(body)

Get a new pre-authorized URL from the storage vendor which allows the end user to upload one object to storage

Parameters

body (dict) – storage_id key provides ID in storage model; other keys provide metadata about the object to be uploaded, which will be stored in the new object’s entry in database upon upload completion

put_object(storage_id, storage_key, content, content_type='image/jpeg')

Send a file to the storage bucket

Parameters
  • storage_id (str) – ID in storage model

  • storage_key (str) – pathname within storage bucket

  • content (bytes) – file content

  • content_type (str) – mime type

update_file_meta(file_id, meta)

Update metadata in redis cache

Parameters
  • file_id (str) – ID in file model

  • meta (dict) – keys to update

upload_complete(file_id, status, func_worker)

Finalize upload - pass to the worker

Parameters
  • file_id (str) – ID of file meta in redis

  • status (str) – completion status from frontend (‘done’ if OK)

  • func_worker (function) – callback for passing to celery worker

Returns

dict with file ID and message, and http status

Return type

tuple

class apicrud.media.storage.StorageBackblaze(model, db_session=None)

Vendor-specific storage interface: Backblaze B2. Not yet implemented

Parameters
  • model (obj) – a storage model

  • db_session (obj) – existing db session

get_upload_url(storage_id, storage_key, content_type='image/jpeg')

Get a presigned authorization URL for upload

Parameters
  • storage_id (str) – ID of storage model object

  • storage_key (str) – pathname of new object to store

  • content_type (str) – mime type

Returns

dict with upload URL, http status

Return type

tuple

class apicrud.media.storage.StorageS3(credential_ttl=3600, credentials=None, access_key=None, secret_key=None, region='us-east-2')

Vendor-specific storage interface: Amazon S3

Parameters
  • credential_ttl (int) – how long until temp credential expires

  • credentials (obj) – API key and secret

  • access_key (str) – API key (if credentials object not specified)

  • secret_key (str) – API secret (if credentials object not specified)

  • region (str) – data center region specification

del_object(bucket, storage_key)

Delete an object

Parameters
  • bucket (str) – name of bucket

  • storage_key (str) – pathname of new object to store

Returns

vendor results

Return type

tuple

get_object(bucket, storage_key)

Fetch the object into a byte array

Parameters
  • bucket (str) – name of bucket

  • storage_key (str) – pathname of new object to store

Returns

bytes

get_upload_url(bucket, storage_key, content_type='image/jpeg')

Get a presigned authorization URL for upload

Parameters
  • bucket (str) – name of bucket

  • storage_key (str) – pathname of new object to store

  • content_type (str) – mime type

Returns

dict with uploadUrl, http status

Return type

tuple

put_object(bucket, storage_key, content, content_type='image/jpeg')

Store a byte-array object

Parameters
  • bucket (str) – name of bucket

  • storage_key (str) – pathname of new object to store

  • content (bytes) – object

  • content_type (str) – mime type

Returns

vendor results

Return type

tuple