Deta Drive SDK
The Deta library is the easiest way to store and retrieve files from your Deta Drive. Currently, we support JavaScript, Typescript and Python 3. Drop us a line if you want us to support your favorite language.
#
Installing- JavaScript
- Python
- Go
Using Yarn:
#
InstantiatingTo start working with your Drive, you need to import the Deta
class and initialize it with a Project Key. Then instantiate a subclass called Drive
with a database name of your choosing.
- JavaScript
- Python
- Go
note
If you are using Deta Drive within a Deta Micro, you must include deta
in your package.json
file to install the latest sdk version.
A valid project key is pre-set in the Micro's environment. There is no need to pass a key in the initialization step.
note
If you are using the deta
npm package of 0.0.6
or below, Deta
is the single default export and should be imported as such.
note
If you are using Deta Drive within a Deta Micro, you must include deta
in your requirements.txt
file to install the latest sdk version.
A valid project key is pre-set in the Micro's environment. There is no need to pass a key in the the initialization step.
warning
Your project key is confidential and meant to be used by you. Anyone who has your project key can access your database. Please, do not share it or commit it in your code.
#
UsingDeta's Drive
offers the following methods to interact with your Deta Drive:
put
- Stores a file to drive. It will overwrite the file if the file already exists.
get
- Retrieves a file from drive by the file name.
delete
- Deletes a file from drive.
list
- Lists the file names in a drive.
#
PutPut
uploads and stores a file in a drive with a given name
. It will overwrite the file if the file name already exists.
- JavaScript
- Python
- Go
async put(name, options)
#
Parametersname (required) -
string
- Description: The name of the file.
options (required) -
{data : string | Uint8Array | Buffer, path: string, contentType: string}
- Description: An object with three optional parameters.
- data -
string
orBuffer
- Description: Either the data string or a buffer.
- path -
string
- Description: The path of the file to be uploaded to drive.
- contentType -
string
- Description: The content type of the file to be uploaded to drive. If the content type is not provided,
drive
tries to figure out the content type from thename
provided. It defaults toapplication/octet-stream
if the content type can not be figured out from the file name.
- Description: The content type of the file to be uploaded to drive. If the content type is not provided,
- data -
options
must have at least and at most one of two propertiesdata
orpath
defined.- Description: An object with three optional parameters.
#
ReturnsReturns a promise which resolves to the name of the item on a successful put
, otherwise, it throws an Error
on error.
#
Exampleput(name, data, *, path, content_type)
#
Parametersname (required) -
string
- Description: The name of the file.
data -
string | bytes | io.TextIOBase | io.BufferedIOBase | io.RawIOBase
- Description: The data content of the file.
path -
string
- Description: The local path of the file to be uploaded to drive.
content_type -
string
- Description: The content type of the file to be uploaded to drive. If the content type is not provided,
drive
tries to figure out the content type from thename
provided. It defaults toapplication/octet-stream
if the content type can not be figured out from the file name.
At least and at most one of two args
data
orpath
must be provided.path
andcontent_type
must be provided with the key words.- Description: The content type of the file to be uploaded to drive. If the content type is not provided,
#
ReturnsReturns the name of the file on a successful put
, otherwise, raises an Exception
on error.
#
ExamplePut(i *PutInput) (string, error)
#
Parameters- i (required) - pointer to a
PutInput
Name
(required) -string
- Description: Name of the file to be uploaded.
Body
(required) -io.Reader
- Description: File content to be uploaded.
ContentType
-string
- Description: If the content type is not provided, drive tries to figure out the content type from Name provided. It defaults to application/octet-stream if the content type can not be figured out from the file name.
#
ReturnsReturns the name
of the file on a successful put (otherwise empty name), and an error
.
#
Example#
GetGet
retrieves a file from a drive by its name.
- JavaScript
- Python
- Go
async get(name)
#
Parameters- name (required) -
string
- Description: The
name
of the file to get.
- Description: The
#
ReturnsReturns a promise that resolves to a blob
of data if found, else null
.
Throws an Error
on errors.
#
Exampleget(name)
#
Parameters- name (required) -
string
- Description: The
name
of the file to get.
- Description: The
#
ReturnsReturns a instance of a DriveStreamingBody
class which has the following methods/properties.
read(size=None)
: Reads all or up to the nextsize
bytes. Callingread
after all the content has been read will return empty bytes.iter_chunks(chunk_size:int=1024)
: Returns an iterator that yields chunks of bytes ofchunk_size
at a time.iter_lines(chunk_size:int=1024)
: Returns an iterator that yields lines from the stream. Bytes ofchunk_size
at a time is read from the raw stream and lines are yielded from there. The line delimiter is alwaysb'\n'
.close()
: Closes the stream.closed
: ReturnsTrue
if the stream has been closed.
#
Example#
DeleteDelete
deletes a file from drive.
- JavaScript
- Python
- Go
async delete(name)
#
Parameters- name (required) -
string
- Description: The name of the file to delete
#
ReturnsReturns a promise that resolves to the name
of the deleted file on successful deletions, otherwise raises an Error
note
If the file did not exist, the file is still returned as deleted.
#
Example#
Delete ManyDeletes multiple files (up to 1000) from a drive.
- JavaScript
- Python
- Go
async deleteMany(names)
#
Parameters- names (required) -
Array of string
- description: The names of the files to be deleted.
#
ReturnsReturns a promise which resolves to an object with deleted
and failed
keys indicating deleted and failed file names.
note
If a file did not exist, the file is still returned as deleted.
#
Exampledelete_many(names)
#
Parameters- names (required):
string
- Description: The names of the files to be deleted.
#
ReturnsReturns a dict
with deleted
and failed
keys indicating deleted and failed file names.
note
If a file did not exist, the file is still returned as deleted.
#
ExampleDeleteMany(names []string) (*DeleteManyOutput, error)
#
Parameters- names (required):
[]string
- Description: The names of the files to be deleted.
#
ReturnsReturns a pointer to a DeleteManyOutput
and an error
.
Deleted
- string slice indicating deleted file names.Failled
- map indicating the names of failed file names along with an error message.
#
Example#
ListList
files in your drive.
- JavaScript
- Python
- Go
async list(options)
#
Parameters- options (required) :
{prefix: string, limit: number, last: string}
- Description: An object with three optional parameters.
- prefix:
string
- Description: The prefix that file names must have.
- limit:
number
- Description: The maximum number of files names to be returned, defaults to
1000
- Description: The maximum number of files names to be returned, defaults to
- last:
string
- Description: The
last
name seen in a previous paginated result. Providelast
to fetch further pages.
- Description: The
- prefix:
- Description: An object with three optional parameters.
#
ReturnsReturns a promise which resolves to an object
with paging
and names
keys.
names
: The names of the filespaging
: 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.
#
Examplelist(limit, prefix, last)
#
Parameters- limit:
int
- Description: The maximum number of files names to be returned, defaults to
1000
- Description: The maximum number of files names to be returned, defaults to
- prefix:
string
- Description: The prefix that file names must have.
- last:
string
- Description: The
last
name seen in a previous paginated result. Providelast
from previous response to fetch further pages.
- Description: The
#
ReturnsReturns a dict
with paging
and names
keys.
names
: The names of the filespaging
: 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.
#
ExampleList(limit int, prefix, last string) (*ListOutput, error)
#
Parameters- limit (required) -
int
- Description: Maximum number of file names to be returned.
- prefix (required) -
string
- Description: The prefix that file names must have.
- last (required) -
string
- Description: The
last
name seen in a previous paginated result. Providelast
from previous response to fetch further pages.
- Description: The
#
ReturnsReturns a pointer to a ListOutput
Paging
- indicates the size and last name of the current page.nil
if there are no further pages.Names
- names of the files.
#
Example#
IssuesIf you run into any issues, consider reporting them in our Github Discussions.