[AWS]用AWS SDK for .NET 上傳檔案至S3

| Comments

AWS的中文介紹雖然沒有很少,但是AWS SDK for .NET的介紹少之又少,所以只能用破破的英文來看一些原文資料,希望可以藉由分享讓大家可以比較快進入AWS的世界。

NOTE.
AWS SDK for .NET:http://aws.amazon.com/sdkfornet/ AWS SDK for .NET Documentation :http://aws.amazon.com/documentation/sdkfornet/ 基本介紹:[AWS][C#]Hello World for .NET
 1: NameValueCollection appConfig = ConfigurationManager.AppSettings;
 
 2: AmazonS3 s3Client = AWSClientFactory.CreateAmazonS3Client(
 
 3:            appConfig["AWSAccessKey"],
 
 4:            appConfig["AWSSecretKey"]
 
 5:            );
 
 6: ListBucketsResponse response = s3Client.ListBuckets();
 
 7: Console.WriteLine("---------S3 Bucket 列表---------");
 
 8: foreach (var item in response.Buckets)
 
 9: {
 
 10:     Console.WriteLine("Bucket名稱:" + item.BucketName);
 
 11: }
 
 12: //上傳資料 
 
 13: PutObjectRequest ObjectRequest = new PutObjectRequest();
 
 14: ObjectRequest.WithBucketName("***")//放置Bucket位置
 
 15:     .WithCannedACL(S3CannedACL.PublicRead)//存取原則
 
 16:     .WithFilePath(@"d:\456.txt")//上傳檔案位置
 
 17:     .WithKey("456.txt");//在AWS上的檔案名稱
 
 18:
 
 19: S3Response myResponse = s3Client.PutObject(ObjectRequest);
 
 20: //上傳資料end
 
 21: Console.WriteLine("上傳完成");
 
 22: Console.Read();
 

Comments