我正在嘗試像這樣列出 s3 物件:
for key in s3_client.list_objects(Bucket='bucketname')['Contents']:
logger.debug(key['Key'])
我只想列印第一層上存在的檔案夾名稱或檔案名。
例如,如果我的存盤桶有這個:
bucketname
folder1
folder2
text1.txt
text2.txt
catallog.json
我只想列印folder1,folder2和catalog.json. 我不想包含 text1.txt 等。
但是,我當前的解決方案還會列印我的存盤桶名稱中的檔案夾中存在的檔案名。
我該如何修改這個?我看到有一個“前綴”引數,但不知道如何使用它。
uj5u.com熱心網友回復:
您可以在“/”上拆分鍵,只保留第一級:
level1 = set() #Using a set removes duplicates automatically
for key in s3_client.list_objects(Bucket='bucketname')['Contents']:
level1.add(key["Key"].split("/")[0]) #Here we only keep the first level of the key
#then print your level1 set
logger.debug(level1)
/!\ 警告
list_object方法已修改,建議list_objects_v2按照AWS S3檔案使用- 此方法僅回傳部分或全部(最多 1,000 個)鍵。如果要確保獲得所有密鑰,則需要使用
continuation_token函式回傳的值:
level1 = set()
continuation_token = ""
while continuation_token is not None:
extra_params = {"ContinuationToken": continuation_token} if continuation_token else {}
response = s3_client.list_objects_v2(Bucket="bucketname", Prefix="", **extra_params)
continuation_token = response.get("NextContinuationToken")
for obj in response.get("Contents", []):
level1.add(obj.get("Key").split("/")[0])
logger.debug(level1)
uj5u.com熱心網友回復:
您使用該Delimiter選項,例如:
import boto3
s3 = boto3.client("s3")
BUCKET = "bucketname"
rsp = s3.list_objects_v2(Bucket=BUCKET, Delimiter="/")
objects = [obj["Key"] for obj in rsp["Contents"]]
folders = [fld["Prefix"] for fld in rsp["CommonPrefixes"]]
for obj in objects:
print("Object:", obj)
for folder in folders:
print("Folder:", folder)
結果:
Object: catalog.json
Folder: folder1/
Folder: folder2/
請注意,如果您的頂層有大量鍵(超過 1000 個),那么您需要對請求進行分頁。
另外,請注意list_objects基本上已被棄用,您應該使用list_objects_v2。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/454784.html
上一篇:將大于5GB的資料幀發送到S3
