Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# List all files with filename under an amazon S3 folder

Tags:

Using C# and amazon .Net SDK, able to list all the files with in a amazon S3 folder as below:

ListObjectsRequest request = new ListObjectsRequest();           
request.BucketName = _bucketName; //Amazon Bucket Name
request.Prefix = _sourceKey; //Amazon S3 Folder path           
do
{
    ListObjectsResponse response = _client.ListObjects(request);//_client - AmazonS3Client

Output

Folder
Folder/file1.pdf
Folder/file2.pdf
Folder/file3.pdf

But i wanted to achieve something like this:

Desired Output

file1.pdf
file2.pdf
file3.pdf

Thanks in advance

like image 742
Jameel Avatar asked Aug 30 '16 10:08

Jameel


1 Answers

Also you can use the following c# code to retrieve the files information.

var bucketName = "your bucket";
var s3Client = new AmazonS3Client("your access key", "your secret key");
var dir = new S3DirectoryInfo(s3Client, bucketName, "your AmazonS3 folder name");
foreach (IS3FileSystemInfo file in dir.GetFileSystemInfos())
{
    Console.WriteLine(file.Name);
    Console.WriteLine(file.Extension);
    Console.WriteLine(file.LastWriteTime);
}

I am using amazon AWSSDK.Core and AWSSDK.S3 version 3.1.0.0 for .net 3.5. I hope it can help you

like image 68
Luis Fernando Camacho Camacho Avatar answered Sep 21 '22 06:09

Luis Fernando Camacho Camacho