diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2018-12-04 21:12:59 +0100 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2018-12-04 21:12:59 +0100 |
| commit | d69086a1b2d7d6e6def55f35e30d0623701de011 (patch) | |
| tree | 1f73899aa4bcb9ecf0600f0d95f5909c79818780 /builder/s3.py | |
| parent | 966e27c7418d6e188ea4b1f651a5e6c67495b765 (diff) | |
embedding images
Diffstat (limited to 'builder/s3.py')
| -rw-r--r-- | builder/s3.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/builder/s3.py b/builder/s3.py new file mode 100644 index 00000000..7d4d52a0 --- /dev/null +++ b/builder/s3.py @@ -0,0 +1,55 @@ +import os +import glob +import boto3 +from paths import * + +session = boto3.session.Session() + +s3_client = session.client( + service_name='s3', + aws_access_key_id=os.getenv('S3_KEY'), + aws_secret_access_key=os.getenv('S3_SECRET'), + endpoint_url=os.getenv('S3_ENDPOINT'), + region_name=os.getenv('S3_REGION'), +) + +def sync_directory(base_fn, s3_path, metadata): + fns = {} + for fn in glob.glob(os.path.join(base_fn, 'assets/*')): + fns[os.path.basename(fn)] = True + + remote_path = s3_path + metadata['url'] + + directory = s3_client.list_objects(Bucket=os.getenv('S3_BUCKET'), Prefix=remote_path) + prefixes = [] + + if 'Contents' in directory: + for obj in directory['Contents']: + s3_fn = obj['Key'] + fn = os.path.basename(s3_fn) + local_fn = os.path.join(base_fn, 'assets', fn) + if fn in fns: + del fns[fn] + if obj['LastModified'].timestamp() < os.path.getmtime(os.path.join(local_fn)): + print("s3 update {}".format(s3_fn)) + client.upload_file( + local_fn, + os.getenv('S3_BUCKET'), + s3_fn, + ExtraArgs={ 'ACL': 'public-read' }) + else: + print("s3 delete {}".format(s3_fn)) + response = client.delete_object( + Bucket=os.getenv('S3_BUCKET'), + Key=s3_fn, + ) + + for fn in fns: + local_fn = os.path.join(base_fn, 'assets', fn) + s3_fn = os.path.join(remote_path, 'assets', fn) + print("s3 create {}".format(s3_fn)) + s3_client.upload_file( + local_fn, + os.getenv('S3_BUCKET'), + s3_fn, + ExtraArgs={ 'ACL': 'public-read' }) |
