Drive

Drive Class

The Drive class provides methods to interact with a Deta Drive. If you have not yet instantiated the Deta Object, you must do so before you can use the Drive Class.

function Drive(driveName: string, host?: string);

The Drive class provides methods to interact with a Deta Drive.

ParameterTypeDescription
driveNamestringThe name of the Drive.
host?stringThe host of the Drive.

Put

Uploads and stores a file with a given name. It will overwrite the file if the name already exists.

async function put(
name: string,
options: PutOptions,
): Promise<PutResponse>;
ParameterTypeDescription
namestringThe name of the file.
optionsPutOptionsAn object with three optional parameters.

options must have exactly one of two properties data or path defined.

data is a Blob or ArrayBuffer of the file data.

path is a string of the path to the file on the local filesystem.

Returns a promise which resolves to a PutResponse containing the name of the item. If the operation did not complete successfully, throws an Error.

Example:

await drive.put("hello.txt", { data: "Hello world!" });
await drive.put("example.txt", { path: "path/to/example.txt" });

Get

Retrieves a file by its name.

async function get(name: string): Promise<GetResponse>;
ParameterTypeDescription
namestringThe name of the file to get.

Returns a promise that resolves to a GetResponse containing a Blob of the data if found, otherwise null. If the operation did not complete successfully, throws an Error.

Example:

const response = await drive.get("hello.txt");

Delete

Deletes a file by its name.

async function delete(name: string): Promise<DeleteResponse>;
ParameterTypeDescription
namestringThe name of the file to delete.

Returns a promise that resolves to a DeleteResponse containing the name of the deleted file. If the operation did not complete successfully, throws an Error.

ℹ️ If the file did not exist, the name is still returned as if it were deleted.

Example:

await drive.delete("hello.txt");

Delete Many

Deletes multiple files, up to a limit of 1000.

async function deleteMany(names: string[]): Promise<DeleteManyResponse>;
ParameterTypeDescription
namesstring[]The names of the files to delete.

Returns a promise which resolves to a DeleteManyResponse.

ℹ️ If the file did not exist, the name is still returned as if it were deleted.

Example:

await drive.deleteMany(["hello.txt", "example.txt"]);

List

Lists file names.

async function list(options?: ListOptions): Promise<ListResponse>;
ParameterTypeDescription
optionsListOptionsAn object with three optional parameters.

Returns a promise which resolves to a ListRepsonse.

Example:

const names = await drive.list().names;

DriveStreamingBody

The DriveStreamingBody class provides methods to read the content of a file from a Drive (Python only).

Read

def read(size: int | None = None) -> bytes:

Reads all or up to the next size bytes. Calling read after all the content has been read will return empty bytes.

Iterate Chunks

def iter_chunks(chunk_size: int = 1024) -> Iterator[bytes]:

Returns an iterator that yields chunks of bytes of chunk_size at a time.

Iterate Lines

def iter_lines(chunk_size: int = 1024) -> Iterator[bytes]:

Returns an iterator that yields lines from the stream. Bytes of chunk_size at a time is read from the raw stream and lines are yielded from there. The line delimiter is always b'\n'.

Close

def close():

Closes the stream.

Closed

@property
def closed() -> bool:

Returns True if the stream has been closed.