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.
No async implementation yet.
function Drive(driveName: string, host?: string);
The Drive
class provides methods to interact with a Deta Drive.
Parameter | Type | Description |
---|---|---|
driveName | string | The name of the Drive. |
host? | string | The host of the Drive. |
func New(d *Deta, driveName string) (*Drive, error)
The Drive
class provides methods to interact with a Deta Drive.
Parameter | Type | Description |
---|---|---|
d | *Deta | A pointer to a Deta instance. |
driveName | string | The name of the Drive. |
Returns a pointer to a Drive
instance.
def Drive(name: str, host: str | None = None):
The Drive
class provides methods to interact with a Deta Drive.
Parameter | Type | Description |
---|---|---|
name | str | The name of the Drive. |
host | str | None | The host of the Drive. |
Put
Uploads and stores a file with a given name. It will overwrite the file if the name already exists.
No async implementation yet.
func (d *Drive) Put(i *PutInput) (string, error)
Parameter | Type | Description |
---|---|---|
i | *PutInput | A pointer to a PutInput . |
Returns the name of the file on a successful put (otherwise empty name), and an error
.
Example:
name, err := drv.Put(&drive.PutInput{ Name: "hello.txt", Body: bufio.NewReader(file), ContentType: "text/plain",})
async function put( name: string, options: PutOptions,): Promise<PutResponse>;
Parameter | Type | Description |
---|---|---|
name | string | The name of the file. |
options | PutOptions | An 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" });
def put( name: str, data: str | bytes | io.TextIOBase | io.BufferedIOBase | io.RawIOBase, path: str, content_type: str | None = None,) -> str:
Parameter | Type | Description |
---|---|---|
name | str | The name of the file. |
data | str | bytes | io.TextIOBase | io.BufferedIOBase | io.RawIOBase | The data content of the file. |
path | str | The local path of the file to be uploaded to drive. |
content_type | str | None | Content type of the file. If not provided, Drive tries to figure out the content type from the name . Defaults to application/octet-stream if it can not be determined. |
You must pass either data
or path
, but not both.
If name
is not a non-empty string, raises a ValueError
.
If neither or both data
and path
are passed, raises a ValueError
.
Returns an str
containing the name of the file.
If the operation did not complete successfully, raises an Exception
.
Example:
drive.put("hello.txt", data="Hello world!")drive.put("example.txt", path="path/to/example.txt")
Get
Retrieves a file by its name.
No async implementation yet.
func (d *Drive) Get(name string) (io.ReadCloser, error)
Parameter | Type | Description |
---|---|---|
name | string | The name of the file to get. |
Returns an io.ReadCloser
for the file, and an error
.
Example:
file, err := drv.Get("hello.txt")
async function get(name: string): Promise<GetResponse>;
Parameter | Type | Description |
---|---|---|
name | string | The 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");
def get(name: str) -> DriveStreamingBody | None:
Parameter | Type | Description |
---|---|---|
name | str | The name of the file to get. |
Returns an instance of DriveStreamingBody
.
If name
is not a non-empty string, raises a ValueError
.
Example:
response = drive.get("hello.txt")content = response.read()
Delete
Deletes a file by its name.
No async implementation yet.
func (d *Drive) Delete(name string) (string, error)
Parameter | Type | Description |
---|---|---|
name | string | The name of the file to delete. |
Returns the name of the deleted file on successful deletions, and an error
.
ℹ️ If the file did not exist, the name is still returned as if it were deleted.
Example:
name, err := drv.Delete("hello.txt")
async function delete(name: string): Promise<DeleteResponse>;
Parameter | Type | Description |
---|---|---|
name | string | The 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");
def delete(name: str) -> str:
Parameter | Type | Description |
---|---|---|
name | str | The name of the file to delete. |
Returns an str
containing the name of the deleted file.
If name
is not a non-empty string, raises a ValueError
.
If the operation did not complete successfully, raises an Exception
.
ℹ️ If the file did not exist, the name is still returned as if it were deleted.
Example:
drive.delete("hello.txt")
Delete Many
Deletes multiple files, up to a limit of 1000.
No async implementation yet.
async function deleteMany(names: string[]): Promise<DeleteManyResponse>;
Parameter | Type | Description |
---|---|---|
names | string[] | 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"]);
def delete_many(names: Sequence[str]) -> dict:
Parameter | Type | Description |
---|---|---|
names | Sequence[str] | The names of the files to delete. |
Returns a dict
with "deleted"
and "failed"
keys indicating deleted and failed file names.
ℹ️ If the file did not exist, the name is still returned as if it were deleted.
Example:
drive.delete_many(["hello.txt", "example.txt"])
func (d *Drive) DeleteMany(names []string) (*DeleteManyOutput, error)
Parameter | Type | Description |
---|---|---|
names | []string | The names of the files to delete. |
Returns a pointer to a DeleteManyOutput
, and an error
.
ℹ️ If the file did not exist, the name is still returned as if it were deleted.
Example:
names := []string{"hello.txt", "example.txt"}response, err := drv.DeleteMany(names)
List
Lists file names.
No async implementation yet.
func (d *Drive) List(limit int, prefix string, last string) (*ListOutput, error)
Parameter | Type | Description |
---|---|---|
limit | int | The maximum number of file names to be returned. |
prefix | string | A prefix that file names must start with to be returned. |
last | string | The last name seen in a previous paginated result. |
Returns a pointer to a ListOutput
, and an error
.
Example:
response, err := drv.List(1000, "", "")
async function list(options?: ListOptions): Promise<ListResponse>;
Parameter | Type | Description |
---|---|---|
options | ListOptions | An object with three optional parameters. |
Returns a promise which resolves to a ListRepsonse
.
Example:
const names = await drive.list().names;
def list( limit: int = 1000, prefix: str | None = None, last: str | None = None,) -> dict[str, Any]:
Parameter | Type | Description |
---|---|---|
limit | int | The maximum number of file names to be returned, defaults to 1000 . |
prefix | str | None | A prefix that file names must start with to be returned. |
last | str | None | The last name seen in a previous paginated result. |
Returns a dict
with "paging"
and "names"
keys.
"names"
: The names of the files."paging"
: Contains paging information."size"
: The number of files returned."last"
: The last name seen in the paginated response. Provide this value in subsequent api calls to fetch further pages. For the last page,last
is not present in the response.
Example:
response = 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
@propertydef closed() -> bool:
Returns True
if the stream has been closed.