亚马逊s3文件上传工作在实时环境但测试环境出错

时间:2018-04-13 05:25:15

标签: c# asp.net amazon-web-services amazon-s3

根据验证程序,远程证书无效。描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中的起源位置的更多信息。

例外详细信息:

  

System.Security.Authentication.AuthenticationException:根据验证程序,远程证书无效。

来源错误:

在执行当前Web请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息。

堆栈追踪:

  

[AuthenticationException:根据验证

,远程证书无效

代码:

public bool UploadFileOnS3Server_sk(string folderName, string fileName)
{
    string _awsAccessKey = ConfigurationManager.AppSettings["awsAccessKey"].ToString();
    string _awsSecretKey = ConfigurationManager.AppSettings["awsSecretKey"].ToString();
    string _bucketName = ConfigurationManager.AppSettings["bucketName"].ToString();
    FileInfo filename = new FileInfo(fileName);
    // string contents = File.ReadAllText(filename.FullName);
    String delimiter = "/" + folderName;
    using (client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1))
    {
        //Console.WriteLine("Uploading an object");
        WritingAnObject(string.Concat(_bucketName, delimiter), filename.FullName, filename.Name);
    }
    return true;
}
static void WritingAnObject(string bucketName, string filePath, string keyName)
{
    try
    {
        AmazonS3Config S3Config = new AmazonS3Config()
        {
            UseHttp = Convert.ToBoolean(ConfigurationManager.AppSettings["UseHttps"])
        };
        PutObjectRequest putRequest1 = new PutObjectRequest
        {
            BucketName = bucketName,
            Key = keyName,
            ContentBody = "sample text",
            CannedACL = S3CannedACL.PublicRead
        };

        PutObjectResponse response1 = client.PutObject(putRequest1);

        // 2. Put object-set ContentType and add metadata.
        PutObjectRequest putRequest2 = new PutObjectRequest
        {
            BucketName = bucketName,
            Key = keyName,
            FilePath = filePath,
            ContentType = "text/plain",
            CannedACL = S3CannedACL.PublicRead,
        };
        putRequest2.Metadata.Add("x-amz-meta-title", "someTitle");

        PutObjectResponse response2 = client.PutObject(putRequest2);

    }
    catch (AmazonS3Exception amazonS3Exception)
    {
        if (amazonS3Exception.ErrorCode != null &&
            (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
            ||
            amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
        {
            Console.WriteLine("Check the provided AWS Credentials.");
            Console.WriteLine(
                "For service sign up go to http://aws.amazon.com/s3");
        }
        else
        {
            Console.WriteLine(
                "Error occurred. Message:'{0}' when writing an object"
                , amazonS3Exception.Message);
        }
    }
}

0 个答案:

没有答案