首页 文章

Amazon S3 C#SDK“底层连接已关闭:无法为SSL / TLS安全通道 Build 信任关系 . ”错误

提问于
浏览
0

我使用的是Amazon C#SDK版本1.5.36.0 . 我创建了一个类来上传文件到Amazon S3和我的机器它工作得很好,没有任何错误,但是当我在 生产环境 服务器中运行时,我收到以下错误:“底层连接已关闭:无法 Build 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只返回一个包含文件夹名称的字符串 .

你能帮帮我吗?我一直在寻找它,但没有答案解决了我的问题 .

提前致谢 .

2 回答

  • 2

    我已经解决了 . 我在创建客户端时将其设置为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;
            }
        }
    
  • 0

    如果您在S3Config中设置ForcePathStyle = true,则可以坚持使用HTTPS,而不是将CommunicationProtocol设置为HTTP,这会破坏安全性 . (这至少需要版本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使用第一个表单 .

相关问题