Client
The Replit Object Storage Client is a Python interface that manages interactions with Replit Object Storage. It is a convenient tool for developers to handle tasks such as copying, deleting, uploading, and downloading objects within Replit Object Storage Buckets.
To import the client class from the replit.object_storage
package, use the following command:
from replit.object_storage import Client
Class Client
The Client
class manages interactions with Replit Object Storage. It provides methods for performing common operations on objects within a specified Bucket. The following command allows you create a Client
instance to interact with Replit Object Storage.
client = Client()
- This
Client
is a thin wrapper over the GCS(Google Cloud Storage) Python library. As a result, many docstrings are borrowed from the underlying library. - If an application uses multiple Buckets, it's recommended to use one
Client
instance per Bucket.
-
When interacting with Replit Object Storage using the
Client
, any method might return one of the following errors:-
BucketNotFoundError
: The method could not find the specified Bucket configured for the client. -
DefaultBucketError
: Occurs when no Bucket was explicitly configured, and an error occurs while resolving the default Bucket. -
ForbiddenError
: Signifies that access to the requested resource is not allowed due to permission restrictions. -
TooManyRequestsError
: Indicates that the operation is rate-limited due to excessive requests. -
UnauthorizedError
: Occurs when the requested operation is not allowed due to lack of proper authorization.
-
__init__
The init
method initializes a new instance of the Client class.
def __init__(bucket_id: Optional[str] = None)
Argument:
bucket_id
(Optional[str]): The ID of the Bucket the client should interface with. If no ID is provided, the method will use the default Bucket associated with the Repl or Deployment.
copy
The copy
method copies an Object within the same Bucket. If an Object exists in the same location, it will be overwritten.
def copy(object_name: str, dest_object_name: str) -> None
Arguments:
object_name
(str) - The full path of the Object to be copied.dest_object_name
(str) - The full path to copy the Object to.
Raises:
ObjectNotFoundError
- If the source Object could not be found.
delete
The delete
method deletes an Object from Object Storage.
def delete(object_name: str, ignore_not_found: bool = False) -> None
Arguments:
object_name
(str) - The name of the Object to be deleted.ignore_not_found
(bool) - Whether to raise an error if the Object does not exist.
Raises:
ObjectNotFoundError
- If the Object could not be found.
download_as_bytes
The download_as_bytes
method downloads the contents of an Object as a bytes Object.
def download_as_bytes(object_name: str) -> bytes
Argument:
object_name
(str) - The name of the Object to be downloaded.
Returns:
bytes
- The raw byte representation of the Object's contents.
Raises:
ObjectNotFoundError
- If the Object could not be found.
download_as_text
The download_as_text
method downloads the contents of an Object as a string.
def download_as_text(object_name: str) -> str
Argument:
object_name
(str) - The name of the Object to be downloaded.
Returns:
- str: The Object's contents as a UTF-8 encoded string.
Raises:
ObjectNotFoundError
- If the Object could not be found.
download_to_filename
Downloads the contents of an Object into a file on the local disk.
def download_to_filename(object_name: str, dest_filename: str) -> None
Arguments:
object_name
(str) - The name of the Object to be downloaded.dest_filename
(str) - The filename of the file on the local disk to be written.
Raises:
ObjectNotFoundError
- If the Object could not be found.
exists
The exists
method checks if an Object exists.
def exists(object_name: str) -> bool
Argument:
object_name
(str) - The name of the Object to be checked.
Returns:
bool
: True if the Object exists, False otherwise.
list
The list
method lists Objects in the Bucket.
def list(end_offset: Optional[str] = None,
match_glob: Optional[str] = None,
max_results: Optional[int] = None,
prefix: Optional[str] = None,
start_offset: Optional[str] = None) -> List[Object]
Arguments:
end_offset
(Optional[str]) - Filter results to Objects whose names are lexicographically before end_offset. If start_offset is also set, the Objects listed have names between start_offset (inclusive) and end_offset (exclusive).match_glob
(Optional[str]) - Glob pattern used to filter results, for example foo*bar.max_results
(Optional[int]) - The maximum number of results that can be returned 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
. IfendOffset
is alsoset
, the Objects listed have names betweenstart_offset
(inclusive) andend_offset
(exclusive).
Returns:
List
(Object): A list of Objects matching the given query parameters.
upload_from_filename
The upload_from_filename
method uploads an Object from a file on the local disk.
def upload_from_filename(dest_object_name: str, src_filename: str) -> None
Arguments:
dest_object_name
(str) - The name of the Object to be uploaded.src_filename
(str) - The filename of a file on the local disk
upload_from_bytes
The upload_from_bytes
method uploads an Object from bytes.
def upload_from_bytes(dest_object_name: str, src_data: bytes) -> None
Arguments:
dest_object_name
(str) - The name of the Object to be uploaded.src_data
(str) - The bytes to be uploaded.
upload_from_text
The upload_from_text
method uploads an Object from a string.
def upload_from_text(dest_object_name: str, src_data: Union[bytes, str]) -> None
Arguments:
dest_object_name
(str) - The name of the Object to be uploaded.src_data
(str)- The text to be uploaded.