こんにちは。
Visual Studio 2017 Communityを使ってプログラミングしています。
OSはWindows10です。
###前提・実現したいこと
AWSのS3とファイル(txt, jpg, zip)をやりとりしたいです。
###試したこと
AWSのアクセスキーとシークレットアクセスキーを用意しました。
https://dev.classmethod.jp/cloud/aws_s3_client_csharp/
を参考に、プロジェクトを作りました。
NuGetでAWSSDK2.3.55.2をインストールしました。
###発生している問題・エラーメッセージ
(1)エラーメッセージ
TransferUtilityUploadRequestにWithBucketNameが含まれていないので参照が不足している。
ビルドできない。
(2)AWSのアクセスキーとシークレットアクセスキーの記述方法がわからない。
###該当のソースコード
C#
1using Amazon; 2using Amazon.S3; 3using Amazon.S3.Model; 4using Amazon.S3.Transfer; 5using System; 6using System.IO; 7using System.Text; 8 9namespace ConsoleApp1 10{ 11 class Program 12 { 13 public static void Main(string[] args) 14 { 15 Console.Write(GetServiceOutput(args)); 16 Console.Read(); 17 } 18 19 public static string GetServiceOutput(string[] args) 20 { 21 StringBuilder sb = new StringBuilder(1024); 22 using (StringWriter sr = new StringWriter(sb)) 23 { 24 var s3 = AWSClientFactory.CreateAmazonS3Client(RegionEndpoint.APNortheast1); //一時証明書を用いない場合 25 26 string bucketName = "mybucketname"; 27 string targetLocalFolder = @"C:\Users\username\Desktop\"; 28 string downloadObjectKey = @"sample.txt"; 29 string uploadObjectKey = @"sample.txt"; 30 long partSize = 1 * 1024 * 1024; // 1MB 31 int timeout = 5 * 60 * 1000; // 5min 32 33 try 34 { 35 // List Objects 36 var listObjectRequest = new ListObjectsRequest(); 37 listObjectRequest.BucketName = bucketName; 38 var response = s3.ListObjects(listObjectRequest); 39 40 foreach (var item in response.S3Objects) 41 { 42 sr.WriteLine("Object Name: " + item.Key); 43 } 44 45 // Put Objects 46 using (TransferUtility transferUtility = new TransferUtility(s3)) 47 { 48 TransferUtilityUploadRequest request = new TransferUtilityUploadRequest() 49 .WithBucketName(bucketName) 50 .WithKey(uploadObjectKey) 51 .WithFilePath(targetLocalFolder + uploadObjectKey) 52 .WithPartSize(partSize) 53 .WithStorageClass(S3StorageClass.Standard) 54 .WithTimeout(timeout) 55 .WithSubscriber(UploadFileProgressCallback); 56 57 transferUtility.Upload(request); 58 sr.WriteLine("Upload Complete: " + uploadObjectKey); 59 } 60 61 //// Get Objects 62 //using (TransferUtility transferUtility = new TransferUtility(s3)) 63 //{ 64 // TransferUtilityDownloadRequest downloadRequest = new TransferUtilityDownloadRequest() 65 // .WithBucketName(bucketName) 66 // .WithKey(downloadObjectKey) 67 // .WithFilePath(targetLocalFolder + downloadObjectKey) 68 // .WithTimeout(timeout) 69 // .WithSubscriber(DownloadFileProgressCallback); 70 71 // transferUtility.Download(downloadRequest); 72 // sr.WriteLine("Download Complete: " + downloadObjectKey); 73 //} 74 } 75 catch (AmazonS3Exception ex) 76 { 77 if (ex.ErrorCode != null && (ex.ErrorCode.Equals("InvalidAccessKeyId") || 78 ex.ErrorCode.Equals("InvalidSecurity"))) 79 { 80 sr.WriteLine("Please check the provided AWS Credentials."); 81 sr.WriteLine("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3"); 82 } 83 else 84 { 85 sr.WriteLine("Caught Exception: " + ex.Message); 86 sr.WriteLine("Response Status Code: " + ex.StatusCode); 87 sr.WriteLine("Error Code: " + ex.ErrorCode); 88 sr.WriteLine("Request ID: " + ex.RequestId); 89 //sr.WriteLine("XML: " + ex.XML); 90 } 91 } 92 sr.WriteLine("Press any key to continue..."); 93 } 94 95 return sb.ToString(); 96 } 97 98 static void DownloadFileProgressCallback(object sender, WriteObjectProgressArgs e) 99 { 100 } 101 102 static void UploadFileProgressCallback(object sender, UploadProgressArgs e) 103 { 104 } 105 106 } 107}
###補足情報(言語/FW/ツール等のバージョンなど)
Visual Studio 2017 Community 15.3.3
.NET Framework 4.7.02053
Windows10 home
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。