-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathupload2s3.py
55 lines (42 loc) · 1.67 KB
/
upload2s3.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
51
52
53
54
55
#!/usr/bin/env python
import boto3
import os
import fnmatch
import time
from os import environ
from botocore.exceptions import ClientError
def get_aws_creds():
aws_properties = {}
if "AWS_KEY_ID" in environ and "AWS_SECRET_ACCESS_KEY" in environ:
aws_properties['aws_access_key_id'] = environ['AWS_KEY_ID']
aws_properties['aws_secret_access_key'] = environ['AWS_SECRET_ACCESS_KEY']
else:
try:
with open('aws.properties') as property_file:
for line in property_file:
if '=' in line:
name, value = line.split('=', 1)
aws_properties[name.strip()] = value.strip()
except IOError as e:
print(f"I/O error reading aws.properties: {e.errno}, {e.strerror}")
print("text2speech script does not have AWS credentials.")
return aws_properties
def upload_file_to_s3(s3_client, filename, bucket, prefix=None):
if prefix is not None:
full_path = str(prefix) + "/" + filename
else:
full_path = filename
try:
response = s3_client.upload_file(filename, bucket, full_path)
except ClientError as e:
print("An error occurred uploading " + filename)
print(e)
print(response)
creds = get_aws_creds()
prefix = int(time.time())
s3_client = boto3.client('s3', aws_access_key_id=creds['aws_access_key_id'], aws_secret_access_key=creds['aws_secret_access_key'])
current_directory = os.listdir('.')
mp3_file_pattern = "*.mp3"
for mp3_file in current_directory:
if fnmatch.fnmatch(mp3_file, mp3_file_pattern):
upload_file_to_s3(s3_client, mp3_file, 'your-valid-bucket-name', prefix)