Created
April 1, 2021 00:34
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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