Amazon S3 使 SSL 与 c# sdk 配合使用时出现问题

2024-04-01

我正在使用亚马逊 AWS .NET SDK v1.2.1。

以下代码在 DNS 查找失败后引发异常myBucket.more.https这显然不是它应该寻找的......

AmazonS3Config S3Config = new AmazonS3Config()
{
    ServiceURL = "https://s3.amazonaws.com",
    CommunicationProtocol = Amazon.S3.Model.Protocol.HTTPS,
};

using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey,secretKey, S3Config))
{
    PutObjectRequest request = new PutObjectRequest();

    using (MemoryStream ms = new MemoryStream(inputBytes))
    {
        request.WithBucketName("myBucket.more")
                .WithKey(output.Name)
                .WithInputStream(ms);

        using (S3Response response = client.PutObject(request))
        {
                        Log.Message("File Sent: " + output.Name);
        }
    }
}

如果我删除https://从 ServiceURL 的前面,它会抛出一个 Web 异常:

"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."

那么我该如何让 SSL 工作呢?

到目前为止,我管理的唯一方法是使用以下方法,但这是不安全的:

AmazonS3Config S3Config = new AmazonS3Config()
{
    ServiceURL = "s3.amazonaws.com",
    CommunicationProtocol = Amazon.S3.Model.Protocol.HTTP,
};

UPDATE

如果我不将自定义 S3Config 传递给CreateAmazonS3Client,它仍然失败:

"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."

"The remote certificate is invalid according to the validation procedure."


ServiceUrl 应该是S3.amazonaws.com没有https://在前。这是默认设置HTTPS为了Communication协议。这就是为什么当您不手动设置设置时会出现相同的错误。

Update

如果您想使用 HTTPS,EU 存储桶的名称中不能包含句点 (.)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Amazon S3 使 SSL 与 c# sdk 配合使用时出现问题 的相关文章

随机推荐