|
|
|
@ -18,29 +18,66 @@ namespace S3Demo.Model
|
|
|
|
|
{
|
|
|
|
|
private static IAmazonS3 _s3Client;
|
|
|
|
|
|
|
|
|
|
private static bool bucketFlag;
|
|
|
|
|
|
|
|
|
|
#region 判断是否存在
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 判断存储桶是否存在
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bucketName"></param>
|
|
|
|
|
/// <param name="bucketName">传入bucketName为 "test" , 而不是 "minio/test"</param>
|
|
|
|
|
/// <param name="credentials"></param>
|
|
|
|
|
/// <param name="conf"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task<bool> isExistBucket(string bucketName, BasicAWSCredentials credentials, AmazonS3Config conf)
|
|
|
|
|
{
|
|
|
|
|
bool flag = false;
|
|
|
|
|
_s3Client = new AmazonS3Client(credentials, conf);
|
|
|
|
|
var response = await _s3Client.ListBucketsAsync();
|
|
|
|
|
List<S3Bucket> bucketList = response.Buckets;
|
|
|
|
|
bucketList.ForEach(b => Console.WriteLine($"Bucket name: {b.BucketName}, created on: {b.CreationDate}"));
|
|
|
|
|
foreach (var item in bucketList)
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (item.BucketName == bucketName)
|
|
|
|
|
_s3Client = new AmazonS3Client(credentials, conf);
|
|
|
|
|
var response = await _s3Client.ListBucketsAsync();
|
|
|
|
|
List<S3Bucket> bucketList = response.Buckets;
|
|
|
|
|
//bucketList.ForEach(b => Console.WriteLine($"Bucket name: {b.BucketName}, created on: {b.CreationDate}"));
|
|
|
|
|
foreach (var item in bucketList)
|
|
|
|
|
{
|
|
|
|
|
flag = true;
|
|
|
|
|
if (item.BucketName == bucketName)
|
|
|
|
|
{
|
|
|
|
|
bucketFlag = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return flag;
|
|
|
|
|
catch (AmazonS3Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Error isExist bucket: '{ex.Message}'");
|
|
|
|
|
}
|
|
|
|
|
return bucketFlag;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 创建存储桶
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Uses Amazon SDK for .NET PutBucketAsync to create a new
|
|
|
|
|
/// Amazon S3 bucket.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client">The client object used to connect to Amazon S3.</param>
|
|
|
|
|
/// <param name="bucketName">完整的桶路径,如: "minio/test"</param>
|
|
|
|
|
public static async Task CreateBucket(string bucketName, BasicAWSCredentials credentials, AmazonS3Config conf)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_s3Client = new AmazonS3Client(credentials, conf);
|
|
|
|
|
var putBucketRequest = new PutBucketRequest
|
|
|
|
|
{
|
|
|
|
|
BucketName = bucketName,
|
|
|
|
|
UseClientRegion = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var putBucketResponse = await _s3Client.PutBucketAsync(putBucketRequest);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (AmazonS3Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Error creating bucket: '{ex.Message}'");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|