【问题标题】:Getting directory info from S3 bucket: Error: Key is a directory从 S3 存储桶获取目录信息:错误:键是目录
【发布时间】:2020-01-12 17:22:21
【问题描述】:

我正在使用适用于 .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();
}

这是个例外:

键是一个目录

这就是我初始化 s3Client 的方式:

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

【问题讨论】:

    标签: c# .net amazon-s3 aws-sdk aws-sdk-net


    【解决方案1】:

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

    注意:使用 AWS 低级 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();
    }
    

    【讨论】:

      猜你喜欢
      • 2014-04-17
      • 2020-11-24
      • 2017-08-13
      • 1970-01-01
      • 2017-01-03
      • 2011-10-31
      • 1970-01-01
      • 2019-11-07
      • 2022-01-23
      相关资源
      最近更新 更多