简体   繁体   中英

AWS S3 Bucket | Determine the count of file datewise and age of the file

I need to develop a service using C# Code to fetch the date-wise count of the files stored in the S3 bucket and group the result through Age of the file and Stored the data into Database with following values. DATE OF SERVICE: Date on which service is run. COUNT: Total Count of the files. AGE: (Current Date - File Created date) There are 5000 files saved in the bucket with random created date.I need to calculate the count and age based on created date.

Suppose Current date is 28/06/2016 and files created on 27/06/2016 is 03
files created on 26/06/2016 is 10 and files created on 25/06/2016 is 09 and so on. then following values should be inserted in the table ie DATEOFSERVICE, AGE, COUNT

I. (DATEOFSERVICE)28/06/2016 (AGE)1 (COUNT)03 II.(DATEOFSERVICE)28/06/2016 (AGE)2 (COUNT)10 III.(DATEOFSERVICE)28/06/2016 (AGE)3 (COUNT)09

until oldest created file reached. could anyone please help me to develop the logic to fetch the count and age.

Thanks

I think there is not create date attribute for the Amazon files.The Amazon file contains last modified(Server-side-Modified).So you can see following code example in order to see How to retrieve all files inside the bucket and group by last modified, and count by date using linq.

BasicAWSCredentials basicCredentials = new BasicAWSCredentials("yoursecretkey", "youraccesskey");
        AmazonS3Config configurationClient = new AmazonS3Config();
        configurationClient.RegionEndpoint = RegionEndpoint.EUCentral1;// region of your bucket
        try
        {
            using (AmazonS3Client clientConnection = new AmazonS3Client(basicCredentials, configurationClient))
            {
                S3DirectoryInfo source = new S3DirectoryInfo(clientConnection, "yourbucket");

                var query = source.GetFiles("*", System.IO.SearchOption.AllDirectories).GroupBy( fi => fi.LastWriteTime.ToString("dd/MM/yyyy")).Select(group => new { LastDateModify = group.Key, Count = group.Count()}).OrderBy( fi => fi.LastDateModify);
                int age = 1;
                foreach (var amazonFile in query)
                {
                    Console.WriteLine(string.Format("DATEOFSERVICE:{0} AGE:{1} COUNT:{2}", amazonFile.LastDateModify, age, amazonFile.Count));
                    age++;
                }
            }
        }
        catch (Exception ex)
        {
            // add the properly handling exception
        }

in the example above I used AWSSDK 3.1.6 for 3.5 net I hope this help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM