You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.5 KiB

3 years ago
// 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;
/// <summary>
/// 判断存储桶是否存在
/// </summary>
/// <param name="bucketName"></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)
{
if (item.BucketName == bucketName)
{
flag = true;
}
}
return flag;
}
}
}