Boto3列出了这样无用的桶:
{'Buckets': [{'CreationDate': datetime.datetime(1, 1, 1, 0, 0, tzinfo=tzlocal()),
'Name': 'foo'},
{'CreationDate': datetime.datetime(1, 1, 1, 0, 0, tzinfo=tzlocal()),
'Name': 'bar'},
{'CreationDate': datetime.datetime(2024, 7, 30, 15, 4, 59, 150000, tzinfo=tzlocal()),
'Name': 'baz'}],
'Owner': {...}}
如果我想知道是否有桶
foo
在列表的开头,我可以写下:
match s3_response:
case {"Buckets": [{"Name": "foo", "CreationDate": _}, *_]}:
print("Bucket 'foo' found")
case _:
print("Bucket 'foo' not found")
如果知道水桶在最后,我可以想出一个类似的解决方案。
但是找桶呢
bar
哪个在存储桶列表的中间?你可能希望你能做到这一点:
match s3_response:
case {"Buckets": [*_, {"Name": "bar", "CreationDate": _}, *_]}:
print("Bucket 'bar' found")
case _:
print("Bucket 'bar' not found")
但这给出了:
SyntaxError:序列模式中的多个星号名称
当然,我可以用无聊的老方法做事,但这有什么乐趣呢:
'bar' in [bucket.get("Name", "") for bucket in s3_response.get("Buckets", [])]