Assets Provider
The Assets provider allows you to store and retrieve objects on the Vonage Cloud Runtime platform. You can generate links to your public files, with TTLs for secure files, or download the binary.
Functions
createDir- this allows you to create a directory ahead of uploading files. See the code snippet here.uploadFiles- this allows you to upload files from your project to the Vonage Cloud Runtime platform for long-term storage. Accepts an optionalretentionparameter to set file retention period. See the code snippet here.uploadData- this allows you to upload in-memory data buffers directly without writing to disk first. Accepts an optionalretentionparameter. See the code snippet here.remove- this allows you to remove a file or directory that you have previously uploaded or created. See the code snippet here.list- this allows you to list files and directories that are available in your project. See the code snippet here.generateLink- this allows you to generate a link to a file that is publicly accessible. See the code snippet here.getRemoteFile- this allows you to download a file you have previously uploaded in its binary/raw form. See the code snippet here.
File Retention Periods
Both uploadFiles and uploadData accept an optional retention parameter to control how long a file is stored. If omitted, the platform default retention applies. Import the FILE_RETENTION_PERIOD enum from the SDK:
import { Assets, FILE_RETENTION_PERIOD } from '@vonage/vcr-sdk';
| Value | Duration |
|---|---|
FILE_RETENTION_PERIOD.ONE_DAY | 1 day |
FILE_RETENTION_PERIOD.SEVEN_DAYS | 7 days |
FILE_RETENTION_PERIOD.ONE_MONTH | 1 month |
FILE_RETENTION_PERIOD.THREE_MONTHS | 3 months |
FILE_RETENTION_PERIOD.SIX_MONTHS | 6 months |
FILE_RETENTION_PERIOD.ONE_YEAR | 1 year |
Initializing the Assets Provider
To use the Assets Provider you need to create an instance of the provider using a session:
const session = vcr.createSession();
const assets = new Assets(session);
Usage
For example to use the Assets provider to upload a file from a HTML form using busboy:
app.post('/upload', async (req, res, next) => {
try {
const bb = busboy({ headers: req.headers });
var filePath;
bb.on('file', (name, file, info) => {
filePath = path.join(os.tmpdir(), `image.png`);
file.pipe(fs.createWriteStream(filePath));
});
bb.on('close', async function() {
const session = vcr.createSession();
const assets = new Assets(session);
await assets.uploadFiles([filePath], '/imgs');
res.end();
});
req.pipe(bb);
} catch (error) {
next(error);
}
});
Then to retrieve the file:
const session = vcr.createSession();
const assets = new Assets(session);
const file = await assets.getRemoteFile('/imgs/image.png');
Or to generate a link to the file:
const session = vcr.createSession();
const assets = new Assets(session);
const fileData = await assets.generateLink('/imgs/image.png');