Skip to content

Instantly share code, notes, and snippets.

@rakisaionji
Created April 1, 2021 00:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rakisaionji/a0708075c52d2e09c3884f27dc2e5caf to your computer and use it in GitHub Desktop.
Save rakisaionji/a0708075c52d2e09c3884f27dc2e5caf to your computer and use it in GitHub Desktop.
List all files with filename under an Amazon S3 folder using ListObjects feature. No special permissions needed, just to save cost.
using Amazon.S3;
using System;
class Program
{
static void Main(string[] args)
{
var bucketName = "my-bucket";
string accessKeyId = "my-access-key-id";
string secretAccessKey = "secret-access-key-id";
var s3Client = new AmazonS3Client(accessKeyId, secretAccessKey);
var totalDirs = 0;
var totalFile = 0;
var totalSize = 0L;
var objects = s3Client.ListObjects(bucketName);
foreach (var item in objects.S3Objects)
{
var key = item.Key;
totalSize += item.Size;
if (key.EndsWith("/"))
{
totalDirs++;
continue;
}
else totalFile++;
Console.WriteLine(key);
}
Console.WriteLine();
Console.WriteLine("Total Objects : {0}", objects.S3Objects.Count);
Console.WriteLine("Total Files : {0}", totalFile);
Console.WriteLine("Total Folders : {0}", totalDirs);
Console.WriteLine("Total Size : {0:N0} bytes", totalSize);
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment