Operator#
- class opendal.Operator(scheme, **kwargs)#
The blocking equivalent of AsyncOperator.
Operator is the entry point for all blocking APIs.
See also
- static __new__(scheme, **kwargs)#
Create a blocking operator.
- Parameters:
scheme – The service scheme as a string or a
Schemevalue.kwargs – Service options. Path-like values are converted to strings.
- open(path: str | os.PathLike[str], mode: str, **kwargs: Unpack[OpenKwargs])#
Open a file-like object for the given path.
The returned file is a context manager and reads or writes bytes. Use read() to supply response header overrides when reading.
- Parameters:
- pathstr
The path to the file.
- modestr
“r” to read, “w” to write (overwrite by default), “a” to append, or “x” to create a new file. An optional “b” can appear before or after the letter. All supported modes are binary; text modes such as “rt” raise ValueError. Wrap the returned file with io.TextIOWrapper to read or write text.
- **kwargs
Additional options for the underlying reader or writer. In write and create modes, checksum selects the ChecksumAlgorithm the service uses to verify the full object before committing. Omit checksum or pass None to use the service default.
- Returns:
- File
A file-like object.
- Raises:
- TypeError
If **kwargs contains an unknown keyword argument, an option with a non-None value that does not apply to mode, or a value with an incompatible type.
- ValueError
If the mode is invalid or unsupported, including text (“t”) and update (“+”) modes, or if the mode conflicts with append or if_not_exists in **kwargs, or if the checksum name is unrecognized.
- ConditionNotMatch
If create mode (“x”) targets an existing file. The failure can surface from write(), flush(), or close().
- Unsupported
If the service does not support the requested write options, including the selected checksum algorithm. Append writes cannot request a checksum.
- ChecksumMismatch
If the checksum fails verification. The failure can surface from write(), flush(), or close().
- capability()#
Get all capabilities of this operator.
- Returns:
- Capability
The capability of the operator.
- check()#
Check if the operator is able to work correctly.
- Raises:
- Exception
If the operator is not able to work correctly.
- copy(source, target, *, if_not_exists=None)#
Copy a file from one path to another.
- Parameters:
- sourcestr
The path to the source file.
- targetstr
The path to the target file.
- if_not_existsbool, optional
If True, require copy_with_if_not_exists and raise Unsupported if the service lacks it. Raise ConditionNotMatch if the target already exists. Defaults to False, which allows overwriting.
- create_dir(path)#
Create a directory at the given path.
- Parameters:
- pathstr
The path to the directory.
Notes
To indicate that a path is a directory, it must end with a /. This operation is always recursive, like mkdir -p.
- delete(path, *, version=None, recursive=None, if_match=None)#
Delete a file at the given path.
- Parameters:
- pathstr
The path to the file.
- versionstr, optional
The version of the file to delete. Only supported on version-aware backends.
- recursivebool, optional
If True, delete the path recursively. Only supported on backends that support recursive delete.
- if_matchstr, optional
If set, only delete when the existing object’s ETag matches.
Notes
This operation will not return an error if the path does not exist.
- delete_many(paths)#
Delete an explicit collection of paths with default delete options.
- Parameters:
- pathsIterable[str | PathLike[str]]
The paths to delete. A single path is not accepted.
Notes
This method collects the iterable and converts its Python path objects before deletion starts, using memory proportional to the number of paths. Empty collections do nothing. Paths that do not exist are ignored. Storage or path-validation errors can leave some paths deleted; this operation is not atomic and does not guarantee a single remote request. Use remove_all to recursively delete a path prefix.
- exists(path)#
Check if a path exists.
- Parameters:
- pathstr
The path to check.
- Returns:
- bool
True if the path exists, False otherwise.
- classmethod from_config(config, *, context=None, layers=None)#
Create a new blocking Operator from typed configuration.
A static type checker validates the service scheme and the options for the service, context, and layers. The binding converts non-string values such as booleans, integers, finite floats, durations, paths, and flat lists or tuples to the strings the registry consumes. Service config values such as bytes, mappings, and nested sequences raise
Unsupported.Construction options are stored when pickling the operator and may include credentials. Reapply layers added via
.layer()after unpickling.- Parameters:
- configServiceConfig
A service configuration such as
opendal.config.S3Config.- contextContextConfig | None
Optional per-operator runtime settings, such as an HTTP proxy.
- layersSequence[LayerConfig] | None
Optional declarative layers, applied in sequence order.
- Returns:
- Operator
The new operator.
Examples
import opendal from opendal.config import S3Config op = opendal.Operator.from_config( S3Config(scheme="s3", bucket="my-bucket") )
- classmethod from_uri(uri, **kwargs)#
Create a new blocking Operator from a URI string.
The URI encodes the scheme and configuration in a single string, e.g.
memory://ors3://bucket/path?region=us-east-1. The scheme must belong to a service enabled in this build. Encode service options as query parameters; useurllib.parse.urlencodewhen building the URI dynamically.- Parameters:
- uristr
The URI of the service, including any options as query parameters.
- **kwargsdict
Overrides for URI options. Prefer the URI query string.
- Returns:
- Operator
The new operator.
Examples
from urllib.parse import urlencode import opendal op = opendal.Operator.from_uri("memory://") query = urlencode({"region": "us-east-1"}) op = opendal.Operator.from_uri(f"s3://bucket/path?{query}")
- layer(layer)#
Add a new layer to this operator.
- Parameters:
- layerLayer
The layer to add.
- Returns:
- Operator
A new operator with the layer added.
- list(path, *, limit=None, start_after=None, recursive=None, versions=None, deleted=None)#
List entries in the given directory.
- Parameters:
- pathstr
The path to the directory.
- limitint, optional
The maximum number of entries to return.
- start_afterstr, optional
The entry to start after.
- recursivebool, optional
Whether to list recursively.
- versionsbool, optional
Whether to list versions.
- deletedbool, optional
Whether to list deleted entries.
- Returns:
- BlockingLister
An iterator over the entries in the directory.
- read(path, *, version=None, concurrent=None, chunk=None, gap=None, offset=None, prefetch=None, size=None, if_match=None, if_none_match=None, if_modified_since=None, if_unmodified_since=None, content_type=None, cache_control=None, content_disposition=None)#
Read all or a selected range of bytes from a file.
A positive prefetch cannot be combined with content_type, cache_control, or content_disposition. Combining them raises ValueError.
- Parameters:
- pathstr
The path to the file.
- versionstr, optional
The version of the file.
- concurrentint, optional
The number of concurrent readers.
- chunkint, optional
The size of each chunk.
- gapint, optional
The gap between each chunk.
- offsetint, optional
The offset of the file.
- prefetchint, optional
The number of completed chunks to buffer during concurrent reads.
- sizeint, optional
The size of the file.
- if_matchstr, optional
The ETag of the file.
- if_none_matchstr, optional
The ETag of the file.
- if_modified_sincedatetime, optional
The last modified time of the file.
- if_unmodified_sincedatetime, optional
The last modified time of the file.
- content_typestr, optional
The Content-Type response header override, where supported.
- cache_controlstr, optional
The Cache-Control response header override, where supported.
- content_dispositionstr, optional
The Content-Disposition response header override, where supported.
- Returns:
- bytes
The contents of the file as bytes.
- remove_all(path)#
Recursively remove all files and directories at the given path.
- Parameters:
- pathstr
The path to remove.
- rename(source, target, *, if_not_exists=None)#
Rename (move) a file from one path to another.
- Parameters:
- sourcestr
The path to the source file.
- targetstr
The path to the target file.
- if_not_existsbool, optional
If True, require rename_with_if_not_exists and raise Unsupported if the service lacks it. Raise ConditionNotMatch if the target already exists, leaving both files unchanged. Defaults to False, which allows overwriting.
- scan(path, *, limit=None, start_after=None, versions=None, deleted=None)#
Recursively list entries in the given directory.
- Parameters:
- pathstr
The path to the directory.
- limitint, optional
The maximum number of entries to return.
- start_afterstr, optional
The entry to start after.
- versionsbool, optional
Whether to list versions.
- deletedbool, optional
Whether to list deleted entries.
- Returns:
- BlockingLister
An iterator over the entries in the directory.
Notes
This method is deprecated. Use list() with recursive=True instead.
- stat(path, *, version=None, if_match=None, if_none_match=None, if_modified_since=None, if_unmodified_since=None, content_type=None, cache_control=None, content_disposition=None)#
Get the metadata of a file at the given path.
- Parameters:
- pathstr
The path to the file.
- versionstr, optional
The version of the file.
- if_matchstr, optional
The ETag of the file.
- if_none_matchstr, optional
The ETag of the file.
- if_modified_sincedatetime, optional
The last modified time of the file.
- if_unmodified_sincedatetime, optional
The last modified time of the file.
- content_typestr, optional
The content type of the file.
- cache_controlstr, optional
The cache control of the file.
- content_dispositionstr, optional
The content disposition of the file.
- Returns:
- Metadata
The metadata of the file.
- to_async_operator()#
Create a new AsyncOperator from this blocking operator.
- Returns:
- AsyncOperator
The async operator.
- write(path, bs, *, append=None, chunk=None, concurrent=None, cache_control=None, content_type=None, content_disposition=None, content_encoding=None, if_match=None, if_none_match=None, if_not_exists=None, user_metadata=None, checksum=None)#
Write bytes to a file at the given path.
This function will create a file if it does not exist, and will overwrite its contents if it does.
- Parameters:
- pathstr
The path to the file.
- bsbytes
The contents to write to the file.
- appendbool, optional
Whether to append to the file instead of overwriting it.
- chunkint, optional
The chunk size to use when writing the file.
- concurrentint, optional
The number of concurrent requests to make when writing the file.
- cache_controlstr, optional
The cache control header to set on the file.
- content_typestr, optional
The content type header to set on the file.
- content_dispositionstr, optional
The content disposition header to set on the file.
- content_encodingstr, optional
The content encoding header to set on the file.
- if_matchstr, optional
The ETag to match when writing the file.
- if_none_matchstr, optional
The ETag to not match when writing the file.
- if_not_existsbool, optional
Whether to fail if the file already exists.
- user_metadatadict, optional
The user metadata to set on the file.
- checksumChecksumAlgorithm | None, optional
The algorithm the service uses to verify the full object before committing. Omit it or pass None to use the service default.
- Raises:
- ValueError
If the checksum name is unrecognized.
- Unsupported
If the service cannot verify the write with the selected algorithm, including when appending.
- ChecksumMismatch
If the checksum fails verification.