The Edge Functions Storage API is an interface that allows access to the Edge Storage. Through this interface, it’s possible to read and write data in the storage and its buckets.
Class instantiation
To leverage this API, you need to import and instantiate the Storage class, passing the bucket’s name.
Syntax
Import:
Instantiate:
Parameters
Parameter
Type
Description
bucket
string
Name of a bucket.
Return value
Storage object used to access Edge Storage.
Methods
async storage.put
The Storage.put method is used to insert a new object into the storage.
Syntax
Parameters
Parameter
Type
Description
key
string
Identifier that allows the search for an object in the storage
value
ArrayBuffer ou ReadableStream
Content of the object being stored. In case value implements a ReadableStream, the Stream APIs can be used, and the option content-lenght is required
options
object
The options attributes are described in the table below
options
To pass the options parameter, you must provide an object with the following attributes:
Attribute
Type
Description
content-type
string
The content-type of the object being created. It’s similar to the HTTP Header content-type and describes how the content of the bucket should be treated. content-type is optional. The default value is application/octet-stream
content-length
string
The size of the object to be created, in bytes. It’s mandatory when the value is ReadableStream
metadata
object
Any JavaScript object containing information about the object that will be stored in the Storage. All properties of the object must be strings
Return
Success
Error
No response
Throws a StorageError
Code sample
To persist the request body:
To persist a JSON object:
To save a JSON with metadata:
async storage.get(key)
The storage.get(key) method is used to retrieve an object.
Syntax
Parameters
Parameter
Type
Description
key
string
Identifier that allows the search for an object in the bucket
Return
Success
Error
Returns an object of the class StorageObject
Throws a StorageError
Code sample
To retrieve and return content from the storage:
To get a JSON from the storage:
To read an object’s metadata:
async storage.delete(key)
The Storage.delete(key) is used to remove an object from Edge Storage.
Syntax
Parameters
Parameter
Type
Description
key
string
Identifier that allows the search for an object in the bucket
Return
Success
Error
No response
Throws a StorageError
Code sample
To remove an object stored in Edge Storage related to a specific bucket:
async storage.list()
The Storage.list() method is used to list all objects belonging to the specific bucket.
Syntax
Return
Success
Error
Returns an object of the class StorageObjectList
It throws a StorageError
Code sample
Listing objects from Azion Storage:
StorageObject Class Methods
Method
Description
StorageObject.content
Read-only property containing a ReadableStream for the content stored in the storage
async StorageObject.arrayBuffer()
Asynchronous function that returns an ArrayBuffer with the content stored in the storage. This method consumes the ReadableStream from the content property
StorageObject.metadata
Read-only property containing a Map object with the metadata of the object stored in the storage
StorageObject.contentType
Read-only property containing the content-type of the content stored in the storage
StorageObject.contentLength
Read-only property containing the size in bytes of the content stored in the storage