최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday

티스토리 뷰

S3 Deleting multiple objects by python 3

단일파일 삭제와 여러개 파일 삭제는 아래처럼 차이가 있다. 주의할 점은 속성명과 대소문자 구분이다.

 

단일 파일 삭제

s3.delete_object(
  Bucket="MyBucketName",
  Key="test/test (2).jpg"
 )

멀티 파일 삭제

s3.delete_objects(
  Bucket="MyBucketName",
  Delete = {
    'Objects': [{'Key': '1/test1.jpg', 'Key': '2/test2.jpg'}]
  }
)

 

 

실제 코드

S3 설정파일을 직접 소스코드에 입력하거나 json 등으로 S3에 대한 설정을 먼저 한다.

 

config.json

{
  "endpoint_url" : "https://object.mycloudstorage.com",
  "region_name" : "kr-standard",
  "access_key" : "my-access-key",
  "secret_key" : "my-secret-key"
}

 

delete.py

# -*- coding: utf-8 -*-

import pymysql
import json
import boto3
from datetime import datetime, timedelta
import inspect, os
import sys

#시작시간
program_path = inspect.getfile(inspect.currentframe())
file_name = program_path.split("\\")[-1]
start_time = datetime.now()
print(f"[{program_path}] ========== {start_time}")

#S3연결
#NCP CONFIG READ
with open("./config.json") as json_file:
  json_data = json.load(json_file)
  

#S3 연결정보
service_name = 's3'
endpoint_url = f"{json_data['endpoint_url']}"
region_name = f"{json_data['region_name']}"
access_key = f"{json_data['access_key']}"
secret_key = f"{json_data['secret_key']}"

s3 = boto3.client(service_name, endpoint_url=endpoint_url, aws_access_key_id=access_key, aws_secret_access_key=secret_key)
bucket_name = "myBucket"

# 여러 파일들을 한번에 삭제한다
  response = s3.delete_objects(
    Bucket=bucket_name,
    Delete = {
      'Objects':[
		{Key: "1/afasdlfkjasdlkfj.jpg"},
		{Key: "2/afasdlfkjasdlkfj.jpg"},
		{Key: "3/afasdlfkjasdlkfj.jpg"}
	  ]
    }
  )
  print(response)
  
  # success
  if( 200 == response['ResponseMetadata']['HTTPStatusCode'] ):
    #S3 에서 성공적으로 삭제되었다
    print(f"[{program_path}] 파일이 성공적으로 삭제되었습니다")
  
print(f"[{program_path}] ========== END")
sys.exit()

 

 

 

 

 

레퍼런스

 

docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObjects.html

 

docs.aws.amazon.com/AmazonS3/latest/dev/DeletingMultipleObjects.html

 

DeleteObjects - Amazon Simple Storage Service

DeleteObjects This operation enables you to delete multiple objects from a bucket using a single HTTP request. If you know the object keys that you want to delete, then this operation provides a suitable alternative to sending individual delete requests, r

docs.aws.amazon.com

stackoverflow.com/questions/52361964/delete-multiple-images-from-aws/52362055

 

Delete multiple images from aws

Here is the code I am using for deleting the multiple images from aws AWS.config.update({ accessKeyId: process.env.ACCESS_KEY, secretAccessKey: process.env.SECRET_ACCESS_KEY }) const s3 = new...

stackoverflow.com

 

stackoverflow.com/questions/52361964/delete-multiple-images-from-aws/52362055

 

Delete multiple images from aws

Here is the code I am using for deleting the multiple images from aws AWS.config.update({ accessKeyId: process.env.ACCESS_KEY, secretAccessKey: process.env.SECRET_ACCESS_KEY }) const s3 = new...

stackoverflow.com

 

 

stackoverflow.com/questions/52361964/delete-multiple-images-from-aws/52362055

댓글