8000 fix get_store call in current provider · localstack/localstack@6658e77 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6658e77

Browse files
committed
fix get_store call in current provider
1 parent ab0d60d commit 6658e77

File tree

1 file changed

+53
-87
lines changed

1 file changed

+53
-87
lines changed

localstack/services/s3/provider.py

Lines changed: 53 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@
6363
GetObjectOutput,
6464
GetObjectRequest,
6565
GetObjectRetentionOutput,
66-
GetObjectTaggingOutput,
67-
GetObjectTaggingRequest,
6866
HeadObjectOutput,
6967
HeadObjectRequest,
7068
InputSerialization,
@@ -215,8 +213,10 @@ class S3Provider(S3Api, ServiceLifecycleHook):
215213
def get_store(account_id: Optional[str] = None, region: Optional[str] = None) -> S3Store:
216214
return s3_stores[account_id or DEFAULT_AWS_ACCOUNT_ID][region or AWS_REGION_US_EAST_1]
217215

218-
def _clear_bucket_from_store(self, context: RequestContext, bucket: BucketName):
219-
store = self.get_store(context.account_id, context.region)
216+
def _clear_bucket_from_store(
217+
self, bucket_account_id: str, bucket_region: str, bucket: BucketName
218+
):
219+
store = self.get_store(bucket_account_id, bucket_region)
220220
store.bucket_lifecycle_configuration.pop(bucket, None)
221221
store.bucket_versioning_status.pop(bucket, None)
222222
store.bucket_cors.pop(bucket, None)
@@ -330,8 +330,14 @@ def create_bucket(
330330
def delete_bucket(
331331
self, context: RequestContext, bucket: BucketName, expected_bucket_owner: AccountId = None
332332
) -> None:
333+
moto_backend = get_moto_s3_backend(context)
334+
moto_bucket = get_bucket_from_moto(moto_backend, bucket=bucket)
333335
call_moto(context)
334-
self._clear_bucket_from_store(context, bucket)
336+
self._clear_bucket_from_store(
337+
bucket_account_id=moto_bucket.account_id,
338+
bucket_region=moto_bucket.region_name,
339+
bucket=bucket,
340+
)
335341
self._cors_handler.invalidate_cache()
336342

337343
def get_bucket_location(
@@ -645,20 +651,18 @@ def delete_object(
645651
request: DeleteObjectRequest,
646652
) -> DeleteObjectOutput:
647653
# TODO: implement DeleteMarker response
654+
bucket_name = request["Bucket"]
655+
moto_backend = get_moto_s3_backend(context)
656+
moto_bucket = get_bucket_from_moto(moto_backend, bucket=bucket_name)
648657
if request.get("BypassGovernanceRetention") is not None:
649-
bucket_name = request["Bucket"]
650-
moto_backend = get_moto_s3_backend(context)
651-
bucket = get_bucket_from_moto(moto_backend, bucket=bucket_name)
652-
if not bucket.object_lock_enabled:
658+
if not moto_bucket.object_lock_enabled:
653659
raise InvalidArgument(
654660
"x-amz-bypass-governance-retention is only applicable to Object Lock enabled buckets.",
655661
ArgumentName="x-amz-bypass-governance-retention",
656662
)
657663

658-
if (
659-
request["Bucket"]
660-
not in self.get_store(context.account_id, context.region).bucket_notification_configs
661-
):
664+
store = self.get_store(moto_bucket.account_id, moto_bucket.region_name)
665+
if request["Bucket"] not in store.bucket_notification_configs:
662666
return call_moto(context)
663667

664668
# TODO: we do not differentiate between deleting a key and creating a DeleteMarker in a versioned bucket
@@ -759,8 +763,7 @@ def complete_multipart_upload(
759763
)
760764

761765
bucket_name = request["Bucket"]
762-
moto_backend = get_moto_s3_backend(context)
763-
moto_bucket = get_bucket_from_moto(moto_backend, bucket_name)
766+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket_name)
764767
if not (upload_id := request.get("UploadId")) in moto_bucket.multiparts:
765768
raise NoSuchUpload(
766769
"The specified upload does not exist. The upload ID may be invalid, or the upload may have been aborted or completed.",
@@ -778,7 +781,7 @@ def complete_multipart_upload(
778781
def upload_part(self, context: RequestContext, request: UploadPartRequest) -> UploadPartOutput:
779782
bucket_name = request["Bucket"]
780783
moto_backend = get_moto_s3_backend(context)
781-
moto_bucket = get_bucket_from_moto(moto_backend, bucket_name)
784+
moto_bucket = get_bucket_from_moto(moto_backend, bucket=bucket_name)
782785
if not (upload_id := request.get("UploadId")) in moto_bucket.multiparts:
783786
raise NoSuchUpload(
784787
"The specified upload does not exist. The upload ID may be invalid, or the upload may have been aborted or completed.",
@@ -873,22 +876,6 @@ def list_multipart_uploads(
873876

874877
return response
875878

876-
@handler("GetObjectTagging", expand=False)
877-
def get_object_tagging(
878-
self, context: RequestContext, request: GetObjectTaggingRequest
879-
) -> GetObjectTaggingOutput:
880-
response: GetObjectTaggingOutput = call_moto(context)
881-
# FIXME: because of an issue with the serializer, we cannot return the VersionId for now
882-
# the specs give a GetObjectTaggingOutput but the real return tag is `Tagging` which already exists as a shape
883-
# we can't add the VersionId for now
884-
if (
885-
"VersionId" in response
886-
and request["Bucket"]
887-
not in self.get_store(context.account_id, context.region).bucket_versioning_status
888-
):
889-
response.pop("VersionId")
890-
return response
891-
892879
@handler("PutObjectTagging", expand=False)
893880
def put_object_tagging(
894881
self, context: RequestContext, request: PutObjectTaggingRequest
@@ -1312,27 +1299,26 @@ def put_bucket_notification_configuration(
13121299
self._verify_notification_configuration(
13131300
notification_configuration, skip_destination_validation, context, bucket
13141301
)
1315-
self.get_store(context.account_id, context.region).bucket_notification_configs[
1316-
bucket
1317-
] = notification_configuration
1302+
moto_backend = get_moto_s3_backend(context)
1303+
moto_bucket = get_bucket_from_moto(moto_backend, bucket=bucket)
1304+
store = self.get_store(moto_bucket.account_id, moto_bucket.region_name)
1305+
store.bucket_notification_configs[bucket] = notification_configuration
13181306

13191307
def get_bucket_notification_configuration(
13201308
self, context: RequestContext, bucket: BucketName, expected_bucket_owner: AccountId = None
13211309
) -> NotificationConfiguration:
13221310
# TODO how to verify expected_bucket_owner
13231311
# check if the bucket exists
1324-
get_bucket_from_moto(get_moto_s3_backend(context), bucket=bucket)
1325-
return self.get_store(context.account_id, context.region).bucket_notification_configs.get(
1326-
bucket, NotificationConfiguration()
1327-
)
1312+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket=bucket)
1313+
store = self.get_store(moto_bucket.account_id, moto_bucket.region_name)
1314+
return store.bucket_notification_configs.get(bucket, NotificationConfiguration())
13281315

13291316
def get_bucket_website(
13301317
self, context: RequestContext, bucket: BucketName, expected_bucket_owner: AccountId = None
13311318
) -> GetBucketWebsiteOutput:
13321319
# to check if the bucket exists
13331320
# TODO: simplify this when we don't use moto
1334-
moto_backend = get_moto_s3_backend(context)
1335-
moto_bucket = get_bucket_from_moto(moto_backend, bucket)
1321+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket=bucket)
13361322
store = self.get_store(moto_bucket.account_id, moto_bucket.region_name)
13371323
if not (website_configuration := store.bucket_website_configuration.get(bucket)):
13381324
ex = NoSuchWebsiteConfiguration(
@@ -1354,8 +1340,7 @@ def put_bucket_website(
13541340
) -> None:
13551341
# to check if the bucket exists
13561342
# TODO: simplify this when we don't use moto
1357-
moto_backend = get_moto_s3_backend(context)
1358-
moto_bucket = get_bucket_from_moto(moto_backend, bucket)
1343+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket)
13591344

13601345
validate_website_configuration(website_configuration)
13611346
store = self.get_store(moto_bucket.account_id, moto_bucket.region_name)
@@ -1366,12 +1351,10 @@ def delete_bucket_website(
13661351
) -> None:
13671352
# to check if the bucket exists
13681353
# TODO: simplify this when we don't use moto
1369-
moto_backend = get_moto_s3_backend(context)
1370-
get_bucket_from_moto(moto_backend, bucket)
1354+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket=bucket)
1355+
store = self.get_store(moto_bucket.account_id, moto_bucket.region_name)
13711356
# does not raise error if the bucket did not have a config, will simply return
1372-
self.get_store(context.account_id, context.region).bucket_website_configuration.pop(
1373-
bucket, None
1374-
)
1357+
store.bucket_website_configuration.pop(bucket, None)
13751358

13761359
def post_object(
13771360
self, context: RequestContext, bucket: BucketName, body: IO[Body] = None
@@ -1397,11 +1380,9 @@ def post_object(
13971380
if "${filename}" in key_name:
13981381
key_name = key_name.replace("${filename}", context.request.files["file"].filename)
13991382

1400-
moto_backend = get_moto_s3_backend(context)
14011383
# TODO: add concept of VersionId
1402-
key = get_key_from_moto_bucket(
1403-
get_bucket_from_moto(moto_backend, bucket=bucket), key=key_name
1404-
)
1384+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket=bucket)
1385+
key = get_key_from_moto_bucket(moto_bucket, key=key_name)
14051386
# hacky way to set the etag in the headers as well: two locations for one value
14061387
response["ETagHeader"] = key.etag
14071388

@@ -1423,7 +1404,8 @@ def post_object(
14231404
"LocationHeader", f"{get_full_default_bucket_location(bucket)}{key_name}"
14241405
)
14251406

1426-
if bucket in self.get_store(context.account_id, context.region).bucket_versioning_status:
1407+
store = self.get_store(moto_bucket.account_id, moto_bucket.region_name)
1408+
if bucket in store.bucket_versioning_status:
14271409
response["VersionId"] = key.version_id
14281410

14291411
self._notify(context, key_name=key_name)
@@ -1444,11 +1426,10 @@ def get_object_attributes(
14441426
request: GetObjectAttributesRequest,
14451427
) -> GetObjectAttributesOutput:
14461428
bucket_name = request["Bucket"]
1447-
moto_backend = get_moto_s3_backend(context)
1448-
bucket = get_bucket_from_moto(moto_backend, bucket_name)
1429+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket_name)
14491430
# TODO: rework the delete marker handling
14501431
key = get_key_from_moto_bucket(
1451-
moto_bucket=bucket,
1432+
moto_bucket=moto_bucket,
14521433
key=request["Key"],
14531434
version_id=request.get("VersionId"),
14541435
raise_if_delete_marker_method="GET",
@@ -1470,10 +1451,8 @@ def get_object_attributes(
14701451

14711452
response["LastModified"] = key.last_modified
14721453

1473-
if (
1474-
bucket_name
1475-
in self.get_store(context.account_id, context.region).bucket_versioning_status
1476-
):
1454+
store = self.get_store(moto_bucket.account_id, moto_bucket.region_name)
1455+
if bucket_name in store.bucket_versioning_status:
14771456
response["VersionId"] = key.version_id
14781457

14791458
if key.multipart:
@@ -1491,8 +1470,7 @@ def put_bucket_analytics_configuration(
14911470
analytics_configuration: AnalyticsConfiguration,
14921471
expected_bucket_owner: AccountId = None,
14931472
) -> None:
1494-
moto_backend = get_moto_s3_backend(context)
1495-
moto_bucket = get_bucket_from_moto(moto_backend, bucket)
1473+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket)
14961474
store = self.get_store(moto_bucket.account_id, moto_bucket.region_name)
14971475

14981476
validate_bucket_analytics_configuration(
@@ -1511,8 +1489,7 @@ def get_bucket_analytics_configuration(
15111489
id: AnalyticsId,
15121490
expected_bucket_owner: AccountId = None,
15131491
) -> GetBucketAnalyticsConfigurationOutput:
1514-
moto_backend = get_moto_s3_backend(context)
1515-
moto_bucket = get_bucket_from_moto(moto_backend, bucket)
1492+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket)
15161493
store = self.get_store(moto_bucket.account_id, moto_bucket.region_name)
15171494

15181495
analytics_configuration: AnalyticsConfiguration = store.bucket_analytics_configuration.get(
@@ -1529,8 +1506,7 @@ def list_bucket_analytics_configurations(
15291506
continuation_token: Token = None,
15301507
expected_bucket_owner: AccountId = None,
15311508
) -> ListBucketAnalyticsConfigurationsOutput:
1532-
moto_backend = get_moto_s3_backend(context)
1533-
moto_bucket = get_bucket_from_moto(moto_backend, bucket)
1509+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket)
15341510
store = self.get_store(moto_bucket.account_id, moto_bucket.region_name)
15351511

15361512
analytics_configurations: Dict[
@@ -1550,8 +1526,7 @@ def delete_bucket_analytics_configuration(
15501526
id: AnalyticsId,
15511527
expected_bucket_owner: AccountId = None,
15521528
) -> None:
1553-
moto_backend = get_moto_s3_backend(context)
1554-
moto_bucket = get_bucket_from_moto(moto_backend, bucket)
1529+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket)
15551530
store = self.get_store(moto_bucket.account_id, moto_bucket.region_name)
15561531

15571532
analytics_configurations = store.bucket_analytics_configuration.get(bucket, {})
@@ -1565,8 +1540,7 @@ def put_bucket_intelligent_tiering_configuration(
15651540
id: IntelligentTieringId,
15661541
intelligent_tiering_configuration: IntelligentTieringConfiguration,
15671542
) -> None:
1568-
moto_backend = get_moto_s3_backend(context)
1569-
moto_bucket = get_bucket_from_moto(moto_backend, bucket)
1543+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket)
15701544

15711545
validate_bucket_intelligent_tiering_configuration(id, intelligent_tiering_configuration)
15721546
store = self.get_store(moto_bucket.account_id, moto_bucket.region_name)
@@ -1578,8 +1552,7 @@ def put_bucket_intelligent_tiering_configuration(
15781552
def get_bucket_intelligent_tiering_configuration(
15791553
self, context: RequestContext, bucket: BucketName, id: IntelligentTieringId
15801554
) -> GetBucketIntelligentTieringConfigurationOutput:
1581-
moto_backend = get_moto_s3_backend(context)
1582-
moto_bucket = get_bucket_from_moto(moto_backend, bucket)
1555+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket)
15831556
store = self.get_store(moto_bucket.account_id, moto_bucket.region_name)
15841557

15851558
intelligent_tiering_configuration: IntelligentTieringConfiguration = (
@@ -1594,8 +1567,7 @@ def get_bucket_intelligent_tiering_configuration(
15941567
def delete_bucket_intelligent_tiering_configuration(
15951568
self, context: RequestContext, bucket: BucketName, id: IntelligentTieringId
15961569
) -> None:
1597-
moto_backend = get_moto_s3_backend(context)
1598-
moto_bucket = get_bucket_from_moto(moto_backend, bucket)
1570+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket)
15991571
store = self.get_store(moto_bucket.account_id, moto_bucket.region_name)
16001572

16011573
bucket_intelligent_tiering_configurations = (
@@ -1607,8 +1579,7 @@ def delete_bucket_intelligent_tiering_configuration(
16071579
def list_bucket_intelligent_tiering_configurations(
16081580
self, context: RequestContext, bucket: BucketName, continuation_token: Token = None
16091581
) -> ListBucketIntelligentTieringConfigurationsOutput:
1610-
moto_backend = get_moto_s3_backend(context)
1611-
moto_bucket = get_bucket_from_moto(moto_backend, bucket)
1582+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket)
16121583
store = self.get_store(moto_bucket.account_id, moto_bucket.region_name)
16131584

16141585
bucket_intelligent_tiering_configurations: Dict[
@@ -1633,7 +1604,7 @@ def put_bucket_logging(
16331604
expected_bucket_owner: AccountId = None,
16341605
) -> None:
16351606
moto_backend = get_moto_s3_backend(context)
1636-
moto_bucket = get_bucket_from_moto(moto_backend, bucket)
1607+
moto_bucket = get_bucket_from_moto(moto_backend, bucket=bucket)
16371608

16381609
if not (logging_config := bucket_logging_status.get("LoggingEnabled")):
16391610
moto_bucket.logging = {}
@@ -1665,8 +1636,7 @@ def put_bucket_logging(
16651636
def get_bucket_logging(
16661637
self, context: RequestContext, bucket: BucketName, expected_bucket_owner: AccountId = None
16671638
) -> GetBucketLoggingOutput:
1668-
moto_backend = get_moto_s3_backend(context)
1669-
moto_bucket = get_bucket_from_moto(moto_backend, bucket)
1639+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket)
16701640
if not moto_bucket.logging:
16711641
return GetBucketLoggingOutput()
16721642

@@ -1721,8 +1691,7 @@ def put_bucket_inventory_configuration(
17211691
inventory_configuration: InventoryConfiguration,
17221692
expected_bucket_owner: AccountId = None,
17231693
) -> None:
1724-
moto_backend = get_moto_s3_backend(context)
1725-
moto_bucket = get_bucket_from_moto(moto_backend, bucket)
1694+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket)
17261695

17271696
validate_inventory_configuration(
17281697
config_id=id, inventory_configuration=inventory_configuration
@@ -1739,8 +1708,7 @@ def get_bucket_inventory_configuration(
17391708
id: InventoryId,
17401709
expected_bucket_owner: AccountId = None,
17411710
) -> GetBucketInventoryConfigurationOutput:
1742-
moto_backend = get_moto_s3_backend(context)
1743-
moto_bucket = get_bucket_from_moto(moto_backend, bucket)
1711+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket)
17441712
store = self.get_store(moto_bucket.account_id, moto_bucket.region_name)
17451713

17461714
inventory_configuration = store.bucket_inventory_configurations.get(bucket, {}).get(id)
@@ -1755,8 +1723,7 @@ def list_bucket_inventory_configurations(
17551723
continuation_token: Token = None,
17561724
expected_bucket_owner: AccountId = None,
17571725
) -> ListBucketInventoryConfigurationsOutput:
1758-
moto_backend = get_moto_s3_backend(context)
1759-
moto_bucket = get_bucket_from_moto(moto_backend, bucket)
1726+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket)
17601727
store = self.get_store(moto_bucket.account_id, moto_bucket.region_name)
17611728

17621729
bucket_inventory_configurations = store.bucket_inventory_configurations.get(bucket, {})
@@ -1775,8 +1742,7 @@ def delete_bucket_inventory_configuration(
17751742
id: InventoryId,
17761743
expected_bucket_owner: AccountId = None,
17771744
) -> None:
1778-
moto_backend = get_moto_s3_backend(context)
1779-
moto_bucket = get_bucket_from_moto(moto_backend, bucket)
1745+
moto_bucket = get_bucket_from_moto(get_moto_s3_backend(context), bucket)
17801746
store = self.get_store(moto_bucket.account_id, moto_bucket.region_name)
17811747

17821748
bucket_inventory_configurations = store.bucket_inventory_configurations.get(bucket, {})

0 commit comments

Comments
 (0)
0