This is a wrapper for Microsoft's azure-storage
package for working with
Azure blobs. It provides a more convenient syntax and async
support.
It is best to cache the files stored in the cloud storage, because the retrieval times are 20-200 ms from within the same region, and 200-600 ms if the data is transferred between regions.
The constructor takes the storage account name (e.g. myaccount
) and the
storage service key that you can find in Azure Portal after creating a
storage account.
These credentials are better stored in the .env
file. Then you can initialize
the service as follows:
const Blobs = require('imago-azure-blobs');
global.blobs = new Blobs(
process.env.AZURE_STORAGE_ACCOUNT, process.env.AZURE_STORAGE_ACCESS_KEY);
The reason to store the blobs service in the global
variable is to
avoid reestablishing the connection to the server each time the service is
accessed from different parts of the code.
Initializes the containers, meaning it checks that the containers are created, or creates them.
The containers
is an object in the following format:
const AZURE_BLOB_STORAGE_CONTAINERS = {
MY_CONTAINER_1: { name: 'my-container-1', type: 'private' },
MY_OTHER_CONTAINER: { name: 'my-other-container', type: 'public' },
};
The key is irrelevant and is for the programmer's convenience when
calling other methods like writeFile
. The name
is the container name in
Azure. The type
can be private
or public
(allows anyone to download files
via a public URL).
Write a file to storage.
Append to a file in storage.
Read a file from storage.
Check if file exists in storage.
Since each operation costs money, in some cases you can simply try to read
a file using readFile()
, and catch an exception if it doesn't exist.
Get a public download URL for a filename. The container type must be set to
public
when it was first created.
Azure Table storage is also supported.
Create tables if they don't exist. tables
is an array of strings (table names).
Read an entity from table.
Save or replace entity with the specified key in database. data
is an object
where each key is a column name.