V.Tur
V.Tur

Reputation: 1427

Remove file using MulterGoogleStorage and NestJS

How to remove file from Google storage bucket using MulterGoogleStorage and NestJS? I can't find any example or docs. I have next storage for upload files:

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}`);
  }
});

Upvotes: 0

Views: 1194

Answers (1)

Stefan Neacsu
Stefan Neacsu

Reputation: 693

You can create something like this, which would iterate over an array containing all the objects, and would delete.

This uses the delete function on the Google Cloud Storage documentation.

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

Upvotes: 2

Related Questions