Warm tip: This article is reproduced from serverfault.com, please click

node.js-使用MulterGoogleStorage和NestJS删除文件

(node.js - Remove file using MulterGoogleStorage and NestJS)

发布于 2020-11-28 08:41:28

如何使用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}`);
  }
});
Questioner
V.Tur
Viewed
11
Stefan Neacsu 2020-11-30 16:14:27

你可以创建类似这样的内容,它将在包含所有对象的数组上进行迭代并删除。

这使用了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();
    });