Skip to content

Files

Resource

FilesResource

Operations on file storage and presigning.

Source code in src/assured/resources/files.py
class FilesResource:
    """Operations on file storage and presigning."""

    def __init__(self, client: AssuredClient) -> None:
        self._client = client

    async def upload(self, file_content: bytes, filename: str, mime_type: str | None = None) -> FileRecord:
        """Upload a file to the Assured platform via its undocumented handle endpoint.

        Note: This utilizes a JSON Web Token explicitly rather than the API Key.

        Args:
            file_content: The raw bytes of the file you are uploading.
            filename: The original filename strings.
            mime_type: File content type, defaults to mimetypes.guess_type or octet-stream.

        Returns:
            FileRecord detailing the UUID object ID along with mapped S3 URIs.
        """
        if mime_type is None:
            mime_type, _ = mimetypes.guess_type(filename)
            mime_type = mime_type or "application/octet-stream"

        ext = os.path.splitext(filename)[1]
        generated_name = f"{uuid.uuid4()}{ext}"

        data = {"name": generated_name}
        files = {"file": (filename, file_content, mime_type)}

        resp = await self._client._post(_HANDLE_PATH, data=data, files=files, requires_jwt=True)
        return FileRecord.model_validate(resp)

    async def presign_url(self, s3_url: str) -> str:
        """Exchange an internal S3 URL for a publicly accessible presigned URL.

        Note: This utilizes a JSON Web Token explicitly rather than the API Key.

        Args:
            s3_url: A target URI starting with `s3://` (found in the `FileRecord` responses)

        Returns:
            A string containing the short-lived presigned URL.
        """
        payload = {
            "s3_url": s3_url,
            "presigned_url": "",
        }
        resp = await self._client._post(_PRESIGN_PATH, json=payload, requires_jwt=True)
        return PresignedUrlResponse.model_validate(resp).presigned_url

upload(file_content, filename, mime_type=None) async

Upload a file to the Assured platform via its undocumented handle endpoint.

Note: This utilizes a JSON Web Token explicitly rather than the API Key.

Parameters:

Name Type Description Default
file_content bytes

The raw bytes of the file you are uploading.

required
filename str

The original filename strings.

required
mime_type str | None

File content type, defaults to mimetypes.guess_type or octet-stream.

None

Returns:

Type Description
FileRecord

FileRecord detailing the UUID object ID along with mapped S3 URIs.

Source code in src/assured/resources/files.py
async def upload(self, file_content: bytes, filename: str, mime_type: str | None = None) -> FileRecord:
    """Upload a file to the Assured platform via its undocumented handle endpoint.

    Note: This utilizes a JSON Web Token explicitly rather than the API Key.

    Args:
        file_content: The raw bytes of the file you are uploading.
        filename: The original filename strings.
        mime_type: File content type, defaults to mimetypes.guess_type or octet-stream.

    Returns:
        FileRecord detailing the UUID object ID along with mapped S3 URIs.
    """
    if mime_type is None:
        mime_type, _ = mimetypes.guess_type(filename)
        mime_type = mime_type or "application/octet-stream"

    ext = os.path.splitext(filename)[1]
    generated_name = f"{uuid.uuid4()}{ext}"

    data = {"name": generated_name}
    files = {"file": (filename, file_content, mime_type)}

    resp = await self._client._post(_HANDLE_PATH, data=data, files=files, requires_jwt=True)
    return FileRecord.model_validate(resp)

presign_url(s3_url) async

Exchange an internal S3 URL for a publicly accessible presigned URL.

Note: This utilizes a JSON Web Token explicitly rather than the API Key.

Parameters:

Name Type Description Default
s3_url str

A target URI starting with s3:// (found in the FileRecord responses)

required

Returns:

Type Description
str

A string containing the short-lived presigned URL.

Source code in src/assured/resources/files.py
async def presign_url(self, s3_url: str) -> str:
    """Exchange an internal S3 URL for a publicly accessible presigned URL.

    Note: This utilizes a JSON Web Token explicitly rather than the API Key.

    Args:
        s3_url: A target URI starting with `s3://` (found in the `FileRecord` responses)

    Returns:
        A string containing the short-lived presigned URL.
    """
    payload = {
        "s3_url": s3_url,
        "presigned_url": "",
    }
    resp = await self._client._post(_PRESIGN_PATH, json=payload, requires_jwt=True)
    return PresignedUrlResponse.model_validate(resp).presigned_url

Models

FileRecord

Bases: BaseModel

Source code in src/assured/models/files.py
class FileRecord(BaseModel):
    model_config = ConfigDict(extra="ignore")

    id: str
    file: str | None = None
    file_url: str | None = None
    name: str | None = None
    created_at: datetime | None = None
    storage_metadata: StorageMetadata | None = None
    uploaded_file_uri: str | None = None

StorageMetadata

Bases: BaseModel

Source code in src/assured/models/files.py
class StorageMetadata(BaseModel):
    model_config = ConfigDict(extra="ignore")

    storage_backend: str | None = None
    storage_backend_class: str | None = None
    bucket_name: str | None = None
    region: str | None = None
    endpoint_url: str | None = None
    custom_domain: str | None = None
    credential_profile: str | None = None
    media_root: str | None = None
    use_s3_for_media: bool | None = None
    acl: str | None = None
    signature_version: str | None = None
    file_overwrite: bool | None = None
    querystring_auth: bool | None = None
    object_parameters: dict[str, Any] | None = None
    original_filename: str | None = None
    content_type: str | None = None
    file_size_bytes: int | None = None
    upload_path: str | None = None
    uploaded_by_user_id: str | None = None
    uploaded_by_client_id: str | None = None
    s3_object_url: str | None = None
    s3_uri: str | None = None
    django_env: str | None = None
    debug_mode: bool | None = None
    uploaded_file_url: str | None = None
    uploaded_file_uri: str | None = None

PresignedUrlResponse

Bases: BaseModel

Source code in src/assured/models/files.py
class PresignedUrlResponse(BaseModel):
    model_config = ConfigDict(extra="ignore")

    presigned_url: str