0

I am using .Net Core 3.0 to download some files from an S3 bucket.

The structure of the bucket is as follows:

my_folder
prod
    year
        month
            day
                hour
                    minute
                        - joe
                        - jack
                        - john
                        - jacob

I have created a loop that downloads the files for the last ten minutes successfully:

foreach(string objectKey in getObjectKeyNames(dispatch))
{
    request1.Key = objectKey;
    try
    {
        using (GetObjectResponse response1 = await s3Client.GetObjectAsync(request1))
        {
            CancellationTokenSource source = new CancellationTokenSource();
            CancellationToken token = source.Token;
            await response1.WriteResponseStreamToFileAsync(@"C:\temp\" + objectKey, false, token);
        }
    }
    catch(Exception e)
    {
        Console.WriteLine("error");
    }
}

The issue I have is with the line:

await response1.WriteResponseStreamToFileAsync(@"C:\temp\" + objectKey, false, token);

This creates the same structure in c:temp as show above whereas I just want to download all of the files as each file name is unique into c:temp. So to be clear I just want all of the files in C:temp without the folder structure.

The objectKey is the filename which has the following format

my_folder/prod/2019/11/21/13/26/2019_11_21_13_26_joe.json

The filename would be 2019_11_21_13_26_joe.json

Hope that makes sense

MyStackRunnethOver
  • 4,872
  • 2
  • 28
  • 42
Silentbob
  • 2,805
  • 7
  • 38
  • 70

1 Answers1

1

Check out Get a file name from a path in C#. Specifically, Path.getFileName():

objectKey = Path.getFileName(objectKey);
await response1.WriteResponseStreamToFileAsync(@"C:\temp\" + objectKey, false, token);
MyStackRunnethOver
  • 4,872
  • 2
  • 28
  • 42