确定文件夹或文件密钥-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).
此刻我只有2个按键
<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()
,可以将密钥转换为字符串还是可以访问keys属性?
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 将具有以下属性:
'ContentType':'application/x-directory'
'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屋!