Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get only file names from s3 bucket folder

I have a s3 bucket named 'Sample_Bucket' in which there is a folder called 'Sample_Folder'. I need to get only the names of all the files in the folder 'Sample_Folder'.

I am using the following code to do so -

import boto3
s3 = boto3.resource('s3', region_name='us-east-1', verify=False)
    bucket = s3.Bucket('Sample_Bucket')
    for files in bucket.objects.filter(Prefix='Sample_Folder):
        print(files)

The variable files contain object variables which has the filename as key.

s3.ObjectSummary(bucket_name='Sample-Bucket', key='Sample_Folder/Sample_File.txt')

But I need only the filename. How do I extract that? Or is there any other way to do it?

like image 525
TeeKay Avatar asked Dec 07 '19 12:12

TeeKay


People also ask

How do I view contents of a S3 bucket?

Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that contains the object. In the Objects list, choose the name of the object for which you want an overview. The object overview opens.

Can I download a folder from S3 bucket?

Use the cp command to download a folder inside a bucket from S3 to local. recursive option will download all files and folders if you have a recursive folder/file structure.

Can I read S3 file without downloading?

Reading objects without downloading them Similarly, if you want to upload and read small pieces of textual data such as quotes, tweets, or news articles, you can do that using the S3 resource method put(), as demonstrated in the example below (Gist).


1 Answers

Here you go.

import boto3


bucket = "Sample_Bucket"
folder = "Sample_Folder"
s3 = boto3.resource("s3")
s3_bucket = s3.Bucket(bucket)
files_in_s3 = [f.key.split(folder + "/")[1] for f in s3_bucket.objects.filter(Prefix=folder).all()]
like image 168
rob Avatar answered Oct 19 '22 05:10

rob