使用 MulterGoogleStorage 和 NestJS 删除文件

Remove file using MulterGoogleStorage and NestJS

如何使用 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 云存储文档中的 delete 函数。

const storage = new Storage({keyFilename: 'google-credentials.json'});
const imagesToDelete = ['fileName1', 'fileName2', 'fileName3'];
    
    imagesToDelete.map(async (image) => {
      await storage
        .bucket('yourbucketName')
        .file(image)
        .delete();
    });