This is how to upload file to object storage like AWS S3, Minio, and another similarly in NestJS Framework.
I use package nestjs-multer-extended
Install package with yarn or npm
yarn add nestjs-multer-extended
In your module
import { MulterExtendedModule } from 'nestjs-multer-extended';
@Module({
imports: [
MulterExtendedModule.register({
awsConfig: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: process.env.AWS_DEFAULT_REGION,
},
bucket: process.env.AWS_BUCKET,
basePath: 'public',
fileSize: 1 * 1024 * 1024,
endpoint: process.env.AWS_ENDPOINT,
});
],
})
In your controller
@UseInterceptors(AmazonS3FileInterceptor('path', { dynamicPath: 'folder_name_or_root', randomFilename: true }))
@Post()
async create(@UploadedFile() file, @Body() createProductGalleryDto: CreateProductGalleryDto) {
if (file) {
createProductGalleryDto.path = file.key;
}
return await this.productGalleryService.create(createProductGalleryDto);
}
You can save file path into database with name "file.key:
Top comments (0)