본문 바로가기

AWS

s3 deleteObject

S3에서 이미지나 다른 정적 파일들을 보관하는 경우, 삭제할 필요성이 있다.

선언

const aws = require("aws-sdk")
const {AWS_ACCESS_KEY , AWS_SECRET_KEY}  = process.env


const s3 = new aws.S3({

    accessKeyId:AWS_ACCESS_KEY,
    secretAccessKey:AWS_SECRET_KEY  
})


module.exports = {s3}

단일 오브젝트 삭제

s3.deleteObject({Bucket:"image-upload2" , Key:`raw/${image.key}`} ,  //aws에서 파일삭제
 (err, data) => {
      if (err) { throw err; }
      console.log('s3 deleteObject ', data)
})

여러 오브젝트 삭제

var params = {
  Bucket: "examplebucket", 
  Delete: {
   Objects: [
      {
     Key: "HappyFace.jpg", 
     VersionId: "2LWg7lQLnY41.maGB5Z6SWW.dcq0vx7b"
    }, 
      {
     Key: "HappyFace.jpg", 
     VersionId: "yoz3HB.ZhCS_tKVEmIOr7qYyyAaZSKVd"
    }
   ], 
   Quiet: false
  }
 };
 s3.deleteObjects(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
   /*
   data = {
    Deleted: [
       {
      Key: "HappyFace.jpg", 
      VersionId: "yoz3HB.ZhCS_tKVEmIOr7qYyyAaZSKVd"
     }, 
       {
      Key: "HappyFace.jpg", 
      VersionId: "2LWg7lQLnY41.maGB5Z6SWW.dcq0vx7b"
     }
    ]
   }
   */
 });

'AWS' 카테고리의 다른 글

S3 presignedUrl 생성 , presignedUrl이용해서 S3,DB에 이미지저장  (0) 2022.03.10
multer-s3  (0) 2022.03.09
IAM 유저생성  (0) 2022.03.05
S3+CloudFront  (0) 2022.03.04
S3  (0) 2022.03.04