Amazon S3 C# 开发工具包 “底层连接已关闭:无法为 SSL/TLS 安全通道建立信任关系.” 错误
本文关键字:TLS SSL 安全 通道 错误 关系 信任 建立 开发工具 工具包 开发 | 更新日期: 2023-09-27 18:36:40
我正在使用 Amazon C# 开发工具包版本 1.5.36.0。我创建了一个类来将文件上传到 Amazon S3,在我的机器中它工作得很好,根本没有错误,但是当我在生产服务器中运行它时,我收到以下错误:"底层连接已关闭:无法为 SSL/TLS 安全通道建立信任关系。
我正在下面粘贴我引用的代码段:
public bool SaveFile(S3Folder folder, string customFolder, string fileName, Stream stream, bool publicFile)
{
// Validações
if (string.IsNullOrEmpty(fileName) || stream == null)
return false;
using (var s3Client = new AmazonS3Client(accessKey, secretKey, region))
{
var request = new PutObjectRequest();
request.BucketName = bucketName;
request.InputStream = stream;
if (!string.IsNullOrEmpty(customFolder))
request.Key = GetFolder(folder) + "/" + customFolder + "/" + fileName;
else
request.Key = GetFolder(folder) + "/" + fileName;
if (!publicFile)
request.CannedACL = S3CannedACL.Private;
else
request.CannedACL = S3CannedACL.PublicRead;
s3Client.PutObject(request);
return true;
}
}
这是我类中保存文件的方法。S3Folder 是一个枚举,GetFolder 只返回一个带有文件夹名称的字符串。
你们能帮我吗?我一直在寻找它,但还没有答案解决我的问题。
提前谢谢。
我已经解决了。我在创建客户端时将其设置为 http。请参阅下面的代码:
public bool SaveFile(S3Folder folder, string customFolder, string fileName, Stream stream, bool publicFile)
{
// Validações
if (string.IsNullOrEmpty(fileName) || stream == null)
return false;
AmazonS3Config S3Config = new AmazonS3Config()
{
ServiceURL = "s3.amazonaws.com",
CommunicationProtocol = Protocol.HTTP,
RegionEndpoint = region
};
using (var s3Client = new AmazonS3Client(accessKey, secretKey, S3Config))
{
var request = new PutObjectRequest();
request.BucketName = bucketName;
request.InputStream = stream;
if (!string.IsNullOrEmpty(customFolder))
request.Key = GetFolder(folder) + "/" + customFolder + "/" + fileName;
else
request.Key = GetFolder(folder) + "/" + fileName;
if (!publicFile)
request.CannedACL = S3CannedACL.Private;
else
request.CannedACL = S3CannedACL.PublicRead;
s3Client.PutObject(request);
return true;
}
}
与其将
通信协议设置为HTTP,这会损害安全性,而是在S3Config中设置ForcePathStyle = true,则可以坚持使用HTTPS。(这至少需要 2.0.13.0 版的 AWSSDK.dll。
这里很好地解释了需要这样做的原因:http://shlomoswidler.com/2009/08/amazon-s3-gotcha-using-virtual-host.html。简而言之,S3 支持对象的两个版本的路径,一个类似
https://s3.amazonaws.com/mybucket.mydomain.com/myObjectKey
和一个喜欢
https://mybucket.mydomain.com.s3.amazonaws.com/myObjectKey
默认使用的第二种形式会导致安全证书出现问题;ForcePathStyle 使 S3Client 使用第一种形式。