.NET/C#でAmazonS3とデータをやりとりする

NuGetでAWSSDK.S3をインストール。


  • AccessKeyとSecretAccessKeyはAmazonの設定
  • RegionEndPointはS3のリージョンを指定
  • BucketNameは作ったBucketの名前
  • Keyはファイル名とか

using System.IO;
using System.Threading.Tasks;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
namespace UseAmazonS3
{
class Program
{
static async Task Main(string[] args)
{
var client = new AmazonS3Client(
"*AccessKey*",
"*SecretAccessKey*",
new AmazonS3Config
{
RegionEndpoint = RegionEndpoint.APNortheast1
});
// Upload
var response = await client.GetObjectAsync(new GetObjectRequest
{
BucketName = "*BucketName*",
Key = "*KeyName*"
});
var stream = new MemoryStream();
response.ResponseStream.CopyTo(stream);
File.WriteAllBytes(@"c:\temp\*Keyname*", stream.ToArray());
// Download
await client.PutObjectAsync(new PutObjectRequest
{
BucketName = "*BucketName*",
Key = "*KeyName*",
InputStream = new MemoryStream(File.ReadAllBytes(@"c:\temp\*KeyName*"))
});
}
}
}
view raw UseAmazonS3.cs hosted with ❤ by GitHub