Search code examples
c#amazon-s3sharepoint-2010aws-sdk

A WebException with status TrustFailure was thrown. AmazonS3 .net 3.5 Version 3 invoking from sharepoint 2010


Currently I am working in POC with CRUD operations using AmazonS3 Sdk for .net 3.5 version 3. I am trying to retrieve the Region Endpoint(Location) of the specific bucket name using secret key and Access Key and bucket name( has Location: EU (Frankfurt) (eu-central-1)). in order to establish connection with AmazonS3 and perform CRUD operations So I get the A WebException with status TrustFailure was thrown when I tried to get the Region Endpoint from share point(web page I create my own page using the master page of SharePoint) in order to create AmazonS3Client instance with Region Retrieve.
with the following code:

private string defaultAmazonHttpsHost = "https://s3.amazonaws.com";
private string defaultAmazonHttpHost = "http://s3.amazonaws.com"; 

private Amazon.RegionEndpoint GetRegionEndpoint(string bucket, BasicAWSCredentials amazonCredentials, bool useSSL)
{
    Amazon.RegionEndpoint regiongEndpoint = null;
    AmazonS3Config configurationClient = new AmazonS3Config();
    configurationClient.UseHttp = !useSSL;
    configurationClient.ServiceURL = useSSL ? defaultAmazonHttpsHost : defaultAmazonHttpHost;
    try
    {
        using (AmazonS3Client clientConnection = new AmazonS3Client(amazonCredentials, configurationClient))
        {
            GetBucketLocationRequest locationRequest = new GetBucketLocationRequest();
            locationRequest.BucketName = bucket;
            string locationName = clientConnection.GetBucketLocation(locationRequest).Location.Value;
            if (locationName.Equals("EU", StringComparison.InvariantCultureIgnoreCase))
            {
                regiongEndpoint = Amazon.RegionEndpoint.EUWest1;
            }
            else if (string.IsNullOrEmpty(locationName))
            {
                regiongEndpoint = Amazon.RegionEndpoint.USEast1;
            }
            else
            {
                regiongEndpoint = Amazon.RegionEndpoint.GetBySystemName(locationName);
            }
        }
    }
    catch (AmazonS3Exception amazonS3Exception)
    {
          throw amazonS3Exception;
    }
    catch (Exception unExpectedException)
    {
        throw unExpectedException;
    }
    return regiongEndpoint;
}
BasicAWSCredentials credentials = new BasicAWSCredentials("my access Key", "my secret key");
AmazonS3Config configurationAmazon = new AmazonS3Config();
configurationAmazon.RegionEndpoint = GetRegionEndpoint("bucketName", credentials, false);
 AmazonS3Client _s3 = new AmazonS3Client(credentials, configurationAmazon );

My task Perform CRUD operations + test connection with AmazonS3 Sdk .net 3.5 version 3 , with the source information : -secret key - access key - bucket Name

the strange is if this part code run(execute) since another Project (without share point interaction for example: Console Project) I do not get this exception) Do you know what is the problem?


Solution

  • I used the following before execute any request to amazonS3 and now it works as expected I think the problem was with the certificates that sharepoint is using .

    ServicePointManager.ServerCertificateValidationCallback +=   
    delegate(  
        object sender,   
        X509Certificate certificate,   
        X509Chain chain,   
        SslPolicyErrors sslPolicyErrors)  
        {  
             return true;  
        };  
    

    the post provide a explanation about it