Write Buffer to FS and add record to Files collection. This function is asynchronous.
FilesCollection#writeAsync(buffer: Buffer, opts?: WriteOpts, proceedAfterUpload?: boolean): Promise<FileObj>;buffer{Buffer} - File data asBufferopts{object} - Recommended properties:opts.fileName{string} - File name with extension, likename.extopts.type{string} - Mime-type, likeimage/pngopts.size{number} - File size in bytes, if not set file size will be calculated fromBufferopts.meta{object} - Object with custom meta-dataopts.userId{string} - UserId, default nullopts.fileId{string} - id, optional - if not set - Random.id() will be used
proceedAfterUpload{boolean} - ProceedonAfterUploadhook (if defined) afterBufferis written to FS- Returns {Promise} - newly created file's object from DB
import { readFile } from 'node:fs/promises';
import { FilesCollection } from 'meteor/ostrio:files';
const imagesCollection = new FilesCollection({collectionName: 'images'});
try {
const data = await readFile('/data/imgs/sample.png');
const fileRef = await imagesCollection.writeAsync(data, {
fileName: 'sample.png',
fileId: 'abc123myId',
type: 'image/png'
});
console.log(`${fileRef.name} is successfully saved to FS. _id: ${fileRef._id}`);
} catch (error) {
console.error('Failed to save image:', error);
}