我正在嘗試列出存盤桶,并選擇使用 CLI 選擇其中一個存盤桶來上傳檔案。
該代碼的輸出始終是“您想要選擇哪個存盤桶”和 1 個存盤桶名稱,然后在我按 Enter 鍵之前,它會列出“哪個存盤桶..”和新存盤桶。我需要一個串列以及選擇其中一個來運行的方法,將一個物件放在該存盤桶上。
我的代碼:
s3 = boto3.client('s3')
response = s3.list_buckets()
# Output the bucket names
print('Existing buckets:')
for bucket in response['Buckets']:
# print(f' {bucket["Name"]}')
questions = [
inquirer.List('Buckets',
message="Which bucket you would like to upload file? \n",
choices=[{bucket["Name"]}],
),
]
answers = inquirer.prompt(questions)
uj5u.com熱心網友回復:
仔細看看你的代碼。每次迭代都會顯示提示,每個存盤桶一次。
相反,您應該預先構建可用存盤桶的串列,然后才顯示提示:
s3 = boto3.client('s3')
response = s3.list_buckets()
list_of_buckets = [bucket["Name"] for bucket in response['Buckets']]
questions = [
inquirer.List('Buckets',
message="Which bucket you would like to upload file? \n",
choices=list_of_buckets)
]
answers = inquirer.prompt(questions)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/342435.html
標籤:Python 亚马逊网络服务 亚马逊-s3 boto3
上一篇:使用膠水(Python/Pyspark)通過組態檔回圈從源到s3的多個表?
下一篇:將檔案放入私有子網的S3存盤桶中
