2

I'm developping an app that upload files to s3 bucket and i want to list the objects and their upload dates using aws sdk for .net. Is there any code that do that ?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Driss Elk
  • 53
  • 1
  • 7

2 Answers2

5

UPD: Unfortunately, S3 doesn't store the object's upload date. It can provide you with lastModifiedDate which only equals to 'upload date' if the object hasn't been modified after the upload.

In order to get the upload date, you should save it during the upload with the object:

                var putRequest = new PutObjectRequest
                {
                    BucketName = bucketName,
                    Key = keyName,
                    FilePath = filePath,
                    ContentType = "text/plain"
                };
                putRequest.Metadata.Add("x-upload-date", "someUploadDate");

and then read it from object metadata

                GetObjectRequest request = new GetObjectRequest
                {
                    BucketName = bucketName,
                    Key = keyName
                };
                using (GetObjectResponse response = await client.GetObjectAsync(request))
                using (Stream responseStream = response.ResponseStream)
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    string uploadDate = response.Metadata["x-upload-date"]; 
                }
Dzmitry Bahdanovich
  • 1,735
  • 2
  • 17
  • 34
  • Yes I have permissions but, I'll try it now and see if the variable "entry" contains the upload date – Driss Elk May 03 '19 at 13:50
  • @DrissElk S3 doesn't save `upload date` by default, only `last modified date`. I've amended the original answer, please have a look – Dzmitry Bahdanovich May 03 '19 at 14:22
  • @DmitryBogdanovich yees indeed i had found the answer in your post (the link you shared) i was near the answer i had to do just few modifications of the class and it worked thankss – Driss Elk May 03 '19 at 16:45
  • @DrissElk; please note that thanks is expressed as an up vote – Stefan May 04 '19 at 12:59
0

I was able to achieve this by setting each file name using a ULID.

https://github.com/ulid/spec

The generated id includes a timestamp that can be decoded using the method linked below

https://github.com/ulid/javascript/issues/65#:~:text=See-,decodeTime,-.%20Looks%20like%20it%27s

Yadam
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 23 '22 at 21:45