【发布时间】:2012-04-14 05:43:50
【问题描述】:
我正在尝试确定我的 Amazon S3 存储桶中是否存在文件夹,如果不存在,我想创建它。
目前我可以使用 .NET SDK 创建文件夹,如下所示:
public void CreateFolder(string bucketName, string folderName)
{
var folderKey = folderName + "/"; //end the folder name with "/"
var request = new PutObjectRequest();
request.WithBucketName(bucketName);
request.StorageClass = S3StorageClass.Standard;
request.ServerSideEncryptionMethod = ServerSideEncryptionMethod.None;
//request.CannedACL = S3CannedACL.BucketOwnerFullControl;
request.WithKey(folderKey);
request.WithContentBody(string.Empty);
S3Response response = m_S3Client.PutObject(request);
}
现在,当我尝试使用此代码查看文件夹是否存在时:
public bool DoesFolderExist(string key, string bucketName)
{
try
{
S3Response response = m_S3Client.GetObjectMetadata(new GetObjectMetadataRequest()
.WithBucketName(bucketName)
.WithKey(key));
return true;
}
catch (Amazon.S3.AmazonS3Exception ex)
{
if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
return false;
//status wasn't not found, so throw the exception
throw;
}
}
找不到文件夹。奇怪的是,如果我使用 AWS 管理控制台创建文件夹,'DoesFolderExist' 方法可以看到它。
我不确定这是否是 ACL/IAM 问题,但不确定如何解决。
【问题讨论】:
标签: c# .net amazon-s3 amazon-web-services