aws-sdk-cpp相关(aws dynamodb/s3/sqs c++用法)

1、安装aws-sdk-cpp

git clone https://github.com/aws/aws-sdk-cpp.git
cd aws-sdk-cpp/
mkdir build
cd build/
cmake .. # cmake -DBUILD_ONLY="s3;dynamodb" ..只编译s3,dynampdb
make # make -j8
sudo make install # 头文件位于/usr/local/include,动态库文件位于/usr/local/lib

2、dynamodb使用

官方例子:https://docs.amazonaws.cn/sdk-for-cpp/v1/developer-guide/examples-dynamodb.html

dynamodb保留的关键字:https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html

dynamodb.cc: 

#include <iostream>
#include "aws/core/Aws.h"
#include "aws/core/auth/AWSCredentialsProvider.h"
#include "aws/core/utils/Outcome.h"
#include "aws/dynamodb/DynamoDBClient.h"
#include "aws/dynamodb/model/AttributeValue.h"
#include "aws/dynamodb/model/AttributeValueUpdate.h"
#include "aws/dynamodb/model/DescribeTableRequest.h"
#include "aws/dynamodb/model/PutItemRequest.h"
#include "aws/dynamodb/model/GetItemRequest.h"
#include "aws/dynamodb/model/UpdateItemRequest.h"
#include "aws/dynamodb/model/QueryRequest.h"
#include "aws/dynamodb/model/ScanRequest.h"
#include "aws/dynamodb/model/DeleteItemRequest.h"
#include "aws/dynamodb/model/BatchWriteItemRequest.h"
#include "aws/dynamodb/model/BatchGetItemRequest.h"

using AttributeValue = Aws::DynamoDB::Model::AttributeValue;
using AttributeValueUpdate = Aws::DynamoDB::Model::AttributeValueUpdate;

std::string table = "alan_test";

auto NumberToAttribute = [](auto num) {
    AttributeValue av;
    av.SetN(std::to_string(num).c_str());
    return av;
};

AttributeValue StringToAttribute(const std::string &str) {
  AttributeValue av;
  av.SetS(Aws::String(str.c_str(), str.size()));
  return av;
}

AttributeValueUpdate AttributeToUpdate(AttributeValue av) {
  AttributeValueUpdate avu;
  avu.SetValue(av);
  return avu;
};

void PrintItem(const Aws::Map<Aws::String, AttributeValue>& item) {
  // id
  int id_ = 0;
  if (item.count("id")) {
    id_ = atoi(item.find("id")->second.GetN().c_str());
    // id_ = atoi(item.at("id").GetN().c_str());
  }
  // class
  int class_ = 0;
  if (item.count("class")) {
    class_ = atoi(item.find("class")->second.GetN().c_str());
  }
  // name
  std::string name_ = "";
  if (item.count("name"))
    name_ = item.find("name")->second.GetS().c_str();

  std::cout << "id: " << id_ << ", class: " << class_ << ", name: " << name_ << std::endl;
}

int main(int argc, char* argv[])
{
  Aws::SDKOptions options;
  // options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Trace;
  Aws::InitAPI(options);

  Aws::Auth::AWSCredentials credentials("access key", "secret key");
  Aws::Client::ClientConfiguration client_config;
  client_config.region = "cn-northwest-1";
  // client_config.requestTimeoutMs = 300000;
  // client_config.endpointOverride = "localhost:8000";
  // client_config.endpointOverride = "10.202.91.2:88";  // dynamodb服务器地址和端口
  // client_config.scheme = Aws::Http::Scheme::HTTPS;
  Aws::DynamoDB::DynamoDBClient dynamodb_client(credentials, client_config);
  std::cout << "success initial !" << std::endl;
  getchar();
  //----------------------------------------------------------------------------
  {
    // check if the table exists
    Aws::DynamoDB::Model::DescribeTableRequest request;
    request.SetTableName(table.c_str());
    auto result = dynamodb_client.DescribeTable(request);
    if (!result.IsSuccess()) {
      std::cout << "Fail to open table " << table.c_str() << ": "
                << result.GetError().GetExceptionName().c_str() << "\n"
                << result.GetError().GetMessage().c_str();
    }
  }
  std::cout << "success check table !" << std::endl;
  getchar();
  //----------------------------------------------------------------------------
  {
    // put item
    Aws::DynamoDB::Model::PutItemRequest request;
    request.SetTableName(table.c_str());
    request.AddItem("id", NumberToAttribute(1));
    request.AddItem("class", NumberToAttribute(4));
    request.AddItem("name", StringToAttribute("alan"));
    // Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> item;
    // // id.
    // item.insert(std::make_pair("id", AttributeValue("test.bag")));
    // // timestamp
    // AttributeValue timestamp_av;
    // timestamp_av.SetN("10010");
    // item.insert(std::make_pair("timestamp", timestamp_av));

    // // status
    // item.insert(std::make_pair("status", AttributeValue("unknow")));
    // request.SetItem(item);
    auto result = dynamodb_client.PutItem(request);
    if (!result.IsSuccess()) {
      std::cout << "Fail to put item: "
                << result.GetError().GetExceptionName().c_str() << "\n"
                << result.GetError().GetMessage().c_str() << std::endl;
    }
  }
  std::cout << "success put item !" << std::endl;
  getchar();
  //----------------------------------------------------------------------------
  {
    // get item(指定主键和排序键,item唯一)(get 1 item)
    // Aws::DynamoDB::Model::AttributeValue attribute_value;
    // attribute_value.SetS("test.bag");
    // get_request.SetKey({
  {"id", attribute_value}});
    Aws::DynamoDB::Model::GetItemRequest request;
    request.SetConsistentRead(true);
    request.SetTableName(table.c_str());
    request.SetExpressionAttributeNames({
  {"#name", "name"},
                                         {"#class", "class"}});

    Aws::StringStream stream;
    stream << "id" << ", " << "#name" << ", " << "#class";
    request.SetProjectionExpression(stream.str());

    request.AddKey("id", NumberToAttribute(1));
    request.AddKey("class", NumberToAttribute(4));

    auto result = dynamodb_client.GetItem(request); // Aws::DynamoDB::Model::GetItemOutcome result
    if (!result.IsSuccess() || result.GetResult().GetItem().size() == 0) {
      std::cout << "Fail to get item: "
                << result.GetError().GetExceptionName().c_str() << "\n"
                << result.GetError().GetMessage().c_str();
    }
    auto item = result.GetResult().GetItem();
    PrintItem(item);
  }
  std::cout << "success get item !" << std::endl;
  getchar();
  //----------------------------------------------------------------------------
  {
    // update item(不能更新主键和排序键)
    Aws::DynamoDB::Model::UpdateItemRequest request;
    request.SetTableName(table.c_str());

    request.AddKey("id", NumberToAttribute(1));
    request.AddKey("class", NumberToAttribute(4));
    // update_item_req.AddKey("id", AttributeValue("test.bag"));
    // update_item_req.AddKey("name", AttributeValue("alan"));
    request.AddAttributeUpdates("name", AttributeToUpdate(StringToAttribute("Alan")));

    // // id
    // AttributeValue id_av("good");
    // AttributeValueUpdate id_avu;
    // id_avu.SetValue(id_av);
    // update_item_req.AddAttributeUpdates("status", id_avu);
    // // time_start
    // Attri
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值