|
|
|
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
|
|
|
// SPDX - License - Identifier: Apache - 2.0
|
|
|
|
|
|
|
|
|
|
using Amazon;
|
|
|
|
|
using Amazon.Runtime;
|
|
|
|
|
using Amazon.S3;
|
|
|
|
|
using Amazon.S3.Model;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace S3Demo.Model
|
|
|
|
|
{
|
|
|
|
|
// This example uses the AWS SDK for .NET to check isExist the Amazon Simple Storage
|
|
|
|
|
// Service (Amazon S3) buckets belonging to the default account. This code
|
|
|
|
|
// was written using AWS SDK for .NET v3.5 and .NET Core 5.0.
|
|
|
|
|
class BucketModel
|
|
|
|
|
{
|
|
|
|
|
private static IAmazonS3 _s3Client;
|
|
|
|
|
|
|
|
|
|
private static bool bucketFlag;
|
|
|
|
|
|
|
|
|
|
#region 判断是否存在
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 判断存储桶是否存在
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_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)
|
|
|
|
|
{
|
|
|
|
|
if (item.BucketName == bucketName)
|
|
|
|
|
{
|
|
|
|
|
bucketFlag = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
Console.WriteLine("Create bucket " + bucketName + " successffl!");
|
|
|
|
|
}
|
|
|
|
|
catch (AmazonS3Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Error creating bucket: '{ex.Message}'");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|