Client class from the replit-object-storage-python package and provides code examples for its class methods.
Client
TheClient class manages interactions with Replit App Storage. This class features methods for performing operations on objects in a bucket.
To import the class from the replit.object_storage package, add the following line to your Python code:
Client instance that interacts with Replit App Storage:
If your app uses multiple buckets, create one
Client instance per bucket.__init__
Theinit method initializes an instance of the Client class.
bucket_id(Optional[str]): The ID of the bucket the client manages. When omitted, the Client uses the default bucket associated with the Replit App or Deployment.
copy
Thecopy method copies an object within the same bucket. If an object exists in the same location, it overwrites the original.
object_name(str) - The full path of the source object.dest_object_name(str) - The full path of the object destination.
ObjectNotFoundError- Indicates the source object does not exist at the specified path.
delete
Thedelete method permanently removes a file from App Storage.
object_name(str) - The name of the object to delete.ignore_not_found(bool) - WhenTrue, suppress the error if the object does not exist.
ObjectNotFoundError- Indicates the object does not exist.
download_as_bytes
Thedownload_as_bytes method retrieves the contents of a file as bytes.
object_name(str) - The name of the object to download.
bytes- The raw byte representation of the object’s contents.
ObjectNotFoundError- Indicates the object does not exist.
download_as_text
Thedownload_as_text method downloads the contents of a file as a str type.
object_name(str) - The source object name to retrieve.
- str: The object’s contents as a UTF-8 encoded string.
ObjectNotFoundError- Indicates the object does not exist.
download_to_filename
Downloads the contents of an object into a file on the local disk.object_name(str) - The name of the source object from App Storage to retrieve.dest_filename(str) - The destination filename on local disk.
ObjectNotFoundError- Indicates the object does not exist.
exists
Theexists method checks if an object exists.
object_name(str) - The name of the object to check for existence.
bool:Trueif the object exists, False otherwise.
list
Thelist method lists objects in the Bucket.
end_offset(Optional[str]) - Filter results to objects named lexicographically beforeend_offset. Ifstart_offsetis defined, the objects listed have names betweenstart_offset(inclusive) andend_offset(exclusive).match_glob(Optional[str]) - Use a glob pattern to filter results. For example: “foo*bar” matches “footbar”, “foo baz bar”, and “foobar”.max_results(Optional[int]) - The maximum number of results to return in the response.prefix(Optional[str]) - Filter results to objects whose names have the specified prefix.start_offset(Optional[str]) - Filter results to objects whose names are lexicographically equal to or afterstart_offset. Whenend_offsetis set, the objects listed have names betweenstart_offset(inclusive) andend_offset(exclusive).
List(Object): A list of objects matching the given query parameters.
upload_from_filename
Useupload_from_filename() to upload an object from a source file on the local disk to App Storage.
dest_object_name(str) - The name of the uploaded file.src_filename(str) - The source file to upload.
upload_from_bytes
Theupload_from_bytes method uploads an object from bytes data.
dest_object_name(str) - The name of the object to upload.src_data(str) - Thebytesdata to upload.
upload_from_text
Theupload_from_text method uploads an object from a string.
dest_object_name(str) - The name of the object to upload.src_data(str)- The text data to upload.
Exception types
-
When interacting with Replit App Storage using the
Client, any method might return one of the following errors:-
BucketNotFoundError: Indicates the configured bucket name does not match with any bucket in App Storage. -
DefaultBucketError: Indicates missing default bucket configuration. -
ForbiddenError: Indicates insufficient permissions to access to the bucket. -
TooManyRequestsError: Indicates the operation is rate-limited due to excessive requests. -
UnauthorizedError: Indicates authorization restricted access to the operation.
-