Skip to content

Commit ecd315e

Browse files
committed
Updated to boto3 and the new dreamobjects location.
1 parent 60fe2cf commit ecd315e

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

dreamobject.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,44 @@
1-
# Create a ~/.boto file with:
1+
# Create a ~/.aws/credentials file with:
22
#[Credentials]
33
#aws_access_key_id = <key>
44
#aws_secret_access_key = <key>
55

66
import os
77

8-
import boto
8+
import boto3
9+
from botocore.exceptions import ClientError
910

1011
accepted_file_exts = ['.pdf', '.png', '.gif', '.mp4', '.jpg', '.svg']
1112
BUCKET_NAME = 'mechmotum'
12-
HOST = 'objects-us-east-1.dream.io'
13+
HOST = 's3.us-east-005.dream.io'
1314
assets_dir = 'assets'
1415

1516

1617
def upload(overwrite=False):
1718

18-
conn = boto.connect_s3(host=HOST)
19-
bucket = conn.get_bucket(BUCKET_NAME)
19+
session = boto3.session.Session()
20+
s3 = session.resource('s3', endpoint_url=f'https://{HOST}')
21+
bucket = s3.Bucket(BUCKET_NAME)
2022

2123
files = os.listdir(assets_dir)
2224

2325
for fname in files:
2426
if os.path.splitext(fname)[-1] in accepted_file_exts:
25-
key = boto.s3.key.Key(bucket, fname)
26-
if key.exists() and overwrite is False:
27-
msg = 'Skipping: {} (already present in the bucket)'
28-
print(msg.format(fname))
29-
else:
27+
s3_object = bucket.Object(fname)
28+
try:
29+
s3_object.last_modified
30+
except ClientError:
3031
print('Uploading: {}'.format(fname))
31-
key.set_contents_from_filename(os.path.join(assets_dir, fname))
32-
else:
33-
print('Skipping: {}'.format(fname))
34-
35-
for o in bucket.list():
36-
o.set_acl('public-read')
32+
s3_object.upload_file(os.path.join(assets_dir, fname))
33+
s3_object.Acl().put(ACL='public-read')
34+
else:
35+
if overwrite is False:
36+
msg = 'Skipping: {} (already present in the bucket)'
37+
print(msg.format(fname))
38+
else:
39+
print('Overwriting, uploading: {}'.format(fname))
40+
s3_object.upload_file(os.path.join(assets_dir, fname))
41+
s3_object.Acl().put(ACL='public-read')
3742

3843

3944
if __name__ == "__main__":

0 commit comments

Comments
 (0)