Fork me on GitHub

sqlalchemy_imageattach.stores.s3 — AWS S3 backend storage

The backend storage implementation for Simple Storage Service provided by Amazon Web Services.

sqlalchemy_imageattach.stores.s3.BASE_URL_FORMAT = 'https://{0}.s3.amazonaws.com'

(str) The format string of base url of AWS S3. Contains no trailing slash. Default is 'https://{0}.s3.amazonaws.com'.

sqlalchemy_imageattach.stores.s3.DEFAULT_MAX_AGE = 31536000

(numbers.Integral) The default max-age seconds of Cache-Control. It’s the default value of S3Store.max_age attribute.

exception sqlalchemy_imageattach.stores.s3.AuthMechanismError(url, code, msg, hdrs, fp)

Raised when the bucket doesn’t support Signature Version 2 (AWS2Auth) anymore but supports only Signature Version 4 (AWS4Auth).

For the most part, it can be resolved by determining S3Store.region.

New in version 1.1.0.

class sqlalchemy_imageattach.stores.s3.S3Request(url, bucket, access_key, secret_key, data=None, headers={}, method=None, content_type=None)

Remained for backward compatibility. Use S3RequestV2 (which was renamed) or S3RequestV4 (which is the current standard).

Deprecated since version 1.1.0: Renamed to S3RequestV2.

class sqlalchemy_imageattach.stores.s3.S3RequestV2(url, bucket, access_key, secret_key, data=None, headers={}, method=None, content_type=None)

HTTP request for S3 REST API which does authentication using Signature Version 2 (AWS2Auth) which has been deprecated since January 30, 2014.

New in version 1.1.0.

Changed in version 1.1.0: Renamed from S3Request (which is now deprecated).

class sqlalchemy_imageattach.stores.s3.S3RequestV4(url, bucket, region, access_key, secret_key, data=None, headers={}, method=None, content_type=None)

HTTP request for S3 REST API which does authentication using Signature Version 4 (AWS4Auth).

New in version 1.1.0.

class sqlalchemy_imageattach.stores.s3.S3SandboxStore(underlying, overriding, access_key=None, secret_key=None, max_age=31536000, underlying_prefix='', overriding_prefix='', underlying_region=None, overriding_region=None, max_retry=5)

It stores images into physically two separated S3 buckets while these look like logically exist in the same store. It takes two buckets for read-only and overwrite: underlying and overriding.

It’s useful for development/testing purpose, because you can use the production store in sandbox.

Parameters:
  • underlying (str) – the name of underlying bucket for read-only
  • overriding (str) – the name of overriding bucket to record overriding modifications
  • max_age (numbers.Integral) – the max-age seconds of Cache-Control. default is DEFAULT_MAX_AGE
  • overriding_prefix (str) – means the same to S3Store.prefix but it’s only applied for overriding
  • underlying_prefix (str) – means the same to S3Store.prefix but it’s only applied for underlying
  • overriding_region (str) – Means the same to S3Store.region but it’s only applied for overriding.
  • underlying_region (str) – Means the same to S3Store.region but it’s only applied for underlying.
  • max_retry (int) – Retry the given number times if uploading fails. 5 by default.
Raises:

AuthMechanismError

Raised when the bucket doesn’t support Signature Version 2 (AWS2Auth) anymore but supports only Signature Version 4 (AWS4Auth). For the most part, it can be resolved by determining region parameter.

New in version 1.1.0: The underlying_region, overriding_region, and max_retry parameters.

DELETED_MARK_MIMETYPE = 'application/x-sqlalchemy-imageattach-sandbox-deleted'

All keys marked as “deleted” have this mimetype as its Content-Type header.

overriding = None

(S3Store) The overriding store to record overriding modification.

underlying = None

(S3Store) The underlying store for read-only.

class sqlalchemy_imageattach.stores.s3.S3Store(bucket, access_key=None, secret_key=None, max_age=31536000, prefix='', public_base_url=None, region=None, max_retry=5)

Image storage backend implementation using S3. It implements Store interface.

If you’d like to use it with Amazon CloudFront, pass the base url of the distribution to public_base_url. Note that you should configure Forward Query Strings to Yes when you create the distribution. Because SQLAlchemy-ImageAttach will add query strings to public URLs to invalidate cache when the image is updated.

Parameters:
  • bucket (str) – the buckect name
  • max_age (numbers.Integral) – the max-age seconds of Cache-Control. default is DEFAULT_MAX_AGE
  • prefix (str) – the optional key prefix to logically separate stores with the same bucket. not used by default
  • public_base_url (str) – an optional url base for public urls. useful when used with cdn
  • region (str) –

    The region code that the bucket belongs to. If None it authenticates using Signature Version 2 (AWS2Auth) which has been deprecated since January 30, 2014. Because Signature Version 4 (AWS4Auth) requires to determine the region code before signing API requests. Since recent regions don’t support Signature Version 2 (AWS2Auth) but only Signature Version 4 (AWS4Auth), if you set region to None and bucket doesn’t support Signature Version 2 (AWS2Auth) anymore AuthMechanismError would be raised. None by default.

  • max_retry (int) – Retry the given number times if uploading fails. 5 by default.
Raises:

AuthMechanismError

Raised when the bucket doesn’t support Signature Version 2 (AWS2Auth) anymore but supports only Signature Version 4 (AWS4Auth). For the most part, it can be resolved by determining region parameter.

New in version 1.1.0: The region and max_retry parameters.

Changed in version 0.8.1: Added public_base_url parameter.

bucket = None

(str) The S3 bucket name.

max_age = None

(numbers.Integral) The max-age seconds of Cache-Control.

max_retry = None

(int) Retry the given number times if uploading fails.

New in version 1.1.0.

prefix = None

(str) The optional key prefix to logically separate stores with the same bucket.

public_base_url = None

(str) The optional url base for public urls.

region = None

(str) The region code that the bucket belongs to. If None it authenticates using Signature Version 2 (AWS2Auth) which has been deprecated since January 30, 2014. Because Signature Version 4 (AWS4Auth) requires to determine the region code before signing API requests.

Since recent regions don’t support Signature Version 2 (AWS2Auth) but only Signature Version 4 (AWS4Auth), if you set region to None and bucket doesn’t support Signature Version 2 (AWS2Auth) anymore AuthMechanismError would be raised.

New in version 1.1.0.