Skip to content

API Reference parrot_formdesigner.services.blob_storage

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot_formdesigner.services.blob_storage

View source

Async blob storage abstraction for FieldType.REST uploads.

Provides AbstractBlobStorage (ABC) plus concrete backends — S3BlobStorage, GCSBlobStorage, LocalBlobStorage, TempBlobStorage — each implemented as a thin adapter over the matching navigator.utils.file FileManager.

Credential resolution and provider-specific I/O live in the FileManager implementations; this module only handles:

  • BlobMetadata → object key construction ({prefix}{form_uid}/{field_uid}/{uuid}).
  • blob_ref round-tripping (scheme + key).
  • The pre_persist_hook extension point.

The put contract collects the inbound async byte stream into memory before writing — sufficient for V1 form uploads, which are size-bounded by the multipart parser upstream. Streaming/multipart uploads are delegated to the FileManager when applicable.

Class AbstractBlobStorage

Abstract async blob storage.

Concrete implementations: S3BlobStorage, GCSBlobStorage, LocalBlobStorage, TempBlobStorage, and any user-supplied backend inheriting from this class.

All methods are async. The pre_persist_hook is called by put before writing; subclasses may raise BlobRejectedError to abort.

Method AbstractBlobStorage.put

Persist a blob and return a stable blob reference.

The stream is consumed from the async iterator. V1 implementations collect all chunks before writing; future revisions may stream.

The pre_persist_hook is invoked before writing begins; if it raises BlobRejectedError the blob is NOT written and the error propagates to the caller.

put(stream: AsyncIterator[bytes], *, metadata: BlobMetadata) -> str

Parameter Type Default Description
stream AsyncIterator[bytes] Async byte-chunk iterator.
metadata BlobMetadata Contextual metadata for the blob.

Returns: str — Stable blob reference string (implementation-defined format).

Raises:

  • BlobRejectedError — If pre_persist_hook rejects the blob.

Method AbstractBlobStorage.get

Stream a blob by reference.

get(blob_ref: str) -> AsyncIterator[bytes]

Parameter Type Default Description
blob_ref str Blob reference returned by put.

Returns: AsyncIterator[bytes] — Async iterator of byte chunks.

Method AbstractBlobStorage.delete

Delete a blob by reference. Idempotent — no error if missing.

delete(blob_ref: str, *, strict: bool = False) -> None

Parameter Type Default Description
blob_ref str Blob reference returned by put.
strict bool False Re-raise backend errors other than "missing" (FEAT-731 reaper). The default keeps the historical swallow-and-warn.

Method AbstractBlobStorage.pre_persist_hook

Pre-write hook for AV/content-scanning. V1 default: no-op.

Subclasses MAY override this to inspect the blob before it is written. Raise BlobRejectedError to prevent the write.

pre_persist_hook(ctx: PrePersistContext) -> None

Parameter Type Default Description
ctx PrePersistContext Pre-persist context (metadata + optional preview bytes).

Returns: None — None (always, in V1 default implementation).

Class BlobMetadata

Metadata associated with a persisted blob.

Fields:

Field Type Default Description
form_uid uuid.UUID Immutable UUID of the parent form (FEAT-389). Used to construct the blob's storage key so renaming a form never orphans its existing uploads.
form_id str Identifier (slug) of the parent form. Kept for human-readable reference/logging — no longer used in key construction.
field_uid uuid.UUID Immutable UUID of the form field that owns this blob (FEAT-393). Used to construct the blob's storage key so renaming a field's field_id never orphans its existing uploads.
field_id str Identifier (slug) of the form field that owns this blob. Kept for human-readable reference/logging — no longer used in key construction.
submission_id str | None None Optional submission ID for audit correlation.
tenant str | None None Optional tenant slug for multi-tenant deployments.
content_type str MIME type of the stored content (e.g. image/jpeg).
size_bytes int Size of the content in bytes.
blob_id str | None None Optional caller-chosen last key segment. When set, the blob is stored under {form_uid}/{field_uid}/{blob_id} instead of a fresh uuid4 — so a retried upload of the SAME file (same client upload id) overwrites one blob instead of adding a duplicate. None keeps the historical random name.

Class BlobRejectedError

Raised by AbstractBlobStorage.pre_persist_hook to abort a put.

Subclasses may raise this from their hook implementation to prevent the blob from being persisted. The upload handler will propagate this error to the caller.

Class GCSBlobStorage

GCS blob storage backed by navigator.utils.file.gcs.GCSFileManager.

blob_ref format::

gs://<bucket>/<prefix><form_uid>/<field_uid>/<uuid>
Parameter Type Default Description
bucket str | None None GCS bucket name.
prefix str '' Key prefix prepended to every blob.
**manager_kwargs Any {} Forwarded to GCSFileManager (e.g. project_id, credentials_path).

Class LocalBlobStorage

Local filesystem blob storage backed by LocalFileManager.

Suitable for single-host deployments or development environments. All blobs live under a single base_path directory sandboxed by the underlying LocalFileManager.

blob_ref format::

file://<prefix><form_uid>/<field_uid>/<uuid>

The path is relative to the manager's base_path — refs are only valid against the same LocalBlobStorage configuration that produced them.

Parameter Type Default Description
base_path str | Path | None None Root directory for blob storage. Falls back to PARROT_BLOB_PATH env var, then to "./blobs".
prefix str '' Key prefix prepended to every blob.

Class PrePersistContext

Context passed to AbstractBlobStorage.pre_persist_hook before writing.

In V1 the default pre_persist_hook is a no-op. V2 will use this context to perform AV/content-scanning before persisting the blob.

Fields:

Field Type Default Description
metadata BlobMetadata Full blob metadata.
content_preview bytes | None None First N bytes of the content for scanning. None disables preview-based scanning.

Class S3BlobStorage

S3 blob storage backed by navigator.utils.file.s3.S3FileManager.

Credentials are resolved by S3FileManager using AWS_CREDENTIALS from parrot.conf keyed by aws_id (default "default"), or an explicit credentials dict passed through. Bucket resolution falls back to PARROT_BLOB_BUCKET for backward compatibility, then to the credential profile's bucket_name.

blob_ref format::

s3://<bucket>/<prefix><form_uid>/<field_uid>/<uuid>
Parameter Type Default Description
bucket str | None None S3 bucket name. Falls back to PARROT_BLOB_BUCKET env var and then to AWS_CREDENTIALS[aws_id]["bucket_name"].
prefix str '' Key prefix prepended to every blob. Falls back to PARROT_BLOB_PREFIX. Defaults to "".
aws_id str 'default' Name of the profile in AWS_CREDENTIALS (default "default").
region_name str | None None AWS region override.
credentials dict[str, Any] | None None Explicit credentials dict ({"aws_key": ..., "aws_secret": ...}). Bypasses AWS_CREDENTIALS lookup when provided.

Attributes:

Attribute Type Description
bucket str
prefix str

Class TempBlobStorage

Ephemeral blob storage backed by TempFileManager.

Default lazy backend when no app["blob_storage"] is configured. Useful for tests and local development: never talks to S3/GCS, never needs credentials, and cleans itself up on process exit.

blob_ref format::

temp://<prefix><form_uid>/<field_uid>/<uuid>
Parameter Type Default Description
prefix str '' Key prefix prepended to every blob (also passed as the temp-directory prefix when temp_dir_prefix is omitted).
temp_dir_prefix str 'parrot_blobs_' Override the temp-directory name prefix.

Method TempBlobStorage.get

Stream the blob bytes directly from disk.

TempFileManager.download_file has move semantics — it removes the source after copying — so we read the file via Path instead to keep get() idempotent.

get(blob_ref: str) -> AsyncIterator[bytes]

Parameter Type Default Description
blob_ref str

Returns: AsyncIterator[bytes]

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally