Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list the files in S3 subdirectory using Python

I'm trying to list the files under sub-directory in S3 but I'm not able to list the files name:

import boto
from boto.s3.connection import S3Connection
access=''
secret=''
conn=S3Connection(access,secret)
bucket1=conn.get_bucket('bucket-name')
prefix='sub -directory -path'
print bucket1.list(prefix) 
files_list=bucket1.list(prefix,delimiter='/') 
print files_list
for files in files_list:
  print files.name

Can you please help me to resolve this issue.

like image 551
prasanna Kumar Avatar asked Jul 11 '17 11:07

prasanna Kumar


People also ask

How do I find files on AWS S3?

open the bucket, select "none" on the right hand side, and start typing in the file name. Still only let's you search by the prefix of the item name.

What is boto3 client (' S3 ')?

​Boto3 is the official AWS SDK for Python, used to create, configure, and manage AWS services. The following are examples of defining a resource/client in boto3 for the Weka S3 service, managing credentials, and pre-signed URLs, generating secure temporary tokens, and using those to run S3 API calls.


2 Answers

Your code can be fixed by adding a / at the end of the Prefix.

The modern equivalent using boto3 would be:

import boto3
s3 = boto3.resource('s3')

## Bucket to use
bucket = s3.Bucket('my-bucket')

## List objects within a given prefix
for obj in bucket.objects.filter(Delimiter='/', Prefix='fruit/'):
    print(obj.key)

Output:

fruit/apple.txt
fruit/banana.txt

Rather than using the S3 client, this code uses the S3 object provided by boto3, which makes some code simpler.

like image 126
John Rotenstein Avatar answered Nov 03 '22 13:11

John Rotenstein


You can do this by using boto3. Listing out all the files.

import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('bucket-name')
objs = list(bucket.objects.filter(Prefix='sub -directory -path'))
for i in range(0, len(objs)):
    print(objs[i].key)

This piece of code will print all the files with path present in the sub-directory

like image 43
AshuGG Avatar answered Nov 03 '22 15:11

AshuGG