文章主要針對(duì)于博主自己的技術(shù)棧,從Unity的角度出發(fā),對(duì)于 DynamoDB 的使用。
綠色通道:
WS SDK for .NET Version 3 API Reference - AmazonDynamoDBClient
Amazon DynamoDB
Amazon DynamoDB is a fast, highly scalable, highly available, cost-effective, non-relational database service. DynamoDB removes traditional scalability limitations on data storage while maintaining low latency and predictable performance.
Amazon DynamoDB 是一種快速、高度可擴(kuò)展、高度可用、經(jīng)濟(jì)高效的非關(guān)系數(shù)據(jù)庫(kù)服務(wù)。 DynamoDB 消除了數(shù)據(jù)存儲(chǔ)的傳統(tǒng)可擴(kuò)展性限制,同時(shí)保持低延遲和可預(yù)測(cè)的性能。
集成使用
1、下載SDK
參考地址:https://docs.aws.amazon.com/mobile/sdkforunity/developerguide/what-is-unity-plugin.html
下載鏈接:https://sdk-for-net.amazonwebservices.com/latest/aws-sdk-unity.zip
AWS Mobile SDK for Unity
這個(gè)SDK 包涵了 Amazon DynamoDB 的相關(guān)內(nèi)容。所以直接下載整個(gè) AWS Mobile SDK for Unity 即可。
下載之后是這樣的,我們只用 AWSSDK.DynamoDBv2.3.3.106.47.unitypackage 這個(gè)就可以,其余的是其他模塊的SDK,不用理會(huì)。
2、集成指南
使用通常的方法導(dǎo)入上面下載的 unitypackage,
使用 SDK 的功能性 API 之前需要進(jìn)行一系列的準(zhǔn)備工作。
首先初始化代碼,一般在場(chǎng)景啟動(dòng)時(shí)候就可以調(diào)用。將代碼放到你自己的腳本 Start 或者 Awake中
UnityInitializer.AttachToGameObject(this.gameObject);
3、參數(shù)
使用DB之前需要準(zhǔn)備到3個(gè)參數(shù)
private string IdentityPoolId = "<**>";
private string CognitoPoolRegion = RegionEndpoint.USEast2.SystemName;
private string DynamoRegion = RegionEndpoint.USEast2.SystemName;
IdentityPoolId: 身份池id
這個(gè)身份池id代表了訪問(wèn)數(shù)據(jù)庫(kù)的權(quán)限標(biāo)識(shí),關(guān)聯(lián)在您的AWS賬號(hào)的身份池中(配置通道) 如果沒(méi)有需要先創(chuàng)建一個(gè) identityPool,創(chuàng)建過(guò)程中需要配置訪問(wèn)權(quán)限,
可配置的權(quán)限訪問(wèn)類型有:
Authenticated access:有身份認(rèn)證的訪問(wèn)
Guest access:訪客訪問(wèn)
我這里直接使用 Guest access 訪問(wèn)數(shù)據(jù)庫(kù)就可以了。
每一個(gè)訪問(wèn)類型都需要?jiǎng)?chuàng)建一個(gè)Role(權(quán)限身份)
并且權(quán)限身份需要配置 權(quán)限策略(Permissions policies)
Permissions policies是一個(gè)遠(yuǎn)端維護(hù)的 json 文件,修改也可以直接在 AWS 控制臺(tái)操作。
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"cognito-identity:GetCredentialsForIdentity",
"dynamodb:DeleteItem",
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:Scan",
"dynamodb:UpdateItem",
"dynamodb:DescribeTable",
"dynamodb:Query"
],
"Resource": [
"*",
"arn:aws:dynamodb:us-east-2:768281874750:table/TestLei"
]
}
]
}
權(quán)限項(xiàng)目說(shuō)明:
“dynamodb:DeleteItem”,刪除表格項(xiàng)的權(quán)限
如果沒(méi)有配置對(duì)應(yīng)的權(quán)限,在操作表格的時(shí)候就會(huì)被提示沒(méi)有對(duì)應(yīng)的權(quán)限,返回錯(cuò)誤。
CognitoPoolRegion:身份池地區(qū)
一般IdentityPoolId最前面會(huì)有地區(qū)字符類似:
us-east-2:XXXXXXXXXXXXX
DynamoRegion:數(shù)據(jù)庫(kù)地區(qū),這個(gè)一般使用服務(wù)配置地區(qū)就可以。
腳本編寫(xiě)
private string IdentityPoolId = "";
private string CognitoPoolRegion = RegionEndpoint.USEast2.SystemName;
private string DynamoRegion = RegionEndpoint.USEast2.SystemName;
private RegionEndpoint _CognitoPoolRegion
{
get { return RegionEndpoint.GetBySystemName(CognitoPoolRegion); }
}
private RegionEndpoint _DynamoRegion
{
get { return RegionEndpoint.GetBySystemName(DynamoRegion); }
}
1、創(chuàng)建 AWSCredentials
AWSCredentials credentials = new CognitoAWSCredentials(IdentityPoolId, _CognitoPoolRegion);
2、使用AWSCredentials 創(chuàng)建 IAmazonDynamoDB (Client)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-852740.html
IAmazonDynamoDB ddbClient = new AmazonDynamoDBClient(Credentials, _DynamoRegion);
3、執(zhí)行查詢操作文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-852740.html
Client.DescribeTableAsync(request, (result) =>
{
if (result.Exception != null)
{
_displayText.text += $"Exception! info:\t\n";
_displayText.text += result.Exception.Message;
Debug.Log(result.Exception);
return;
}
var response = result.Response;
TableDescription description = response.Table;
_displayText.text += ("Name: " + description.TableName + "\n");
_displayText.text += ("# of items: " + description.ItemCount + "\n");
_displayText.text += ("Provision Throughput (reads/sec): " +
description.ProvisionedThroughput.ReadCapacityUnits + "\n");
_displayText.text += ("Provision Throughput (reads/sec): " +
description.ProvisionedThroughput.WriteCapacityUnits + "\n");
}, null);
到了這里,關(guān)于關(guān)于 Amazon DynamoDB 的學(xué)習(xí)和使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!