如何使用c#验证AWS accesskey,密钥

时间:2018-05-16 07:08:34

标签: c# amazon-s3 aws-sdk

有没有办法验证AWS AccessKey,SecretKey是否有效使用c#?

using Amazon.S3.Model;
using Amazon.S3.IO;
using Amazon.S3;
using Amazon.S3.Transfer;

AmazonS3Client s3Client = new AmazonS3Client(AccessKey, SecretKey, Amazon.RegionEndpoint.USEast1);

1 个答案:

答案 0 :(得分:0)

您只需使用try-catch检查它,只需使用get bucket等基本方法即可。如果您无法获得存储桶,则意味着您未经过验证或存在存储桶或其他问题,但事实是您无法访问存储桶

        AmazonS3Client s3Client = new AmazonS3Client(AccessKey, SecretKey, Amazon.RegionEndpoint.USEast1);
        try
        {
            var buckets = s3Client.GetBucketLocationAsync("some-bucket").Result;
        }
        catch (AmazonS3Exception ex)
        {
              //do something when you can't access to your bucket
        }