AWS SDK for .NETを使ってAmazon S3からファイルをダウロードしましょう。
自前で認証文字列を書く方法もあるのですが実装するのが大変なのであまりおススメしません。さて、ここではC#+WPFでの実装方法を紹介します、試していませんがWinFormsでも同じコードで実行できると思います。
まずはNuGetを使って「AWS SDK for .NET」をインストールしておきます。
ボタンをクリックすると、東京リージョンのバケット「data2.softbuild.jp」に保存している「directory/temp.jpg」というファイルをAmazon S3からダウンロードして、BitmapImageに読み込み、Imageコントロールで表示させています。
private void Button_Click(object sender, RoutedEventArgs e) { var accessKey = "YOUR ACCESS KEY"; var accessSecretKey = "YOUR ACCESS SECRET KEY"; var filePath = @"C:\Users\ch3cooh\Pictures\\temp.jpg"; var s3Client = AWSClientFactory.CreateAmazonS3Client(accessKey, accessSecretKey, RegionEndpoint.APNortheast1); var request = new GetObjectRequest() { BucketName = "data2.softbuild.jp", Key = "directory/temp.jpg" }; using (var response = s3Client.GetObject(request)) { response.WriteResponseStreamToFile(filePath); } var imageSource = new BitmapImage(); imageSource.BeginInit(); imageSource.UriSource = new Uri(filePath); imageSource.EndInit(); DownloadedImage.Source = imageSource; }
上記のコードを実行すると、temp.jpgがダウンロードされてImageコントロールに表示されているのが確認できると思います。