如何使用MulterGoogleStorage和NestJS从Google存储桶中删除文件?我找不到任何示例或文档。我的下一个存储空间用于上传文件:
const storage = new MulterGoogleStorage({
projectId: 'myprojectId',
keyFilename: path.join(__dirname, '../../../mykeyfile.json'),
bucket: 'mybucketname',
filename: (req: Request, file, cb) => {
let dir = '';
const filePath = file.originalname.split('/');
if(filePath.length > 1) {
dir = `${filePath[0]}/`;
}
const fileExt = file.originalname.split('.').pop();
cb(null, `${dir}${Date.now()}.${fileExt}`);
}
});
你可以创建类似这样的内容,它将在包含所有对象的数组上进行迭代并删除。
这使用了Google Cloud Storage文档中的删除功能。
const storage = new Storage({keyFilename: 'google-credentials.json'});
const imagesToDelete = ['fileName1', 'fileName2', 'fileName3'];
imagesToDelete.map(async (image) => {
await storage
.bucket('yourbucketName')
.file(image)
.delete();
});
非常感谢,它起作用了!我想使用MulterGoogleStorage,但看来您的报价是解决此问题的唯一方法。我将等待一个星期,也许有人会找到其他解决方案,我会接受您的回答。