AWS SDK for .NETをつかってWindowsアプリからS3にファイルを転送する

AWS SDK for .NET をインストールしておく

・ VisualStudioで.NetFramework3.5以上のソリューションを新規作成

・ VisualStudioのプロジェクト→NuGet パッケージの管理

・ 検索ボックスに「AWSSDK」と入力して検索し、AWSSDK.S3をインストール

・ メインのソースコードはこんな感じ

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;

namespace S3Sample
{
    class Program
    {
        static IAmazonS3 client;
        static string bucketName = "yourbucket";
        static string profileName = "default";
        static string accessKey = "XXXXXXX";
        static string secretKey = "XXXXXXX";

        static string srcPath = "path\\to\\filename";
        static string destPath = "path/to/filename";

        static void Main(string[] args)
        {
            Amazon.Util.ProfileManager.RegisterProfile(profileName, accessKey, secretKey);

            using (client = new AmazonS3Client(RegionEndpoint.APNortheast1))
            {
                Console.WriteLine("Uploading an object");
                WritingAnObject();
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
        static void WritingAnObject()
        {
            try
            {
                PutObjectRequest putRequest = new PutObjectRequest
                {
                    BucketName = bucketName,
                    Key = destPath,
                    FilePath = srcPath
                };

                PutObjectResponse response = client.PutObject(putRequest);
                Console.WriteLine("Status Code: " + response.HttpStatusCode);
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                if (amazonS3Exception.ErrorCode != null &&
                    (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
                    ||
                    amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    Console.WriteLine("Invalid AccessKey");
                }
                else
                {
                    Console.WriteLine(
                        "Error occurred. Message:'{0}' when writing an object"
                        , amazonS3Exception.Message);
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(
                       "Generic Error occurred. Message:'{0}' when writing an object"
                       , exception.Message);
            }
        }
    }
}