diff --git a/flask_fs/backends/s3.py b/flask_fs/backends/s3.py index cb84d7f..9ce63a2 100644 --- a/flask_fs/backends/s3.py +++ b/flask_fs/backends/s3.py @@ -41,10 +41,13 @@ def __init__(self, name, config): aws_secret_access_key=config.secret_key) self.bucket = self.s3.Bucket(name) - try: - self.bucket.create() - except self.s3.meta.client.exceptions.BucketAlreadyOwnedByYou: - pass + if self.bucket.creation_date is None: + # Boto3 does not automatically get the region from the conneciton information + # They use the default US Standard region. + # Ref: https://github.com/boto/boto3/issues/781 + self.bucket.create(CreateBucketConfiguration={ + 'LocationConstraint': self.s3.meta.client.meta.region_name + }) def exists(self, filename): try: diff --git a/tests/test_s3_backend.py b/tests/test_s3_backend.py index 8cb82d9..906d461 100644 --- a/tests/test_s3_backend.py +++ b/tests/test_s3_backend.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals import logging +from datetime import datetime from .test_backend_mixin import BackendTestCase @@ -9,6 +10,8 @@ from flask_fs.storage import Config import boto3 +import hashlib +import six from botocore.exceptions import ClientError @@ -68,6 +71,18 @@ def file_exists(self, filename): except ClientError: return False + def test_metadata(self, app, faker): + content = six.text_type(faker.sentence()) + hasher = getattr(hashlib, self.hasher) + hashed = hasher(content.encode('utf8')).hexdigest() + self.put_file('file.txt', content) + + metadata = self.backend.metadata('file.txt') + assert metadata['checksum'] == '{0}:{1}'.format(self.hasher, hashed) + assert metadata['size'] == len(content) + assert metadata['mime'] == 'application/octet-stream' + assert isinstance(metadata['modified'], datetime) + # def test_root(self): # self.assertEqual(self.backend.root, self.test_dir)