0

I am using AWS SDk for .NET, I am trying to get the directory info from an S3 bucket.

The following code works and I can get the files in my root folder

// path: folder
S3DirectoryInfo di = new S3DirectoryInfo(s3Client, "myS3BucketName", "folder");

if (di.Exists)
{
    IS3FileSystemInfo[] files = di.GetFileSystemInfos();
}

Now I try the same code, and query a sub-folder under folder, the code gets the directory info... but while reading the file contents, it throws exception

// path: folder/sub-folder
S3DirectoryInfo di = new S3DirectoryInfo(s3Client, "myS3BucketName", "folder/sub-folder");

if (di.Exists)
{
    // directory exists, but reading files throws exception
    IS3FileSystemInfo[] files = di.GetFileSystemInfos();
}

This is the exception:

Key is a directory

This is how I initialize the s3Client:

s3Client = new AmazonS3Client("Ak...access-key...", "+8b...secret-key...", RegionEndpoint.APSoutheast2);
Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137

1 Answers1

0

The problem was using slash in the path... when using high-level APIs, you need to use backslash in the path.

Note: you need to use slash in the path when using AWS low-level API.

This code works fine:

// path: folder\sub-folder\
string path = @"folder\sub-folder\";
S3DirectoryInfo di = new S3DirectoryInfo(s3Client, "myS3BucketName", path);

if (di.Exists)
{
    // directory exists, but reading files throws exception
    IS3FileSystemInfo[] files = di.GetFileSystemInfos();
}
Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137