Monday, 7 April 2014

AWS .NET SDK on Mono/Linux

A quick guide to getting an AWS .NET console program running on Linux. Starting with the hard part, compiling the SDK from source.

Update: NuGet can also be used if you are running Mono 3.2+, see this post

1. Install mono and associated tools (on Ubuntu/Debian), mozroots downloads root certificates to enable SSL via mono:
sudo apt-get update
sudo apt-get install mono-complete git
mozroots --import --sync

2. Create a working directory and change to it:
mkdir mono-aws
mkdir mono-aws/lib
cd mono-aws

3. Retrieve the SDK source:
git clone https://github.com/aws/aws-sdk-net.git

4. Fix some file names (case is important to real operating systems). Update: this should be fixed in the next release of the SDK in which case it can be skipped:
cd aws-sdk-net/AWSSDK_DotNet35/
mv Amazon.S3/Model/PUtACLResponse.cs Amazon.S3/Model/PutACLResponse.cs
mv Amazon.S3/IAmazonS3.extensions.cs Amazon.S3/IAmazonS3.Extensions.cs

5. Compile core library, the command line parameters are to default the compile to use .NET 3.5 and not warn on anything other than compile errors (still in aws-sdk-net/AWSSDK_DotNet35):
xbuild /p:TargetFrameworkProfile="" /p:WarningLevel=0

6. Compile extensions (still in aws-sdk-net/AWSSDK_DotNet35):
cd ../AWS.Extensions/
xbuild /p:TargetFrameworkProfile="" /p:WarningLevel=0

7. Install libraries (from aws-sdk-net/AWS.Extentions):
cd ../..
cp aws-sdk-net/AWS.Extensions/SessionProvider/bin/Debug\ v3.5/AWS.SessionProvider.dll lib
cp aws-sdk-net/AWS.Extensions/TraceListener/bin/Debug\ v3.5/AWS.TraceListener.dll lib
cp aws-sdk-net/AWSSDK_DotNet35/bin/Debug/AWSSDK.dll lib

8. Create a command line program for example in s3.cs:
using System;
using Amazon.S3;
using Amazon.S3.Model;

class Upload
{
  public static void Main(string[] args)
  {
    // Create a client
    AmazonS3Client client = new AmazonS3Client(Amazon.RegionEndpoint.USWest2);

    // Create a PutObject request
    PutObjectRequest request = new PutObjectRequest
    {
      BucketName = "SampleBucket",
      Key = "Item1",
      ContentBody = "This is sample content..."
    };

    // Put object
    PutObjectResponse response = client.PutObject(request);
  }
}

9. Compile your program:
mcs s3.cs -r:./lib/AWSSDK.dll -r:./lib/AWS.TraceListener.dll -r:./lib/AWS.SessionProvider.dll

10. Set the Mono path
export MONO_PATH=`pwd`/lib:.

11. Create an application configuration (must be named to match the compiled program) for example in s3.exe.config:
<?xml version="1.0"?>
<configuration>
    <appSettings>
        <add key="AWSAccessKey" value="AKXXXXXXXXXXXXXX"/>
        <add key="AWSSecretKey" value="XXXXXXXXXXXXXXXXXXXXXXXX"/>
    </appSettings>
</configuration>

12. Run your program:
mono s3.exe

Of course there is also an easier approach that does not require the SDK to be compiled, you can just copy the DLLs from a Windows machine that has the SDK installed (typically into C:\Program Files (x86)\AWS SDK for .NET\bin\Net35). This allows you to skip steps 3 - 7.