Open wisekaa03 opened 2 years ago
Something like that:
Main difference:
inject: [getS3ConnectionToken('')]
Full working example:
// your-cool.module.ts
import { S3Module, S3, getS3ConnectionToken } from 'nestjs-s3';
import { MulterModule } from '@nestjs/platform-express';
import { customAlphabet } from 'nanoid';
import { nolookalikes } from 'nanoid-dictionary';
import multerS3 from 'multer-s3';
import mime from 'mime-types';
const nanoId = customAlphabet(nolookalikes, 37);
const S3_BUCKET_NAME = 'your-s3-bucket';
// ...
@Module({
imports: [
MulterModule.registerAsync({
imports: [S3Module],
inject: [getS3ConnectionToken('')],
useFactory: (s3Client: S3) => ({
storage: multerS3({
s3: s3Client,
bucket: S3_BUCKET_NAME,
acl: 'public-read',
metadata: function (req, file, cb) {
/*
file: {
fieldname: 'file',
originalname: '123.jpg',
encoding: '7bit',
mimetype: 'image/jpeg'
},
*/
cb(null, {
fileName: file.originalname,
uploadedAt: new Date().toJSON(),
});
},
key: function (req, file, cb) {
const ext = mime.extension(file.mimetype);
const fileName = `uploaded-tmp-files/${nanoId()}.${ext}`;
cb(null, fileName);
},
contentType: function (req, file, cb) {
cb(null, file.mimetype);
},
cacheControl: `max-age=${60 * 60}`, // 1 Hour
}),
}),
}),
],
controllers: [/* ... */],
providers: [/* ... */],
})
export class YourCoolModule {}
Error: resove dependencies :)