Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

s3: Use connection region to create bucket #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions flask_fs/backends/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_s3_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
from __future__ import unicode_literals

import logging
from datetime import datetime

from .test_backend_mixin import BackendTestCase

from flask_fs.backends.s3 import S3Backend
from flask_fs.storage import Config

import boto3
import hashlib
import six

from botocore.exceptions import ClientError

Expand Down Expand Up @@ -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)

Expand Down