Ardent

File Upload

class q2_sdk.ardent.file_upload.PublicDownloadUrl(raw)[source]

PublicDownloadUrl(raw: dict)

// Sample raw input
{
    "data": {
        "headers": {
            "x-amz-server-side-encryption-customer-algorithm":"AES256",
            "x-amz-server-side-encryption-customer-key":"abc",
            "x-amz-server-side-encryption-customer-key-MD5":"hash"
        },
        "url": "https://awsurl",
        "proxyUrl": "http://ardentfsproxyurl"
    }
}
class q2_sdk.ardent.file_upload.AwsFile(raw)[source]

AwsFile(raw: dict)

Transforms an ArdentFS JSON blob into a usable Python object

// Sample raw input
{
  "data": {
    "url": "https://s3.us-east-2.amazonaws.com/ardent-fs",
    "fields": {
      "bucket": "ardent-fs",
      "X-Amz-Algorithm": "AWS4-HMAC-SHA256",
      "X-Amz-Credential": "AAAAAB4WV3347YTY2G7Z/20200820/us-east-2/s3/aws4_request",
      "X-Amz-Date": "20200820T195929Z",
      "X-Amz-Security-Token": "FwoGZX...",
      "Policy": "eyJleHBpcmF...",
      "X-Amz-Signature": "f5e87a...",
      "key": "dev/c35a08ed-6e17-4bd3-a021-41cd8ef5298f"
    },
    "bucket": "dev",
    "id": "c35a08ed-6e17-4bd3-a021-41cd8ef5298f"
  }
}
enum q2_sdk.ardent.file_upload.TTLType(value)[source]

Valid values are as follows:

Days = <TTLType.Days: 1>
Years = <TTLType.Years: 2>
class q2_sdk.ardent.file_upload.File(logger)[source]

Uploads and downloads files from Amazon S3 by way of Q2’s ArdentFS endpoints. This allows for much larger file sizes over the network than would be possible by POSTing those files to the SDK directly.

Uploaded files can be downloaded for a limited time before their TTL expires (24 hours typically).

async get_upload_url(name=None)[source]

Query ArdentFS for a url to POST a file to. This is useful for loading a file to cloud storage straight from the JavaScript layer, bypassing the Q2 infrastructure.

Parameters:

name (Optional[str]) – Name in AWS is always a guid, but this adds a metadata field with the name

Return type:

AwsFile

async upload_from_file(data, name=None, serialize_for_cli=False, timeout=30, ttl=1, ttl_type=TTLType.Days, content_type=None, **kwargs)[source]

Uploads file to AWS using temporary upload URL gathered from ArdentFS

Parameters:
  • data (Union[str, BytesIO]) – Either the path to a file or an already open BytesIO object

  • name (Optional[str]) – Name in AWS is always a guid, but this adds a metadata field with the name

  • timeout – Max upload time. Defaults to 30 seconds

  • ttl – Time to Live for file in AWS S3. Allows [1, 2, 3]

  • ttl_type (TTLType) – Can be either days for short term or years for long term storage

  • content_type (Optional[str]) – Corresponds to the Content-Type header of the request

Return type:

str

Returns:

ID of file in aws

async upload(data, name=None, timeout=30, ttl_days=1, content_type=None)[source]

Uploads to AWS using a temporary upload URL gathered from ArdentFS

Parameters:
  • data (Union[str, bytes]) – String/Bytes to upload to AWS

  • name (Optional[str]) – Name in AWS is always a guid, but this adds a metadata field with the name

  • timeout – Max upload time. Defaults to 30 seconds

  • ttl_days – Time to Live for file in AWS S3. Allows [1, 2, 3]

  • content_type (Optional[str]) – Corresponds to the Content-Type header of the request

Return type:

str

Returns:

ID of file in aws

async get_public_download_url(file_key, expiry=7200, render_in_browser=False, serialize_for_cli=False)[source]

Generates several download urls accessible from outside the Q2 network. The response.aws_url combined with response.headers will download directly from Amazon S3. The response.proxy_url can be used as a standalone url with a hop in between Amazon S3.

Parameters:
  • file_key (str) – Name of file in AWS. Corresponds to AwsFile.id

  • expiry – TTL (in seconds)

  • render_in_browser – If True, browser will attempt to render inline rather than auto download, if content-type was provided at the time of upload

  • serialize_for_cli – Used when running from the command line

Return type:

PublicDownloadUrl

Returns:

PublicDownloadUrl instance

static get_download_url(file_key)[source]

Returns the url to download a file stored in AWS

Parameters:

file_key (str) – Name of file in AWS. Corresponds to AwsFile.id

async download(file_key, output_path=None, serialize_for_cli=False, timeout=30)[source]

Pull file_key contents from AWS, optionally writing it to the filesystem.

Parameters:
  • file_key (str) – Name of file in AWS. Corresponds to AwsFile.id

  • output_path (Optional[str]) – If specified, will write the data to a file

  • timeout – Max download time. Defaults to 30 seconds

Return type:

bytes