简体   繁体   English

从没有目录名的 S3 存储桶和目录中获取文件名?

[英]Get files names from S3 bucket and directory without directory name?

I have connected to S3 and got all files in my S3 bucket foo and from folder poo.我已连接到 S3 并从我的 S3 存储桶 foo 和文件夹 poo 中获取所有文件。 All my files are in S3://foo/poo .我所有的文件都在S3://foo/poo

Running this code returns:运行此代码返回:

[i.key for i in s3_resource.Bucket(foo).objects.filter(Delimiter='/', Prefix='poo/')]

I am getting:我正进入(状态:

['poo/',
 'poo/a.png',
 'poo/b.png',
 'poo/c.png',
.....]

How can I get rid of poo/ ?我怎样才能摆脱poo/

use rsplit with max splits equal to 1 and take the 2nd value使用最大拆分等于 1 的 rsplit 并取第二个值

[i.key.rsplit('/', 1)[1] 
 for i in s3_resource.Bucket(foo).objects.filter(Delimiter='/', Prefix='poo/')
 if i.key.rsplit('/', 1)[1] != '']

should give应该给

['a.png', 'b.png', 'c.png', ...]
[i.key for i in s3_resource.Bucket("foo").objects.filter(Delimiter='/', Prefix='poo/') if i.key != 'poo/']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM