Skip to main content
This reference guide explains the Client class from the object-storage library and provides code examples for its methods.

Client

The Client class manages interactions with Replit App Storage. This class features methods for performing operations on files in a bucket. To import the class from the library, add the following line to your JavaScript code:
Use the following code to create a Client instance that interacts with Replit Object Storage:

Constructors

constructor
  • new Client(options?): Client
Creates a new Client instance to interact with Replit App Storage. Parameters Returns Client

Methods

copy

  • copy(objectName, destObjectName): Promise<Result<null, RequestError>>
Copies an object within the same bucket. This method overwrites any existing object at the destination path. Parameters Returns Promise<Result<null, RequestError>>

delete

  • delete(objectName, options?): Promise<Result<null, RequestError>>
Deletes an object from the bucket. Parameters Returns Promise<Result<null, RequestError>>

downloadAsBytes

  • downloadAsBytes(objectName, options?): Promise<Result<Buffer, RequestError>>
Downloads an object and returns its raw contents as a buffer. Parameters Returns Promise<Result<Buffer, RequestError>>

downloadAsStream

  • downloadAsStream(objectName, options?): Readable
Creates a readable stream of the object’s contents. The stream emits any errors encountered during the download. Parameters Returns Readable

downloadAsText

  • downloadAsText(objectName, options?): Promise<Result<string, RequestError>>
Downloads an object and returns its contents as a string. Parameters Returns Promise<Result<string, RequestError>>

downloadToFilename

  • downloadToFilename(objectName, destFilename, options?): Promise<Result<null, RequestError>>
Downloads an object and saves it to the specified location on the local filesystem. Parameters Returns Promise<Result<null, RequestError>>

exists

  • exists(objectName): Promise<Result<boolean, RequestError>>
Checks if an object exists in the bucket. Parameters Returns Promise<Result<boolean, RequestError>>

getBucket

  • getBucket(): Promise<Bucket>
Returns Promise<Bucket>

init

  • init(bucketId?): Promise<Bucket>
Parameters Returns Promise<Bucket>

list

  • list(options?): Promise<Result<StorageObject[], RequestError>>
Returns a list of all objects in the bucket. Parameters Returns Promise<Result<StorageObject[], RequestError>>

mapUploadOptions

  • mapUploadOptions(options?): undefined | UploadOptions
Parameters Returns undefined | UploadOptions

uploadFromBytes

  • uploadFromBytes(objectName, contents, options?): Promise<Result<null, RequestError>>
Uploads an object using its in-memory byte representation. This method overwrites any existing object with the same name. Parameters Returns Promise<Result<null, RequestError>>

uploadFromFilename

  • uploadFromFilename(objectName, srcFilename, options?): Promise<Result<null, RequestError>>
Uploads a file from the local filesystem to the bucket. This method overwrites any existing object with the same name. Parameters Returns Promise<Result<null, RequestError>>

uploadFromStream

  • uploadFromStream(objectName, stream, options?): Promise<void>
Uploads an object by reading its contents from the provided stream. The stream emits any errors encountered during the upload. This method overwrites any existing object with the same name. Parameters Returns Promise<void>

uploadFromText

  • uploadFromText(objectName, contents, options?): Promise<Result<null, RequestError>>
Uploads an object using its in-memory text representation. This method overwrites any existing object with the same name. Parameters Returns Promise<Result<null, RequestError>>

Method examples

The following sections provide code examples for managing your files using the Replit Object Storage SDK.

Retrieve an object as text

Retrieve an object as its raw byte representation

Retrieve an object from a stream

Download an object to the filesystem

List objects in the bucket

Upload an object from text

Upload an object as bytes

Upload an object from the filesystem

Upload an object from a stream

Delete an object from the bucket