-
Notifications
You must be signed in to change notification settings - Fork 0
/
boto3-S3.py
50 lines (41 loc) · 1.38 KB
/
boto3-S3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#%%
import boto3
from botocore.exceptions import ClientError
# IAM 유저 생성 후 받은 키 입력
my_id = "YOUR_ID"
my_key = "YOUR_KEY"
#%%
def create_s3_bucket(bucket_name):
print("Creating a bucket... " + bucket_name)
s3 = boto3.client(
's3', # 사용할 서비스 이름, ec2이면 'ec2', s3이면 's3', dynamodb이면 'dynamodb'
aws_access_key_id=my_id, # 액세스 ID
aws_secret_access_key=my_key) # 비밀 엑세스 키
try:
response = s3.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={
'LocationConstraint': 'ap-northeast-2' #Seoul # us-east-1을 제외한 지역은 LocationConstraint 명시해야함.
}
)
return response
except ClientError as e:
if e.response['Error']['Code'] == 'BucketAlreadyOwnedByYou':
print("Bucket already exists. skipping..")
else:
print("Unknown error, exit..")
#%%
response = create_s3_bucket("BUCKET_NAME_YOU_WANT")
print("Bucket : " + str(response))
# %%
# 파일 올리기
import os
import glob
os.getcwd()
input_path = "G:/내 드라이브/"
files = glob.glob(os.path.join(input_path,'(before)*'))
stored_names = list(map(lambda x: x.split("\\")[1], files))
# %%
for file,name in zip(files,stored_names):
s3.upload_file(file,"BUCKET_NAME_YOU_WANT",name)
# %%