|
1 | | -# Create a ~/.boto file with: |
| 1 | +# Create a ~/.aws/credentials file with: |
2 | 2 | #[Credentials] |
3 | 3 | #aws_access_key_id = <key> |
4 | 4 | #aws_secret_access_key = <key> |
5 | 5 |
|
6 | 6 | import os |
7 | 7 |
|
8 | | -import boto |
| 8 | +import boto3 |
| 9 | +from botocore.exceptions import ClientError |
9 | 10 |
|
10 | 11 | accepted_file_exts = ['.pdf', '.png', '.gif', '.mp4', '.jpg', '.svg'] |
11 | 12 | BUCKET_NAME = 'mechmotum' |
12 | | -HOST = 'objects-us-east-1.dream.io' |
| 13 | +HOST = 's3.us-east-005.dream.io' |
13 | 14 | assets_dir = 'assets' |
14 | 15 |
|
15 | 16 |
|
16 | 17 | def upload(overwrite=False): |
17 | 18 |
|
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) |
20 | 22 |
|
21 | 23 | files = os.listdir(assets_dir) |
22 | 24 |
|
23 | 25 | for fname in files: |
24 | 26 | 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: |
30 | 31 | 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') |
37 | 42 |
|
38 | 43 |
|
39 | 44 | if __name__ == "__main__": |
|
0 commit comments