Bucket creation needed correct region to be set.

That resulted in non-idempotent create_bucket in non us east 1
location, so needed to check for bucket existence first.
This commit is contained in:
Adam Shimali
2016-02-02 22:26:49 +00:00
parent dd07a00e68
commit c05a4b2059
3 changed files with 23 additions and 6 deletions

View File

@@ -1,18 +1,32 @@
import botocore
from boto3 import resource
def s3upload(upload_id, service_id, filedata):
def s3upload(upload_id, service_id, filedata, region):
s3 = resource('s3')
bucket_name = 'service-{}-{}-notify'.format(service_id, upload_id)
s3.create_bucket(Bucket=bucket_name)
bucket_name = 'service-{}-notify'.format(service_id)
contents = '\n'.join(filedata['data'])
bucket = s3.Bucket(bucket_name)
exists = True
try:
s3.meta.client.head_bucket(Bucket=bucket_name)
except botocore.exceptions.ClientError as e:
error_code = int(e.response['Error']['Code'])
if error_code == 404:
exists = False
if not exists:
s3.create_bucket(Bucket=bucket_name,
CreateBucketConfiguration={'LocationConstraint': region})
key = s3.Object(bucket_name, upload_id)
key.put(Body=contents, ServerSideEncryption='AES256')
def s3download(service_id, upload_id):
s3 = resource('s3')
bucket_name = 'service-{}-{}-notify'.format(service_id, upload_id)
bucket_name = 'service-{}-notify'.format(service_id)
key = s3.Object(bucket_name, upload_id)
contents = key.get()['Body'].read().decode('utf-8')
return contents