Storage Service

The storage service: the platform’s private object storage, for files that only their owner may download.

The environment’s public bucket serves the web console and the chat widget through the CDN, so everything in it is public. Files that belong to one person, e.g. the archive of a user’s data export, go to private storage instead, and are shared with a link that expires: a presigned url.

The platform uses StorageService through smarter.apps.infrastructure.services.infrastructure .storage, and the cloud provider implements it, e.g. S3StorageService with the environment’s private exports bucket, smarter_settings.aws_s3_exports_bucket_name.

from smarter.apps.infrastructure.services import infrastructure

infrastructure.storage.put_object("data-exports/42/export.zip", content, "application/zip")
url = infrastructure.storage.presigned_url("data-exports/42/export.zip", expires_in=86400, filename="export.zip")
infrastructure.storage.delete_object("data-exports/42/export.zip")
smarter.apps.infrastructure.services.storage.MAX_PRESIGNED_URL_SECONDS = 604800

seven days, the most that AWS S3 allows.

Type:

The longest that a presigned url may last

class smarter.apps.infrastructure.services.storage.StorageService(provider_name, *args, **kwargs)[source]

Bases: InfrastructureService

The private object storage of a cloud provider.

Objects are private: nobody can read one without the provider’s credentials, or a presigned url, which presigned_url() makes.

Parameters:

provider_name (str) – The name of the provider, e.g. aws.

abstract property bucket: str

The name of the private bucket, or its equivalent, that holds the objects.

delete_object(key)[source]

Delete a private object, if it exists.

Parameters:

key (str) – The object’s key.

Raises:

StorageServiceError – If the object cannot be deleted.

Return type:

None

error_class

alias of StorageServiceError

presigned_url(key, expires_in, filename=None)[source]

Return a url that downloads a private object, without credentials, until it expires.

Anyone who has the url can download the object until then, so it is sent only to the object’s owner.

Parameters:
  • key (str) – The object’s key.

  • expires_in (int) – How many seconds the url works, at most seven days.

  • filename (str | None) – The file name that the browser saves the download as, or None for the key’s.

Return type:

str

Returns:

The url.

Raises:

StorageServiceError – If expires_in is out of range, or the url cannot be made.

put_object(key, content, content_type='application/octet-stream')[source]

Save a private object.

Parameters:
  • key (str) – The object’s key, e.g. data-exports/42/export.zip.

  • content (bytes) – The object’s contents.

  • content_type (str) – The object’s MIME type, e.g. application/zip.

Raises:
Return type:

None

service_name: str = 'storage'

The name of the service in signals and logs, see InfrastructureServiceNames.

The storage service of AWS: the environment’s private S3 bucket of exports.

S3StorageService implements the primitives of StorageService with an S3 client of the AWS provider’s boto3 session. Its bucket, smarter_settings.aws_s3_exports_bucket_name, is created by smarter-infrastructure: it blocks all public access, encrypts its objects, and deletes them after a day. A presigned url is signed with Signature Version 4, which AWS requires for urls that last longer than an hour.

class smarter.apps.infrastructure.providers.aws.storage.S3StorageService(provider, bucket=None, client=None, **kwargs)[source]

Bases: StorageService

AWS S3, as the platform’s private object storage.

Parameters:
  • provider (AWSProvider) – The AWS provider, which authenticates with AWS.

  • bucket (str | None) – The bucket, by default smarter_settings.aws_s3_exports_bucket_name.

  • client (Any | None) – An S3 client, e.g. one that a test stubs, or None for one of the provider’s session.

__init__(provider, bucket=None, client=None, **kwargs)[source]
property bucket: str

The private bucket, smarter_settings.aws_s3_exports_bucket_name unless one was given.

property client: Any

The S3 client, created from the provider’s boto3 session when it is first used.

property ready: bool

Whether the AWS provider is ready, i.e. authenticated with AWS.