示例:使用 Amazon SDK for .NET 低级 API 处理二进制类型属性 - Amazon DynamoDB
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

示例:使用 Amazon SDK for .NET 低级 API 处理二进制类型属性

以下 C# 代码示例介绍如何处理二进制类型属性。示例介绍将项目添加到 Reply 表。项目包含存储压缩数据的二进制类型属性 (ExtendedMessage)。然后,示例检索该项目,并打印所有属性值。为方便说明,该示例使用 GZipStream 类压缩示例数据流,分配至 ExtendedMessage 属性,输出属性值时解压缩。

如果您执行了 为 DynamoDB 中的代码示例创建表和加载数据 中的步骤,则您应已创建 Reply 表。您还能够以编程方式创建这些样本表。有关更多信息,请参见 创建示例表并使用 Amazon SDK for .NET 上传数据

有关测试以下示例的 step-by-step 说明,请参阅.NET 代码示例

using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using Amazon.DynamoDBv2; using Amazon.DynamoDBv2.Model; using Amazon.Runtime; namespace com.amazonaws.codesamples { class LowLevelItemBinaryExample { private static string tableName = "Reply"; private static AmazonDynamoDBClient client = new AmazonDynamoDBClient(); static void Main(string[] args) { // Reply table primary key. string replyIdPartitionKey = "Amazon DynamoDB#DynamoDB Thread 1"; string replyDateTimeSortKey = Convert.ToString(DateTime.UtcNow); try { CreateItem(replyIdPartitionKey, replyDateTimeSortKey); RetrieveItem(replyIdPartitionKey, replyDateTimeSortKey); // Delete item. DeleteItem(replyIdPartitionKey, replyDateTimeSortKey); Console.WriteLine("To continue, press Enter"); Console.ReadLine(); } catch (AmazonDynamoDBException e) { Console.WriteLine(e.Message); } catch (AmazonServiceException e) { Console.WriteLine(e.Message); } catch (Exception e) { Console.WriteLine(e.Message); } } private static void CreateItem(string partitionKey, string sortKey) { MemoryStream compressedMessage = ToGzipMemoryStream("Some long extended message to compress."); var request = new PutItemRequest { TableName = tableName, Item = new Dictionary<string, AttributeValue>() { { "Id", new AttributeValue { S = partitionKey }}, { "ReplyDateTime", new AttributeValue { S = sortKey }}, { "Subject", new AttributeValue { S = "Binary type " }}, { "Message", new AttributeValue { S = "Some message about the binary type" }}, { "ExtendedMessage", new AttributeValue { B = compressedMessage }} } }; client.PutItem(request); } private static void RetrieveItem(string partitionKey, string sortKey) { var request = new GetItemRequest { TableName = tableName, Key = new Dictionary<string, AttributeValue>() { { "Id", new AttributeValue { S = partitionKey } }, { "ReplyDateTime", new AttributeValue { S = sortKey } } }, ConsistentRead = true }; var response = client.GetItem(request); // Check the response. var attributeList = response.Item; // attribute list in the response. Console.WriteLine("\nPrinting item after retrieving it ............"); PrintItem(attributeList); } private static void DeleteItem(string partitionKey, string sortKey) { var request = new DeleteItemRequest { TableName = tableName, Key = new Dictionary<string, AttributeValue>() { { "Id", new AttributeValue { S = partitionKey } }, { "ReplyDateTime", new AttributeValue { S = sortKey } } } }; var response = client.DeleteItem(request); } private static void PrintItem(Dictionary<string, AttributeValue> attributeList) { foreach (KeyValuePair<string, AttributeValue> kvp in attributeList) { string attributeName = kvp.Key; AttributeValue value = kvp.Value; Console.WriteLine( attributeName + " " + (value.S == null ? "" : "S=[" + value.S + "]") + (value.N == null ? "" : "N=[" + value.N + "]") + (value.SS == null ? "" : "SS=[" + string.Join(",", value.SS.ToArray()) + "]") + (value.NS == null ? "" : "NS=[" + string.Join(",", value.NS.ToArray()) + "]") + (value.B == null ? "" : "B=[" + FromGzipMemoryStream(value.B) + "]") ); } Console.WriteLine("************************************************"); } private static MemoryStream ToGzipMemoryStream(string value) { MemoryStream output = new MemoryStream(); using (GZipStream zipStream = new GZipStream(output, CompressionMode.Compress, true)) using (StreamWriter writer = new StreamWriter(zipStream)) { writer.Write(value); } return output; } private static string FromGzipMemoryStream(MemoryStream stream) { using (GZipStream zipStream = new GZipStream(stream, CompressionMode.Decompress)) using (StreamReader reader = new StreamReader(zipStream)) { return reader.ReadToEnd(); } } } }