not exist then create

master
ZGGSONG 3 years ago
parent 464dcdcab0
commit df08e604c0

@ -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
}
}
}

@ -15,6 +15,16 @@ namespace S3Demo.Model
class ObjectModel
{
#region 发送对象(带标签)
/// <summary>
/// 带标签发送对象至存储桶
/// </summary>
/// <param name="bucketName"></param>
/// <param name="keyName"></param>
/// <param name="filePath"></param>
/// <param name="tags"></param>
/// <param name="credentials"></param>
/// <param name="conf"></param>
/// <returns></returns>
public static async Task Putobjectswithtags(string bucketName, string keyName, string filePath, string[] tags, BasicAWSCredentials credentials, AmazonS3Config conf)
{
//string bucketName = "doc-example-bucket";

@ -29,6 +29,9 @@ namespace S3Demo
{
try
{
//creating Bucket
//await CreateBucket.CreatingBucket("minio/bu29", credentials, conf);
//upload Dir
string dirPath = @"C:\Users\song\Pictures\Saved Pictures\";
string suffix = "*.png";
@ -47,11 +50,12 @@ namespace S3Demo
//await ObjectModel.Putobjectswithtags(bucketName, keyName, filaPath, tags, credentials, conf);
//read Object
await ObjectModel.Readobjectdata(bucketName, keyName, credentials, conf);
//await ObjectModel.Readobjectdata(bucketName, keyName, credentials, conf);
//isExist Bucket
//从"minio/test"中取出存储桶名
var _bucketName = bucketName.Split('/')[1];
//此处传入bucketName: "test"
var flag = await BucketModel.isExistBucket(_bucketName, credentials, conf);
if (flag)
{
@ -60,6 +64,8 @@ namespace S3Demo
else
{
Console.WriteLine("not exist");
//此处传入BucketName: "minio/test"
await BucketModel.CreateBucket(bucketName, credentials, conf);
}
}
catch (Exception ex)

@ -53,11 +53,12 @@
<ItemGroup>
<Compile Include="Model\BucketModel.cs" />
<Compile Include="Model\ObjectModel.cs" />
<Compile Include="Stack\ListObjects.cs" />
<Compile Include="Storage\CreateBucket.cs" />
<Compile Include="Storage\ListObjects.cs" />
<Compile Include="Run.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Stack\UploadDirMPUHighLevelAPI.cs" />
<Compile Include="Stack\UploadFileMPUHighLevelAPI.cs" />
<Compile Include="Storage\UploadDirMPUHighLevelAPI.cs" />
<Compile Include="Storage\UploadFileMPUHighLevelAPI.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />

@ -0,0 +1,58 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX - License - Identifier: Apache - 2.0
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Threading.Tasks;
namespace S3Demo.Storage
{
public class CreateBucket
{
// This example shows how to use Amazon Simple Storage Service (Amazon S3)
// to create a new Amazon S3 bucket. The examples uses AWS SDK for .NET 3.5 and
// .NET 5.0.
private static IAmazonS3 _s3Client;
// Specify the name of the new bucket.
//private const string NEW_BUCKET_NAME = "doc-example-bucket";
public static async Task CreatingBucket(string NEW_BUCKET_NAME, BasicAWSCredentials credentials, AmazonS3Config conf)
{
_s3Client = new AmazonS3Client(credentials, conf);
Console.WriteLine($"\nCreating a new bucket, named: {NEW_BUCKET_NAME}.");
await CreatingBucketAsync(_s3Client, NEW_BUCKET_NAME);
}
/// <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">The name of the bucket to create.</param>
static async Task CreatingBucketAsync(IAmazonS3 client, string bucketName)
{
try
{
var putBucketRequest = new PutBucketRequest
{
BucketName = bucketName,
UseClientRegion = true
};
var putBucketResponse = await client.PutBucketAsync(putBucketRequest);
}
catch (AmazonS3Exception ex)
{
Console.WriteLine($"Error creating bucket: '{ex.Message}'");
}
}
}
}
Loading…
Cancel
Save