AWS-MULTER-S3-SHARP key, config, 세팅 value

 aws: {

    access_key_id: "https://aws.amazon.com/ko/blogs/security/wheres-my-secret-access-key/",
    secret_access_key: "https://aws.amazon.com/ko/blogs/security/wheres-my-secret-access-key/",
    region: "ap-northeast-2",
    bucket: "indj",
  },

Key: 경로/파일이름
ACL: "public-read",


Lib 에 만든 s3용 multer upload
uploadS3.js->
const { config } = require("../../config");
// AWS
const aws = require("aws-sdk");
const multer = require("multer");
const s3Storage = require("multer-sharp-s3");

//const config = require("../../config.json");

const crypto = require("crypto");
const path = require("path");

// aws S3
const s3 = new aws.S3();
aws.config.update({
  secretAccessKey: config.aws.secret_access_key// Not working key, Your SECRET ACCESS KEY from AWS should go here, never share it!!!
  accessKeyId: config.aws.access_key_id// Not working key, Your ACCESS KEY ID from AWS should go here, never share it!!!
  region: config.aws.region// region of your bucket
});

exports.upload = multer({
  storage: s3Storage({
    s3,
    Bucket: config.aws.bucket,
    ACL: "public-read",
    limits: {
      fileSize: 5 * 1024 * 1024,
    },
    Key: (reqfilecb=> {
      cb(
        null,
        `${file.mimetype.split("/")[0]}/${file.fieldname}/${crypto
          .createHash("md5")
          .update(new Date().getTime().toString(36))
          .digest("hex")}${path.extname(file.originalname)}`
      );
    },
    resize: {
      width: 500,
      height: 500,
    },
  }),
  fileFilter: function (reqfilecallback) {
    var ext = path.extname(file.originalname);
    if (ext !== ".png" && ext !== ".jpg" && ext !== ".gif" && ext !== ".jpeg") {
      req.fileValidateError = `png, jpg, gif, jpeg 파일만 가능합니다`;
      return callback(new Error("png, jpg, gif, jpeg 파일만 가능합니다"));
    }
    callback(nulltrue);
  },
});



router->
const mUploadS3 = require("../mUploadS3 loc");
router
  .route("/")
  .post(mUploadS3.upload.single("thumbnail"), async (reqres=> {
    //'thumbnail' 이건 formdata의 칼럼명
    try {
      console.log("## req.file.s3filename: "req.file.s3filename);
      console.log("## req.file.s3folder: "req.file.s3folder);
      res.json({
        s3filename: req.file.s3filename,
        s3folder: req.file.s3folder,
      });
    } catch (e) {
      console.log(e);
      res.status(400).json({ code: 0reason: "bad request" });
    }
  });

댓글

이 블로그의 인기 게시물

[ref game]

[AI Image]