我有一个 python for 循环,它应该使用 uuid_list
中的每个 uuid,然后转到正确的 AWS S3 存储桶、组织等获取正确的证书和密钥以将消息发送到 IOT主题。
uuid_list = ["cde2107e-8736-47de-9e87-2033c3063589", "7904f39f-97b0-4a6e-bd9d-fa692c40fe6f"]
for uuid in uuid_list:
s3_client = boto3.client('s3')
bucket = 'sensor-certificates'
org = "abc", "def"
object_str = org + '/' + uuid + '/'
result = s3_client.list_objects(Bucket=bucket, Prefix=object_str)
print('-------------------')
print(result)
print('-------------------')
我的问题是,随着规模的扩大,我现在拥有不止一个组织。我试过把它们放在一个列表中,就像上面一样,但我收到了错误
can only concatenate tuple (not "str") to tuple: TypeError
Traceback (most recent call last):
File "/var/task/iot_sim_lambda.py", line 78, in lambda_handler
object_str = org + '/' + uuid + '/'
TypeError: can only concatenate tuple (not "str") to tuple
谁能建议我在尝试获取证书时如何让循环识别多个组织
答案 0 :(得分:0)
以下代码行将为您提供一个元组对象:-
org = "abc", "def"
这里的 org 是一个元组对象,您正试图将它与一个字符串连接起来。只需执行以下操作:-
org = "abc" + "def"
它会给你一个字符串,然后你可以将它与 uuid 连接起来。更新您的代码如下:-
uuid_list = ["cde2107e-8736-47de-9e87-2033c3063589", "7904f39f-97b0-4a6e-bd9d-fa692c40fe6f"]
for uuid in uuid_list:
s3_client = boto3.client('s3')
bucket = 'sensor-certificates'
org = "abc" + "def"
object_str = org + '/' + uuid + '/'
result = s3_client.list_objects(Bucket=bucket, Prefix=object_str)
print('-------------------')
print(result)
print('-------------------')
答案 1 :(得分:0)
您显然需要将 org = "abc", "def"
转换为列表并将其移出循环。
uuid_list = ["cde2107e-8736-47de-9e87-2033c3063589", "7904f39f-97b0-4a6e-bd9d-fa692c40fe6f"]
orgs = ["abc", "def"]
for uuid in uuid_list:
for org in orgs:
s3_client = boto3.client('s3')
bucket = 'sensor-certificates'
object_str = org + '/' + uuid + '/'
result = s3_client.list_objects(Bucket=bucket, Prefix=object_str)
print('-------------------')
print(result)
print('-------------------')
答案 2 :(得分:0)
uuid_list = ["cde2107e-8736-47de-9e87-2033c3063589", "7904f39f-97b0-4a6e-bd9d-
fa692c40fe6f"]
for uuid in uuid_list:
s3_client = boto3.client('s3')
bucket = 'sensor-certificates'
for org in ["abc", "def"]:
object_str = org + '/' + uuid + '/'
result = s3_client.list_objects(Bucket=bucket, Prefix=object_str)
print('-------------------')
print(result)
print('-------------------')