Getting directory info from S3 bucket: Error: Key is a directory

Getting directory info from S3 bucket: Error: Key is a directory

我正在使用适用于 .NET 的 AWS SDK,我正在尝试从 S3 存储桶中获取目录信息。

以下代码有效,我可以在根目录中获取文件 folder

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

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

现在我尝试相同的代码,并查询 folder 下的 sub-folder,代码获取目录信息...但是在读取文件内容时,它抛出异常

// 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();
}

这是例外情况:

Key is a directory

这是我初始化 s3Client 的方式:

s3Client = new AmazonS3Client("Ak...access-key...", "+8b...secret-key...", RegionEndpoint.APSoutheast2);

问题是在路径中使用斜杠...使用高级 APIs 时,您需要在路径中使用反斜杠。

注意:使用AWS low-level时需要在路径中使用斜杠API.

这段代码工作正常:

// 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();
}