S3 서비스

Amazon Q Developer를 사용해 S3 서비스를 위한 코드를 생성합니다.


예시 #1

버킷 생성

일반적인 프롬프트

Given a string bucket_name
and an AWS session object
Then Create a bucket with the given name
and return the bucket object if successful
otherwise return None

Python 프롬프트

VSCode에서 파일을 생성하고 이름을 s3.py로 지정하고 다음 코드를 입력합니다.

"""
Given a string bucket_name
and an AWS session object
Then Create a bucket with the given name
and return the bucket object if successful
otherwise return None

"""
Amazon Q Developer 제안
import boto3

def create_bucket(bucket_name, session):
    """
    :param bucket_name:
    :param session:
    :return:
    """
    s3 = session.resource('s3')
    try:
        bucket = s3.create_bucket(Bucket=bucket_name)
        return bucket
    except Exception as e:
        print(e)
        return None

예시 #2

버킷 리스트 가져오기

일반적인 프롬프트

Given an AWS session object
Then return a list of all buckets in the account if successful
otherwise return None

Python 프롬프트

VSCode에서 파일을 생성하고 이름을 s3.py로 지정하고 다음 코드를 입력합니다.

"""
Given an AWS session object
Then return a list of all buckets in the account if successful
otherwise return None
"""
Amazon Q Developer 제안
def list_buckets(session):
    """
    :param session:
    :return:
    """
    s3 = session.resource('s3')
    try:
        return s3.buckets.all()
    except Exception as e:
        print(e)
        return None

예시 #3

재귀적 삭제

일반적인 프롬프트

Given a bucket Name 
And an AWS session object
Then recursively get list of all folders in the bucket
and delete all versions of objects folders
and delete the folders
and delete the bucket
and return True if successful
otherwise return False

Python 프롬프트

VSCode에서 파일을 생성하고 이름을 s3.py로 지정하고 다음 코드를 입력합니다.

"""
Given a bucket Name 
And an AWS session object
Then recursively get list of all folders in the bucket
and delete all versions of objects folders
and delete the folders
and delete the bucket
and return True if successful
otherwise return False
"""
Amazon Q Developer 제안
def delete_bucket(bucket_name, session):
    """
    :param bucket_name:
    :param session:
    :return:
    """
    s3 = session.resource('s3')
    bucket = s3.Bucket(bucket_name)
    try:
        bucket.object_versions.delete()
        bucket.delete()
        return True
    except Exception as e:
        print(e)
        return False

예시 #4

버킷 존재 여부

일반적인 프롬프트

Given a bucket Name 
Then create a client object for s3
And Check if the bucket exists
and return True if it exists
otherwise return False

Python 프롬프트

VSCode에서 파일을 생성하고 이름을 s3.py로 지정하고 다음 코드를 입력합니다.

"""
Given a bucket Name 
Then create a client object for s3
And Check if the bucket exists
and return True if it exists
otherwise return False
"""
Amazon Q Developer 제안
import boto3

def check_bucket_exists(bucket_name):
    """
    :param bucket_name:
    :return:
    """
    s3 = boto3.client('s3')
    try:
        s3.head_bucket(Bucket=bucket_name)
        return True
    except Exception as e:
        print(e)
        return False

Last updated