有人可以告诉我如何确定S3存储桶中是否存在某个文件/对象,并显示消息是否存在或是否存在.
基本上我想要它:
1)检查我的S3帐户上的一个桶,例如testbucket
2)在该存储桶内部,查看是否存在前缀为test_(test_file.txt或test_data.txt)的文件.
3)如果该文件存在,则显示该文件存在的MessageBox(或控制台消息),或该文件不存在.
有人可以告诉我该怎么做吗?
Pra*_*eep 59
使用S3FileInfo.Exists方法:
using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey))
{
S3FileInfo s3FileInfo = new Amazon.S3.IO.S3FileInfo(client, "your-bucket-name", "your-file-name");
if (s3FileInfo.Exists)
{
// file exists
}
else
{
// file does not exist
}
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*lex 51
使用AWSSDK For .Net I目前做的事情如下:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
它有点糟糕,但它现在有效.
Alv*_*vis 12
这解决了它:
列出现有对象的存储桶,并使用这样的前缀.
var request = new ListObjectsRequest()
.WithBucketName(_bucketName)
.WithPrefix(keyPrefix);
var response = _amazonS3Client.ListObjects(request);
var exists = response.S3Objects.Count > 0;
foreach (var obj in response.S3Objects) {
// act
}
Run Code Online (Sandbox Code Playgroud)
Rob*_*iha 11
我知道这个问题已经有几年了,但是新的SDK处理得很漂亮.如果有人还在搜索这个.您正在寻找S3DirectoryInfo类
using (IAmazonS3 s3Client = new AmazonS3Client(accessKey, secretKey))
{
S3DirectoryInfo s3DirectoryInfo = new Amazon.S3.IO.S3DirectoryInfo(s3Client, "testbucket");
if (s3DirectoryInfo.GetFiles("test*").Any())
{
//file exists -- do something
}
else
{
//file doesn't exist -- do something else
}
}
Run Code Online (Sandbox Code Playgroud)
不确定这是否适用于 .NET Framework,但 AWS SDK (v3) 的 .NET Core 版本仅支持异步请求,因此我不得不使用稍微不同的解决方案:
/// <summary>
/// Determines whether a file exists within the specified bucket
/// </summary>
/// <param name="bucket">The name of the bucket to search</param>
/// <param name="filePrefix">Match files that begin with this prefix</param>
/// <returns>True if the file exists</returns>
public async Task<bool> FileExists(string bucket, string filePrefix)
{
// Set this to your S3 region (of course)
var region = Amazon.RegionEndpoint.USEast1;
using (var client = new AmazonS3Client(region))
{
var request = new ListObjectsRequest {
BucketName = bucket,
Prefix = filePrefix,
MaxKeys = 1
};
var response = await client.ListObjectsAsync(request, CancellationToken.None);
return response.S3Objects.Any();
}
}
Run Code Online (Sandbox Code Playgroud)
而且,如果要搜索文件夹:
/// <summary>
/// Determines whether a file exists within the specified folder
/// </summary>
/// <param name="bucket">The name of the bucket to search</param>
/// <param name="folder">The name of the folder to search</param>
/// <param name="filePrefix">Match files that begin with this prefix</param>
/// <returns>True if the file exists</returns>
public async Task<bool> FileExists(string bucket, string folder, string filePrefix)
{
return await FileExists(bucket, $"{folder}/{filePrefix}");
}
Run Code Online (Sandbox Code Playgroud)
用法:
var testExists = await FileExists("testBucket", "test_");
// or...
var testExistsInFolder = await FileExists("testBucket", "testFolder/testSubFolder", "test_");
Run Code Online (Sandbox Code Playgroud)