确定文件夹或文件密钥 - Boto [英] Determine if folder or file key - Boto
问题描述
使用 boto 和 Python,我试图区分密钥是否返回文件文件夹(我知道 S3 对两者的处理方式完全相同,因为我没有直接处理文件系统).
Using boto and Python I am trying to differentiate whether a key is returning a folder of file (I am aware that S3 treats both exactly the same as I am not dealing with a filesystem directly).
我现在有两把钥匙
<Key: my-folder,output/2019/01/28/>
<Key: my-folder,output/2019/01/28/part_1111>
第一个是文件夹",第二个是文件".我想要做的是确定密钥是否是文件",但不确定如何确定这一点,很明显,密钥不是以 /
结尾,但我将如何在 Python 中确定它.
The first being a "Folder" and the second being a "File". What I would like to do is determine whether the key is a "File" but unsure on how to determine this, the obvious is the key not ending in a /
but how would I determine that in Python.
如果我正在迭代 list()
,我可以将密钥转换为字符串或访问密钥属性吗?
If I was iterating over a list()
can I turn the key into a string or access the keys attributes?
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>
推荐答案
S3.Object 和 S3.ObjectSummary 将具有以下属性:
S3.Object and S3.ObjectSummary will have the following property:
'ContentType': '应用程序/x-目录'
'ContentType': 'application/x-directory'
如果键是目录.
for s3_obj_summary in bucket.objects.all():
if s3_obj_summary.get()['ContentType'] == 'application/x-directory':
print(str(s3_obj_summary))
这篇关于确定文件夹或文件密钥 - Boto的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!