【发布时间】:2019-06-23 23:17:48
【问题描述】:
使用 boto 和 Python,我试图区分一个键是否返回一个文件文件夹(我知道 S3 对两者的处理完全相同,因为我不直接处理文件系统)。
我现在拥有的是 2 把钥匙
<Key: my-folder,output/2019/01/28/>
<Key: my-folder,output/2019/01/28/part_1111>
第一个是“文件夹”,第二个是“文件”。我想做的是确定密钥是否是“文件”,但不确定如何确定这一点,显然密钥不是以/ 结尾,但我将如何在 Python 中确定它。
如果我正在迭代 list(),我可以将密钥转换为字符串或访问密钥属性吗?
for obj in srcBucket.list():
# Get the Key object of the given key, in the bucket
k = Key(srcBucket, obj.key)
print(k)
<Key: my-folder,output/2019/01/28/>
<Key: my-folder,output/2019/01/28/part_1111>
【问题讨论】:
-
参见stackoverflow.com/questions/29657384/… .... 你应该使用
import os和os.path.basename方法来提取你的目录。这不是该帖子中公认的答案,但我认为它会做你想要的。迭代您的对象并首先调用密钥。例如files_in_bucket = list(my_bucket.objects.all()) # iterate and remove any portion of the key beyond the last '/' paths_in_bucket = [os.path.dirname(files_in_bucket[x].key) for x in range(len(files_in_bucket))]
标签: python amazon-web-services amazon-s3 boto