진지한 개발자

특정 이름의 S3 파일을 지우고 싶을 때 본문

IT/Python

특정 이름의 S3 파일을 지우고 싶을 때

제이_엔 2023. 6. 9. 15:37
728x90
  • 특정 이름을 가진 S3 파일 리스트 필터링
import boto3

s3 = boto3.resource('s3')
bucket = s3.Bucket('bucket-name')
     

for file_key in file_list:
    print(file_key)
    s3.Object('s3-an2-op-hlp-etl', file_key).delete()
  • 필터링 된 S3 리스트 내 파일 삭제
import boto3

sess = boto3.Session()

s3 = sess.resource('s3')
operation_bucket = s3.Bucket('bucket-name')
file_list = []
for file in operation_bucket.objects.filter(Prefix=f'test_prefix/'):
    if 'filtered_string' in str(file):
        file_list.append(file._key) 
len(file_list)
728x90

'IT > Python' 카테고리의 다른 글

unzip  (0) 2023.08.26
S3 file 지우기  (0) 2023.06.29
Convert GroupBy Series to DataFrame  (0) 2023.06.05
pip 설치 시 SSLError 오류 해결방법 (SSLCertVerificationError)  (0) 2023.05.22
Python 유용한 코드  (0) 2023.05.20