dev/개발일지

multer , multer-3, s3, sharp, buffer 이미지 최적화(썸네일)

Box 2021. 7. 22. 22:19
728x90

multer : form_data 로 넘어오는 파일들 받기위해

 

- 위지위그 에디터 썻을때

이미지(url) 로 넘어온 데이터를 s3에 저장 시키기위해

  • 디스크립션 으로 넘어온 여러 이미지중 첫 이미지
  • 정규식으로 이용해 추출
  • Buffer 를 이용하여 이미지를 바이너리식 데이터 추출
  • 데이터를 sharp 에 대입 함
  • 옵션중 toBuffer 를 이용하여 s3 업로드에 필요한 param 작성
  • s3.upload(param) 성공

const axios =  require('axios');
const Buffer = require('buffer/').Buffer;
const sharp = require('sharp');
const s3 = require('../../helpers/s3');

 

const input = Buffer.from((await axios({ url: ${url} , responseType: "arraybuffer" })).data);

sharp(input)

.resize({width:500 , height: 500})

.withMetadata()

.toFormat('png')

.toBuffer(function(err, data){

var param = {

'Bucket':'s3버킷 이름',

'Key': crypto.randomBytes(48).toString('hex'),

'ACL':'public-read',

'Body':data,

'ContentType':'image/png'

}

s3.upload(param,function(err, data){

 

}

S3는  키값이 들어있는 컴포넌트 모듈화 부른것