4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWS DynamoDB, S3, SQS を .NET Core で使うコードスニペット集

Last updated at Posted at 2020-09-04

AWS で比較的使われることが多い DynamoDB, S3, SQS を .NET Core & C# で使う際のコードスニペットをまとめました。

Amazon DynamoDB

DynamoDBに関するコードスニペットは、AWSSDK.DynamoDBv2パッケージと、以下通りの名前空間の参照とSampleTableItemクラスの定義を使用することを前提としています。

// dotnet add package AWSSDK.DynamoDBv2
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
using Amazon.DynamoDBv2.DataModel;
using Amazon.DynamoDBv2.DocumentModel;

[DynamoDBTable("sample-table")]
class SampleTableItem
{
    [DynamoDBHashKey("pk")]
    public int PartitionKey { get; set; }

    [DynamoDBRangeKey("sk")]
    public string SortKey { get; set; }

    [DynamoDBProperty("prop")]
    public string Property { get; set; }
}

Scan (全件取得)

var client = new AmazonDynamoDBClient();
var context = new DynamoDBContext(client);
var items = await context.ScanAsync<SampleTableItem>(null).GetRemainingAsync();

Query (検索取得)

指定したPartitionKeyの項目をすべて取得

var client = new AmazonDynamoDBClient();
var context = new DynamoDBContext(client);
var partitionKey = 0;
var items = await context.QueryAsync<SampleTableItem>(partitionKey).GetRemainingAsync();

指定したPartitionKeyとSortKeyでの範囲検索

var client = new AmazonDynamoDBClient();
var context = new DynamoDBContext(client);
// PartitionKeyの値が0で、SortKeyがSampleから始まるあるすべてのItemを取得する
var partitionKey = 0;
var sortKeys = new[] { "Sample" };
var items = await context.QueryAsync<SampleTableItem>(partitionKey, QueryOperator.BeginsWith, sortKeys).GetRemainingAsync();

Query (GSIの検索取得)

var client = new AmazonDynamoDBClient();
var context = new DynamoDBContext(client);
const string gsiPartitionKey = "SampleSortKey";
const string indexName = "SampleIndex";
var filter = new QueryFilter("sk", QueryOperator.Equal, gsiPartitionKey);
var config = new QueryOperationConfig
{
    Filter = filter,
    IndexName = indexName
};
var items = await context.QueryAsync<SampleTableItem>(config).GetRemainingAsync();

GSI: Global Secondary Index

GetItem (要素の取得)

var client = new AmazonDynamoDBClient();
var context = new DynamoDBContext(client);
const int partitionKey = 0;
const string sortKey = "SampleSortKey";
var item = await context.LoadAsync<SampleTableItem>(partitionKey, sortKey);

BatchGetItem 処理 (複数要素をまとめて取得)

var client = new AmazonDynamoDBClient();
var context = new DynamoDBContext(client);
var batch = context.CreateBatchGet<SampleTableItem>();
batch.AddKey(0, "SampleSortKey1");
batch.AddKey(0, "SampleSortKey2");
await batch.ExecuteAsync();
var items = batch.Results;

UpdateItem 処理 (追加・更新処理)

var client = new AmazonDynamoDBClient();
var context = new DynamoDBContext(client);
var item = new SampleTableItem
{
    PartitionKey = 0,
    SortKey = "SampleSortKey",
    Property = "Value"
};
await context.SaveAsync(item);

DeleteItem 処理 (削除処理)

var client = new AmazonDynamoDBClient();
var context = new DynamoDBContext(client);
const int partitionKey = 0;
const string sortKey = "SampleSortKey";
await context.DeleteAsync<SampleTableItem>(partitionKey, sortKey);

BatchWriteItem 処理 (複数要素をまとめて更新)

var client = new AmazonDynamoDBClient();
var context = new DynamoDBContext(client);
var batch = context.CreateBatchWrite<SampleTableItem>();
var items = System.Linq.Enumerable.Range(0, 25).Select(i => new SampleTableItem
{
    PartitionKey = 0,
    SortKey = $"SampleSortKey{i}",
    Property = $"Value{i}"
});
batch.AddPutItems(items);
await batch.ExecuteAsync();

TransactWriteItems (トランザクションでの更新処理)

var client = new AmazonDynamoDBClient();
var request = new TransactWriteItemsRequest();
request.TransactItems = new List<TransactWriteItem>();
request.TransactItems.Add(new TransactWriteItem
{
    Put = new Put
    {
        TableName = "sample-table",
        Item = new Dictionary<string, AttributeValue>
        {
            { "pk", new AttributeValue { N = "0" } },
            { "sk", new AttributeValue { S = "SampleSortKey1" } },
            { "prop", new AttributeValue { S = "Value" } }
        }
    }
});
request.TransactItems.Add(new TransactWriteItem
{
    Delete = new Delete
    {
        TableName = "sample-table",
        Key = new Dictionary<string, AttributeValue>
        {
            { "pk", new AttributeValue { N = "0" } },
            { "sk", new AttributeValue { S = "SampleSortKey2" } }
        }
    }
});
await client.TransactWriteItemsAsync(request);

Amazon DynamoDB トランザクション: 仕組み
https://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/transaction-apis.html

Amazon S3

S3に関するスニペットはAWSSDK.S3パッケージと以下の名前空間を使用します。

// dotnet add package AWSSDK.S3
using Amazon.S3;
using Amazon.S3.Transfer;

アップロード処理

アップロードするファイルのパスを指定してアップロード

var client = new AmazonS3Client();
var transfer = new TransferUtility(client);

const string bucketName = "sample-bucket";
const string key = "sample-uploaded-file.txt";
const string uploadFilePath = "sample-uploaded-file.txt";
await transfer.UploadAsync(uploadFilePath, bucketName, key);

アップロードするデータのバイナリを指定してアップロード

var client = new AmazonS3Client();
var transfer = new TransferUtility(client);

const string bucketName = "sample-bucket";
const string key = "sample-uploaded-file.txt";
var bytes = System.Text.Encoding.UTF8.GetBytes("Hello S3 !");
using (var stream = new MemoryStream(bytes))
{
    await transfer.UploadAsync(stream, bucketName, key);
}

ダウンロード処理

ダウンロードしてファイルとして保存する

var client = new AmazonS3Client();
var transfer = new TransferUtility(client);

const string bucketName = "sample-bucket";
const string key = "sample-uploaded-file.txt";
const string downloadFilePath = "s3_sample_download.txt";
await transfer.DownloadAsync(downloadFilePath, bucketName, key);

ダウンロードしたファイルをメモリ上で扱う

var client = new AmazonS3Client();

const string bucketName = "sample-bucket";
const string key = "sample-uploaded-file.txt";
var s3Object = await client.GetObjectAsync(bucketName, key);
using (var stream = new StreamReader(s3Object.ResponseStream))
{
    var text = await stream.ReadToEndAsync();
}

署名付きURLの発行

const string bucketName = "sample-bucket";
const string key = "sample-uploaded-file.txt";

var client = new AmazonS3Client();
var request = new Amazon.S3.Model.GetPreSignedUrlRequest
{
    BucketName = bucketName,
    Key = key,
    Verb = HttpVerb.GET,
    Expires = DateTime.UtcNow.AddMinutes(1)
};
var presignedUrl = client.GetPreSignedURL(request);

署名付き URL を使用したオブジェクトのアップロード
https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/dev/PresignedUrlUploadObject.html

Amazon SQS

S3に関するスニペットはAWSSDK.S3パッケージと以下の名前空間を使用します。

// dotnet add package AWSSDK.SQS
using Amazon.SQS;
using Amazon.SQS.Model;

SendMessage 処理 (エンキュー)

var client = new AmazonSQSClient();
const string queueUrl = "https://sqs.region-name.amazonaws.com/0000000000/your-queue-name";
await client.SendMessageAsync(queueUrl, "Hello SQS !");

SendMessageBatch 処理 (まとめてのエンキュー)

var client = new AmazonSQSClient();
const string queueUrl = "https://sqs.region-name.amazonaws.com/0000000000/your-queue-name";
var messages = System.Linq.Enumerable.Range(0, 5).Select(i => new SendMessageBatchRequestEntry
{
    Id = i.ToString(),
    MessageBody = "Hello SQS !! - " + i,
}).ToList();
await client.SendMessageBatchAsync(queueUrl, messages);

ReceiveMessage 処理 (デキュー)

var client = new AmazonSQSClient();
const string queueUrl = "https://sqs.region-name.amazonaws.com/0000000000/your-queue-name";
var receiveMessageRequest = new ReceiveMessageRequest
{
    QueueUrl = queueUrl,
    MaxNumberOfMessages = 10
};
var resp = await client.ReceiveMessageAsync(receiveMessageRequest);
var messages = resp.Messages;
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?