谁能告诉我如何确定某个文件/对象是否存在于 S3 存储桶中,并在存在或不存在时显示消息。
基本上我希望它:
1) 检查我的 S3 帐户上的存储桶,例如 testbucket
2) 在该桶内,查看是否有前缀为 test_ 的文件(test_file.txt 或 test_data.txt)。
3) 如果该文件存在,则显示该文件存在或该文件不存在的 MessageBox(或控制台消息)。
有人可以告诉我怎么做吗?
最佳答案
使用适用于 .Net 的 AWSSDK 我目前正在做一些事情:
public bool Exists(string fileKey, string bucketName)
{
try
{
response = _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;
}
}
它有点糟糕,但它现在有效。
关于c# - 根据通配符判断对象是否存在于S3 bucket中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3526585/