File functions
These functions can be used to work with files.
| Function | Description |
|---|---|
file::bucket() | Returns the bucket path from a file pointer |
file::copy() | Copies the contents of a file |
file::copy_if_not_exists() | Copies the contents of a file to a new file if the name is available |
file::delete() | Deletes a file |
file::exists() | Checks if a file already exists |
file::get() | Loads a file |
file::head() | Returns the metadata of a file |
file::key() | Returns the key (the portion following the bucket) from a file pointer |
file::list() | Returns a list of files inside a bucket |
file::put() | Writes bytes to a file |
file::put_if_not_exists() | Attempts to write bytes to a file |
file::rename() | Renames a file |
file::rename_if_not_exists() | Renames a file if the new name is not already in use |
file::bucket
The file::bucket function returns the name of the bucket in which a file is located.
The counterpart to this function is file::key, which returns the latter part of a file pointer.
file::copy
The file::copy function copies the contents of a file to a new file, overwriting any existing file that has the same name as the new file.
Example of a file my_book.txt being copied to a new location lion_witch_wardrobe.txt:
file::copy_if_not_exists
The file::copy_if_not_exists function copies the contents of a file to a new file, returning an error if a file already exists that has the same name as that of the intended copy.
Example of a file my_book.txt attempting to copy to a new location lion_witch_wardrobe.txt:
file::delete
The file::delete function deletes a file.
Example of a file my_book.txt being deleted:
file::exists
The file::exists function checks to see if a file exists at the path and file name indicated.
Example of an IF else STATEMENT used to check if a file exists before writing content to the location:
file::get
The file::get function retrieves a file for use.
A retrieved file will display as bytes. If valid text, these can be cast into a string.
file::head
The file::head function returns the metadata for a file.
If a file is found, the metadata will be returned as an object with the following fields:
e_data(option<string>): the unique identifier for the file.last_modified(datetime)location(string)size(int)version(option<string>)
An example of this function and its output:
file::key
The file::key function returns the key of a file: the part of a file pointer following the bucket name.
The counterpart to this function is file::bucket, which returns the bucket name of a file pointer.
file::list
The file::list returns the metadata for the files inside a certain bucket. The output is an array of objects, each containing the following fields:
file: the pointer to the file.size(int): the file size in bytes.updated(datetime): the last time a change was made to the file.
To modify the output, a second argument can be passed in that contains a single object with up to three fields:
limit(int): the maximum number of files to display.start(string): displays files ordered afterstart.prefix(string): displays files whose names begin withprefix.
Some examples of the function containing the second object and their responses:
file::put
The file::put function adds data into a file, overwriting any existing data.
An example of this function followed by file::get() to display the contents:
file::put_if_not_exists
The file::put function adds data into a file, unless a file of the same name already exists.
An example of this function followed by file::get() to display the contents:
file::rename
The file::rename function renames a file, overwriting any existing file that has the same name as the target name.
An example of a file being renamed over an existing file:
file::rename_if_not_exists
The file::rename_if_not_exists function renames a file, returning an error if a file already exists that has the same name as the target name.