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

Add s3_bucket_exists on storage backend #1

Open
wants to merge 3 commits into
base: s3-improve-get-bucket
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
5 changes: 3 additions & 2 deletions storage_backend/models/storage_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ class StorageBackend(models.Model):
help="Relative path to the directory to store the file"
)
has_validation = fields.Boolean(compute="_compute_has_validation")

validated = fields.Boolean(string="Validated",compute="_compute_has_validation")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this? It should be a normal stored field, not computed :/


def _compute_has_validation(self):
for rec in self:
adapter = self._get_adapter()
rec.has_validation = hasattr(adapter, "validate_config")
rec.validated = rec.has_validation = hasattr(adapter, "validate_config")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls rollback this


@property
def _server_env_fields(self):
Expand Down
22 changes: 11 additions & 11 deletions storage_backend_s3/components/s3_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
_logger.debug(err)


# Keep track of existing buckets and avoid checking for them every time.
EXISTING_BUCKETS = []


class S3StorageAdapter(Component):
_name = "s3.adapter"
_inherit = "base.storage.adapter"
Expand All @@ -50,6 +46,7 @@ def _get_bucket(self):
s3 = boto3.resource("s3", **params)
bucket_name = self.collection.aws_bucket
bucket = self._get_or_create_bucket(s3, bucket_name, **params)
self.collection.validated = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this writes on EVERY lookup for the bucket, pls drop it

return bucket

def _get_or_create_bucket(self, s3, bucket_name, **params):
Expand All @@ -60,19 +57,17 @@ def _get_or_create_bucket(self, s3, bucket_name, **params):
else:
create_params = self._get_create_bucket_params(bucket_name, params)
bucket = s3.create_bucket(**create_params)
self.collection.validated = True
return bucket

def _check_bucket_exists(self, s3, bucket_name):
if bucket_name in EXISTING_BUCKETS:
# If the bucket is not available later
# we'll have an error on each call of course
# but that does not happen often
# and in any case you will get what's wrong soon.
def _check_bucket_exists(self, s3, bucket_name, force=False):
if force is False and self.collection.validated:
return True
# if test is ok, set the flag
try:
s3.meta.client.head_bucket(Bucket=bucket_name)
# The call above is expensive, avoid it when possible.
EXISTING_BUCKETS.append(bucket_name)
self.collection.validated = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are writing on this a second time because is done already on line 148.
Drop this and rely only on the flag that this method should return and update the flag only in validate_config

return True
except ClientError as e:
# If a client error is thrown, then check that it was a 404 error.
Expand Down Expand Up @@ -146,3 +141,8 @@ def list(self, relative_path):
def delete(self, relative_path):
s3object = self._get_object(relative_path)
s3object.delete()

def validate_config(self, **params):
s3 = boto3.resource("s3", **params)
bucket_name = self.collection.aws_bucket
self.validated = self._check_bucket_exists(s3, bucket_name)
1 change: 1 addition & 0 deletions storage_backend_s3/views/backend_storage_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
name="aws_bucket"
attrs="{'required': [('backend_type', '=', 'amazon_s3')]}"
/>
<field name="validated" />
<field name="aws_cache_control" />
<field name="aws_file_acl" />
</group>
Expand Down