-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: s3-improve-get-bucket
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
|
||
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls rollback this |
||
|
||
@property | ||
def _server_env_fields(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
return True | ||
except ClientError as e: | ||
# If a client error is thrown, then check that it was a 404 error. | ||
|
@@ -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) |
There was a problem hiding this comment.
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 :/