Dependencies : TypeScript / koa-multer / multer-s3
에러 메시지 :
Type 'import("c:/Users/moonq/Desktop/-----/node_modules/@types/multer/index").StorageEngine' is not assignable to type 'import("c:/Users/moonq/Desktop/---------/node_modules/@types/koa-multer/index").StorageEngine'.
Types of property '_handleFile' are incompatible.
Type '(req: Request<ParamsDictionary, any, any, ParsedQs>, file: File, callback: (error?: any, info?: Partial) => void) => void' is not assignable to type '(req: IncomingMessage, file: File, callback: (error?: any, info?: File) => void) => void'.
Types of parameters 'callback' and 'callback' are incompatible.
Types of parameters 'info' and 'info' are incompatible.
Type 'Partial' is not assignable to type 'File'.
Property 'fieldname' is optional in type 'Partial' but required in type 'File'.ts(2322)
index.d.ts(75, 9): The expected type comes from property 'storage' which is declared here on type 'Options'
multer-s3
를 깔면 multer
와 @types/multer
가 깔려버린다. multer-s3가 multer를 바탕으로 만들어져서 어쩔 수 없다. npm i
이후에 @types/multer를 지워버리면 아무런 문제 없이 작동한다. 하지만 매번 지울수도 없는 노릇.
multer-s3의 koa 전용 모듈은 따로 없는 듯하다.
multer-s3
를 쓰지말자. aws-sdk
를 이용해서 업로드하자.multer
도 굳이 안써도 된다. fileStream
을 이용하자.
도움받은 코드:
s3StorageMulti = multerS3({
s3: this.s3,
bucket: this.params.Bucket,
key: function (req, file, cb) {
const state = (req as any).state as UploadContext;
state.files.push({
originalFilename: file.originalname,
storedPath: `${Env.get().UPLOAD_DIR}/${file.originalname}`, // need to change
});
let extension = path.extname(file.originalname);
let basename = path.basename(file.originalname, extension);
cb(null, `images/${basename}${Date.now().toString()}${extension}`);
},
acl: 'public-read-write',
// contentDisposition: 'attachment',
serverSideEncryption: 'AES256'
});
singleUpload: Koa.Middleware = multer({ storage: this.s3StorageSingle }).single('file'); // 이곳이 문제. storage: this.s3StorageSingle
multiUpload: Koa.Middleware = multer({ storage: this.s3StorageMulti }).array('files'); // 이곳이 문제. storage: this.s3StorageMulti