Working with Amazon (AWS) SDK / API behind a proxy server

I am currently working on a few projects that require me to use the Amazon AWS SDK for .NET. With that said when I go to the trouble of actually going into the office and not work from home I find myself sitting behind a corporate proxy server and this has introduced a few hurdles.

Most examples you will find on the internet won't work in this case because your request will get denied so I thought I would share a few examples on how I work around this.

The answer is in a "config" object - if we create a config object for whatever request we are trying to do we can pass in proxy authentication information.

Example 1: Deploy an AWS CloudFormation template.

At the top of your .NET page be sure to set your references.

using Amazon.CloudFormation;
using Amazon.CloudFormation.Model;
using Amazon.StorageGateway;
using Amazon.StorageGateway.Model;


What most examples on line are going to have you start with is something like this...

var client = new AmazonCloudFormationClient();

Before we create the client we need to create a config object and then pass that as a parameter to the client as a parameter.
            
            var ProxyUser = ConfigurationManager.AppSettings["ProxyUserName"];
            var ProxyPass = ConfigurationManager.AppSettings["ProxyPassword"];

            AmazonCloudFormationConfig config = new AmazonCloudFormationConfig();
            config.ProxyCredentials = new NetworkCredential(ProxyUser, ProxyPass);
            config.ProxyPort=9090;
            config.ProxyHost="*****************";
            config.RegionEndpoint = Amazon.RegionEndpoint.USWest1;

Okay so in my example I had the ProxyHost hard coded which is dumb but you just as easy could pull that as an app setting from the Web.Config file. So now that we have a "config" object we just need to pass that to the client.

var client = new AmazonCloudFormationClient(config);



Now you can go use your client to go do the work.

Example 2: Create  AWS Storage Gateway
   
            var ProxyUser = ConfigurationManager.AppSettings["ProxyUserName"];
            var ProxyPass = ConfigurationManager.AppSettings["ProxyPassword"];
            var config = new AmazonStorageGatewayConfig();
            config.ProxyCredentials = new NetworkCredential(ProxyUser, ProxyPass);
            config.ProxyPort = 9090;
            config.ProxyHost = "*************";
            config.RegionEndpoint = Amazon.RegionEndpoint.USWest1;
            var client = new AmazonStorageGatewayClient(config);

So in this example we created an AmazonStorageGatewayConfig but it is still just a config that you pass to the constructor of the client.

If you are looking for examples on how to get around a proxy server hopefully this helps.
Working with Amazon (AWS) SDK / API behind a proxy server Working with Amazon (AWS) SDK / API behind a proxy server Reviewed by Shawn on 7:26 AM Rating: 5

No comments:

Powered by Blogger.