data engineering

AWS SDK. 코드 예제.

qkqhxla1 2018. 4. 10. 16:52

http://boto3.readthedocs.io/en/latest/guide/examples.html 에 잘 나와있다.


미리 http://qkqhxla1.tistory.com/912 에 적은것처럼 configure 구성을 해줘야 한다.

aws에 처음 가입하면 1년치에 무료로 ec2라던지 s3라던지 일부 무료로 쓸수 있는데 그걸 이용해서 만들어서 테스트를 해보자.


익숙해질 겸 s3버킷을 하나 만들어서 파일을 올리고 다운로드 받는 스크립트를 짜봤다.

import boto3

print 'region =', boto3.session.Session().region_name

s3 = boto3.client('s3')
bucket_name = 'qkqhxla1-hosting-example2'

bucket_list = map(lambda x:x['Name'], s3.list_buckets()['Buckets'])
if bucket_name not in bucket_list:
    s3.create_bucket(Bucket=bucket_name,
                     CreateBucketConfiguration={'LocationConstraint': 'ap-northeast-1'})

file_path = './csvfile/print.txt'
s3.upload_file(file_path, bucket_name, 'print.txt')
s3.download_file(bucket_name, 'print.txt', './conf/print.txt')