Python boto, list contents of specific dir in bucket

前端 未结 7 1809
难免孤独
难免孤独 2020-12-13 18:19

I have S3 access only to a specific directory in an S3 bucket.

For example, with the s3cmd command if I try to list the whole bucket:

             


        
7条回答
  •  一个人的身影
    2020-12-13 18:42

    I just had this same problem, and this code does the trick.

    import boto3
    
    s3 = boto3.resource("s3")
    s3_bucket = s3.Bucket("bucket-name")
    dir = "dir-in-bucket"
    files_in_s3 = [f.key.split(dir + "/")[1] for f in 
    s3_bucket.objects.filter(Prefix=dir).all()]
    

提交回复
热议问题