core-cloud-services-clients v1.0.5
Cloud-Services-Clients
Intro
This npm package is used to access is cloud service like storage, vision etc. Your services will be able to access your third-party web services using one single client.
Installation
$ npm install --save core-cloud-services-clients
Sample usage
const { storage, ocr } = require('core-cloud-services-clients');
Downloader
Create an instance of the Downloader to get a single file from the bucket
const downloader = new storage.Downloader({
bucketName: 'name', // required
localDestinationPath: '/path/to/localfile', // required
remoteFilepath: '/bucketFolder/filename', // required
});
await downloader.download();
// file is downloaded to local destination path
Full Bucket Downloader
Create an instance of the BucketDownloader to get a promise of all files from a bucket.
const downloader = new storage.BucketDownloader({
bucketName: 'name', // required
});
try {
const filesArray = await downloader.downloadAll();
// do something with files
} catch (e) {
// handle error
}
You can also filter the bucket content using a prefix and a delimiter. The delimiter argument can be used to restrict the results to only the "files" in the given "folder". Without the delimiter, the entire tree under the prefix is returned.
e.g.- given:
/public/file1.txt
/public/subfolder/file2.txt
If you just specify prefix = '/public', you'll get back:
/public/file1.txt
/public/subfolder/file2.txt
However, if you specify prefix='/public' and delimiter='/', you'll get back:
/public/file1.txt
const downloader = new storage.BucketDownloader({
bucketName: 'name', // required
prefix: '/public', // required
delimiter: '/subfolder' // optional
});
try {
const filesArray = await downloader.downloadAllFiltered();
// do something with files
} catch (e) {
// handle error
}
Uploader
Create an instance of the Uploader to upload a single file to the bucket. The uploaded file is private and not accessible without the bucket access credentials.
const uploader = new storage.Uploader({
bucketName: 'name', // required
localFilepath: '/path/to/localfile', // required
remoteFilepath: '/bucketFolder/filename', // required
});
const { filepath, isPrivate } = await uploader.upload(); // private
To upload a file with public access use the uploadAnPublish
method instead
const {
filepath,
isPrivate,
} = await uploader.uploadAndPublish(); // public