brunch

9. 파이선용 SDK for S3 API

by Master Seo

# S3 API로 버킷 만들고 파일을 올려보자.


1

참고자료

https://guide.ncloud-docs.com/docs/storage-storage-8-2




2

로키 리눅스 1대 생성


yum install pip




2

SDK 설치

pip install boto3==1.6.19



3

access-key , secret-key 준비



4

버킷 생성

vi make.py


import boto3

service_name = 's3'

endpoint_url = 'https://kr.object.ncloudstorage.com'

region_name = 'kr-standard'

access_key = 'ACCESS_KEY'

secret_key = 'SECRET_KEY'

if __name__ == "__main__":

s3 = boto3.client(service_name, endpoint_url=endpoint_url, aws_access_key_id=access_key,

aws_secret_access_key=secret_key)

bucket_name = 'sample-bucket-07-16-07'

s3.create_bucket(Bucket=bucket_name)



5

# python3 make.py


콘솔에서 버킷 생성 확인





6

파일 업로드

vi up.py


import boto3

service_name = 's3'

endpoint_url = 'https://kr.object.ncloudstorage.com'

region_name = 'kr-standard'

access_key = 'ACCESS_KEY'

secret_key = 'SECRET_KEY'

if __name__ == "__main__":

s3 = boto3.client(service_name, endpoint_url=endpoint_url, aws_access_key_id=access_key,

aws_secret_access_key=secret_key)

bucket_name = 'sample-bucket'

# create folder

object_name = 'sample-folder/'

s3.put_object(Bucket=bucket_name, Key=object_name)

# upload file

object_name = 'sample-object'

local_file_path = '/tmp/test.txt'

s3.upload_file(local_file_path, bucket_name, object_name)



7

[root@s107113338 conf]# touch /tmp/test.txt



8

[root@s107113338 conf]# python3 up.py



9

버킷 리스트

vi list.py



[root@s107113338 conf]# python3 list.py

material222

sample-bucket-07-16-1

sample-bucket-07-16-2

sample-bucket-07-16-3

sample-bucket-07-16-7

sample-bucket-12321

sample-bucket-1234321

sample-bucket-20211

sample-bucket-test7777

[root@s107113338 conf]#




10

버킷 삭제

CLI로 삭제시 = 버킷안에 파일이 없어야 한다.

콘솔로 삭제시는 파일도 같이 삭제 된다.


python3 del.py




다음

https://brunch.co.kr/@topasvga/4733


keyword
매거진의 이전글8. 어플리케이션 로드 밸런서