A utility to wrap file access to local, Google Cloud Storage and AWS S3 file storage.
Add the parent package to your dependencies:
yarn add @swrlab/utilsWe like to create one local file in a project that provides this initialized utility:
// load node util
const Storage = require('@swrlab/utils/packages/storage-wrapper')
// load custom keys and storage
const storage = new Storage({
gs: {
projectId: 'my-project-id',
},
s3: { region: 'eu-west-1' },
logging: true,
})
// export everything
module.exports = storageThe gs object is the same, as one can provide to the Google Cloud Storage utility. We usually only set the projectId explicitly. If needed you can provide more details like keyFilename or set credentials using GOOGLE_APPLICATION_CREDENTIALS env.
For AWS the s3 also provides access to configure the aws-sdk library. It has multiple options to load credentials, all documented in AWS: Configuring the SDK for JavaScript. We sometimes also set the credentials asynchronously using Google Cloud Secret Manager:
const storage = new Storage({
gs: {
projectId: 'my-project-id',
},
s3: { region: 'eu-west-1' },
logging: true,
})
secrets.get('radiohub-ingest-aws').then(({ json }) => {
storage.sdk.s3.config.update(json)
})The logging flag controls whether all file actions by this utility should be logged.
bucket(required)path(required)
Create a URI (e.g. gs://my-bucket/my-file.txt) from bucket and path names.
Works with Google Cloud Storage (gs):
const fileUri = storage.createUri.gs(bucket, name)
// gs://bucket/nameAnd s3:
const fileUri = storage.createUri.s3(bucket, name)
// s3://bucket/nameuri(required) - Source URI to use for signingttl(required) - Time to live in milliseconds for public link, provide as integer or shortform (e.g.60e3for60000)logPrefix(optional) - Prefix for log lines
Use this to sign a URI into a (publicly) accessible URL. Currently only implemented for Google Cloud Storage:
const sourceUrl = await storage.createUrl(fileUri, 60e3)uri(required) - Source URI to deletelogPrefix(optional) - Prefix for log lines
Delete an existing file:
await storage.delete(fileUri)The proper connector is detected using the prefixes (s3://, gs://, http[s]:// or local file). Actions against http[s]:// are rejected.
uri(required) - Source URI prefix to listmax(required) - Rough maximum of elements expectednext(optional) - Next token if available (AWS only)logPrefix(optional) - Prefix for log lines
List all elements matching a prefix, which can be a folder name or folder + fileprefix. Google Cloud provides a full list of elements:
const list = await storage.list(fileUriPrefix, 300)AWS also returns a next token if available
const { list, next } = await storage.list(fileUriPrefix, 300)The utility keeps listing more elements until either max is reached or no more items are available. Please note that AWS and GCP provide lists in batches, the maximum of elements might be exceeded.
uri(required) - Source URI to loadlogPrefix(optional) - Prefix for log linesoptions(optional) - Prefix for log linestimeout(optional) - Timeout for http[s] requests
Download a single file and returns the contents buffer.
const fileBuffer = await storage.list(fileUri)sourceUri(required) - Source file URI to copydestinationUri(required) - Destination URI where to copy the file tokeepOriginal(required) - Boolean whether to keep the sourcelogPrefix(optional) - Prefix for log lines
Moving provides easy access to copy and move actions between multiple storage types and providers. Switching between copying (non-destructive) and moving (deletes the source) is handled via the keepOriginal flag.
There are essentially three ways to move files:
gstogs- Use the Google Cloud's native Storage option to move or copy a files3tos3- Use the AWS method for copying and deleting an objectanytoany- This uses the internal methods toload,saveanddeletethe files. Please note that this actually downloads the file (incurring bandwidth), rather than using the cloud provider's native methods for moving data.
uri(required) - Destination URI to use for savingbuffer(required) - File buffer to savelogPrefix(optional) - Prefix for log linesresumable(optional) - Set Google Cloud transfer asresumable
Provide a URI and file buffer to save a file.
await storage.save(fileUri, myBuffer)