Setup
Inside an Opus Code task,Opus is already defined and ready to use.
File Operations
All file operations are accessed viaOpus.file.
Downloading a File
UseOpus.file.download(file_url) to fetch a file from Opus storage by its URL:
Modifications to downloaded files are local only unless uploaded via
Opus.file.upload, which creates a new file and returns a new URL; the original remote file is never modified.Getting a Presigned Download URL
UseOpus.file.presigned_download(file_url, expiry=None) to get a presigned URL for downloading a file without fetching it locally:
If no
expiry is provided, the server default is used.Creating a File
UseOpus.file.create(file_name, file_type) to create a new empty file:
If the file isn’t uploaded via
Opus.file.upload, the created file won’t be persisted.Supported File Types
Pass any of these as thefile_type argument:
| Type | Extension | Content Type |
|---|---|---|
JPEG | .jpeg | image/jpeg |
PNG | .png | image/png |
JPG | .jpg | image/jpg |
CSV | .csv | text/csv |
PDF | .pdf | application/pdf |
DOCX | .docx | application/vnd.openxmlformats... |
PPTX | .pptx | application/vnd.openxmlformats... |
XLS | .xls | application/vnd.ms-excel |
XLSX | .xlsx | application/vnd.openxmlformats... |
TXT | .txt | text/plain |
MD | .md | text/markdown |
JSON | .json | application/json |
HTML | .html | text/html |
XML | .xml | application/xml |
"pdf", "PDF", and "Pdf" all work.
Uploading a File
UseOpus.file.upload(file, access_scope="all") to upload an OpusFile to Opus storage.
The
access_scope parameter is optional and defaults to "all".Code Playground: In the code playground, the
access_scope is always "unlisted" regardless of the value provided.You can upload the same file multiple times. Each upload returns a new URL.
Supported Access Scopes
| Scope | Description |
|---|---|
all | Visible to everyone (default) |
workspace | Visible only to current workspace members |
unlisted | Not listed — accessible only via direct file URL |
"Workspace", "WORKSPACE", and "WorkSpace" all work.
OpusFile Interface
OpusFile is the object returned by download() and create(). Its attributes are read-only.
Properties
| Property | Type | Description |
|---|---|---|
file_name | str | None | File name without extension. |
file_ext | str | Extension including leading dot (e.g. ".pdf"). |
file_type | str | Resolved file type enum value. Check Supported File Types. |
file_size | int | Current size on disk in bytes. |
Operations
| Method | Description |
|---|---|
read_bytes() -> bytes | Read entire file content as bytes. |
read_text(encoding="utf-8") -> str | Read entire file content as a string. |
write_bytes(data: bytes) | Replace file content with binary data. |
write_text(data: str, encoding="utf-8") | Replace file content with text. |
open(mode="rb") | Open the underlying file and return a file handle. |
Examples
Reading:Working with File Formats
For format-specific helpers and patterns, check the dedicated guide: Working with File Formats.Errors
All SDK exceptions inherit fromOpusError and are available via Opus.errors:
| Exception | When it’s raised |
|---|---|
OpusError | Base class for all SDK errors. Also raised directly for generic failures. |
FileAPIError | An API call failed (download or upload). |
FileAlreadyExistsError | create() with a name that already exists. |
UnsupportedFileTypeError | create() with an unrecognized file type. |
InvalidFileExpiryError | presigned_download() with an expiry outside the range. |
UnsupportedAccessScopeError | upload() with an invalid access scope. |
PathTraversalError | File name attempted to escape the allowed directory. |
Related
Available Packages
Browse the full list of Python packages you can import in Opus Code.
Working with File Formats
Format-specific helpers for reading and writing PDFs, spreadsheets, and more.
Opus Code
Write custom Python code directly in your workflow.