nestjs / azure-storage

Azure Storage module for Nest framework (node.js) ☁️

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add custom filename on upload

manekinekko opened this issue · comments

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

When uploading a file to Storage, we use the original filename.

Expected behavior

When uploading a file to Storage, we can provide a custom filename.

What is the motivation / use case for changing the behavior?

Users might want to have a custom logic when it comes to filename, or an internal pattern naming convention...etc

Notes

We should easily change this line so it can accept an external filename https://github.com/nestjs/azure-storage/blob/master/lib/azure-storage.service.ts#L116

It turns out that we can already do this with the current implementation, here is how:

 @Post('azure/upload')
 @UseInterceptors(FileInterceptor('file'))
 async UploadedFilesUsingService(
   @UploadedFile()
   file: UploadedFileMetadata,
 ) {
   file = {
     ...file,
     originalname: 'foo-bar.txt',
   };
   const storageUrl = await this.azureStorage.upload(file, {
     containerName: 'nest-demo-container-service',
   });
   Logger.log(`Storage URL: ${storageUrl}`, 'AppController');
 }```

I will add this to the readme.

How do you specify the folder though? I tried uploading a file with the name "folder/filename.ext" and it created a file literally named "folder/filename.ext" instead of the folder "folder" and the filename.ext inside.

I am sorry. Folder creation is not supported yet. Would you mind creating a different issue or a feature request to track this?