代码之家  ›  专栏  ›  技术社区  ›  Ashy Ashcsi

从中排除S3文件夹bucket.objects.filter文件(Prefix=前缀)

  •  0
  • Ashy Ashcsi  · 技术社区  · 6 年前

    我有一个python boto3代码,它列出了s3文件夹/前缀下的所有对象。此代码将返回所有对象,即。

    温度/测试/日期=2019年9月17日/

    温度/测试/日期=17-09-2019/100.json

    温度/测试/日期=2019年9月17日/

    bucket_name = 'temp-bucket'
    prefix = 'temp/test/date=17-09-2019'
    
    bucket = s3_resource.Bucket(bucket_name)
    s3_files = list(bucket.objects.filter(Prefix=prefix))
    
    for file in s3_files:
        print(file)
    

    有没有办法从响应中排除文件夹?

    谢谢

    0 回复  |  直到 6 年前
        1
  •  0
  •   Lamanus    6 年前

    无法筛选出结果,但您可以按以下操作:

    import boto3
    
    bucket_name = 'temp-bucket'
    s3 = boto3.resource('s3')
    
    ignore = s3.Bucket(bucket_name ).objects.filter(Prefix='temp/test/date=17-09-2019')
    response = s3.Bucket(bucket_name ).objects.all()
    
    for item in response:
        if item not in ignore:
            print(item.key)