确定文件夹或文件密钥-Boto [英] Determine if folder or file key - Boto

查看:91
本文介绍了确定文件夹或文件密钥-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))

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.ObjectSummary.get

这篇关于确定文件夹或文件密钥-Boto的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆