Skip to content

Latest commit

 

History

History
executable file
·
647 lines (510 loc) · 18.9 KB

helper_AWS-SDK.md

File metadata and controls

executable file
·
647 lines (510 loc) · 18.9 KB

AWS SDK

GitHub license GitHub stars GitHub forks GitHub issues GitHub watchers

Using AWS for troubleshooting and diagnostics

As you learn to develop applications with the AWS SDK for C++, it's also valuable to get comfortable in using both the AWS Management Console and the AWS CLI. These tools can be used interchangeably for various troubleshooting and diagnostics .

The following tutorial shows you an example of these troubleshooting and diagnostics tasks. It focuses on the Access denied error, which can be encountered for several different reasons. The tutorial shows an example of how you might determine the actual cause of the error. It focuses on two of the possible causes: incorrect permissions for the current user and a resource that isn't available to the current user.

要使用 AWS 服務,都需要先建立憑證;步驟很複雜,先跳過再研究

$ cat ~/.aws/credentials
$ cat ~/.aws/config

1.1.2. Logging

#include <aws/external/gtest.h>

#include <aws/core/utils/memory/stl/AWSString.h>
#include <aws/core/utils/logging/DefaultLogSystem.h>
#include <aws/core/utils/logging/AWSLogging.h>

#include <iostream>

int main(int argc, char** argv)
{
    Aws::Utils::Logging::InitializeAWSLogging(
        Aws::MakeShared<Aws::Utils::Logging::DefaultLogSystem>(
            "RunUnitTests", Aws::Utils::Logging::LogLevel::Trace, "aws_sdk_"));
    ::testing::InitGoogleTest(&argc, argv);
    int exitCode = RUN_ALL_TESTS();
    Aws::Utils::Logging::ShutdownAWSLogging();
    return exitCode;
}

官網提供的網頁很不友善,操作不易。

對於我熟悉 c 而不熟悉 cpp 的碼農,也是有些阻礙!

只能請大家把下面幾個目錄放 ctag 或是善用 Source Insight 等編輯工具來建立關聯。

最後如果你是完全沒有相關 c 或是 cpp 的工作經驗,建議請另外找工作吧!

aws-sdk-cpp/aws-cpp-sdk-core

aws-sdk-cpp/aws-cpp-sdk-dynamodb

aws-sdk-cpp/aws-cpp-sdk-iam

aws-sdk-cpp/aws-cpp-sdk-s3

aws-sdk-cpp/aws-cpp-sdk-sts

申請帳號、憑證後,才能使用相關服務,很複雜!很複雜!很複雜!

先假設大家都已經用過 AWS。

AWS 的文件有千萬篇(有些描述真的過於冗長,這就是我為什麼一直要寫文件,把事情簡單化),可是卻沒有說明當程式編譯完後,執行程式時要如何引用憑證。

aws-doc-sdk-examples/README.md

其實我也是遶了一大圈,最後得到一個結果 “AWS CLI” 可以正常執行,偉大的 SDK & examples 就可以執行。

所以聰明的您,從 4 開始看就可以了。

使用請三思,雖然是官方提供,但是支援度和更新並不是每個功能都能完整使用或編譯。GitHub 和 AWS 官網之間的文件有差異,請小心使用。

AWS 文件很多,很偉大!再加一句,我有閱讀 AWS 文件困難!

4.1. Download

$ git clone --recurse-submodules https://github.com/aws/aws-sdk-cpp

or

$ cd aws-sdk-cpp
$ git checkout main
$ git pull origin main
$ git submodule update --init --recursive
$ cd aws-sdk-cpp-1.10.9
# 請一定要執行
$ ./prefetch_crt_dependency.sh

The AWS SDK for C++ has a dependency on cJSON. This dependency was updated to version 1.7.14 in the recent SDK updates. We would recommend to upgrade your SDK to version 1.9.67 for 1.9.x or 1.8.187 for 1.8.x. Thank @dkalinowski for reporting this issue: #1594

4.1.1. code-generation/api-descriptions/*.json

ADD_CUSTOM_CLIENTS
BUILD_ONLY
BUILD_SHARED_LIBS
CPP_STANDARD
CURL_INCLUDE_DIR
CURL_LIBRARY
CUSTOM_MEMORY_MANAGEMENT
ENABLE_CURL_LOGGING
ENABLE_RTTI
ENABLE_TESTING
ENABLE_UNITY_BUILD
FORCE_CURL
FORCE_SHARED_CRT
G
MINIMIZE_SIZE
NO_ENCRYPTION
NO_HTTP_CLIENT
REGENERATE_CLIENTS
SIMPLE_INSTALL
TARGET_ARCH
USE_OPENSSL

4.2. Building the SDK

4.2.1. Cmake build options

A. All services

$ cd aws-sdk-cpp-1.10.9
$ mkdir build_xxx

$ cd build_xxx \
	&& cmake \
	-DCMAKE_BUILD_TYPE=Debug \
	..

B. BUILD_ONLY

# Builds only the clients you want to use.
# dynamodb, iam, S3 and sts
$ cd build_xxx \
	&& cmake \
	-DCMAKE_BUILD_TYPE=Debug \
	-DBUILD_ONLY="dynamodb;iam;s3;sts" \
	..

The core SDK module, aws-sdk-cpp-core, is always built, regardless of the value of the BUILD_ONLY parameter.

C. Myself

CMAKE_FLAGS += \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DBUILD_ONLY="dynamodb;iam;s3;sts"
		
cd $(SOURCE) \
&& $(PJ_SH_MKDIR) build_xxx \
&& cd build_xxx \
&& $(PJ_SH_CP) $(PJ_CMAKE_CROSS) ./build.cmake \
&& cmake \
	-DCMAKE_TOOLCHAIN_FILE=./build.cmake \
	-DCMAKE_INSTALL_PREFIX=$(SDK_ROOT_DIR) \
	-DCMAKE_PREFIX_PATH=$(SDK_ROOT_DIR) \
	$(CMAKE_FLAGS) \
	..
	#-DTARGET_ARCH=ANDROID
	#-DCMAKE_VERBOSE_MAKEFILE=ON \
	#-DCMAKE_MODULE_LINKER_FLAGS=-L$(SDK_LIB_DIR) \
	#-DCMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE=$(SDK_INC_DIR)
	#-DOPENSSL_ROOT_DIR=$(PJ_INSTALL_OPENSSL) \

4.2.2. Build

$ cd build_xxx \
	&& make
$ cd build_xxx \
	&& make install

4.3. Code examples

5. Examples

$ git clone --recurse-submodules https://github.com/awsdocs/aws-doc-sdk-examples.git

$ cd aws-doc-sdk-examples

# dynamodb
$ cd aws-doc-sdk-examples/cpp/example_code/dynamodb 

# s3
$ cd aws-doc-sdk-examples/cpp/example_code/s3 
$ mkdir build_xxx \
	&& cmake \
	..

Invoke example code

To invoke this example code, you must have an AWS account. For more information about creating an account, see AWS Free Tier.

You must also have AWS credentials configured. For steps on using the AWS Command Line Interface (AWS CLI) to configure credentials, see CLI Configuration basics

5.1.2. DynamoDB applications

$ ls -al aws-doc-sdk-examples/cpp/example_code/dynamodb/*
$ run_create_table_composite_key
Usage:
    run_create_table_composite_key <table> <optional:region>
Where:
    table - the table to create
Example:
    run_create_table_composite_key Music
$ run_create_table_composite_key Music
Creating table Music with a composite primary key:
* Artist - partition key
* SongTitle - sort key
Table "Music" was created!

$ run_create_table_composite_key Music
Creating table Music with a composite primary key:
* Artist - partition key
* SongTitle - sort key
Failed to create table:Table already exists: Music
$ run_describe_table
Usage:
    run_describe_table <region> <table>
Where:
    region - region.
    table - the table to describe.
Examples:
    run_describe_table ap-northeast-1 Music
$ run_describe_table ap-northeast-1 Music
Table name  : Music
Table ARN   : arn:aws:dynamodb:ap-northeast-1:123456789012:table/Music
Status      : ACTIVE
Item count  : 4
Size (bytes): 285
Throughput
  Read Capacity : 5
  Write Capacity: 5
Attributes
  Artist (S)
  SongTitle (S)
$ run_delete_item
Usage:
    run_delete_item <tableName> <Artist-keyValue> <SongTitle-keyValue>
Where:
    tableName - the table to delete the item from.
    Artist-keyValue - the key value to update
    SongTitle-keyValue - the key value to update
Example:
    run_delete_item Music Lanka Lanka520520
**Warning** This program will actually delete the item that you specify!
$ run_delete_item Music Lanka Lanka520520
Deleting item Artist: "Lanka", SongTitle: "Lanka520520" from table Music
Artist "Lanka", SongTitle: "Lanka520520" deleted!
$ run_delete_table
Usage:
     run_delete_table <tableName>
Where:
    tableName - the table to delete.
Example:
    delete_table Music
**Warning** This program will actually delete the table that you specify!
$ run_delete_table Music
Your Table "Music was deleted!
$ run_delete_table Music
Failed to delete table: Requested resource not found: Table: Music not found
Failed to get item: The provided key element does not match the schema

Attributes
  Artist (S)
  SongTitle (S)

需要完整輸入,Partition key (Artist) 和 Sort key (SongTitle);
原本的程式只有 Partition key (Artist)。
$ run_get_item
Usage:
    run_get_item <tableName> <pk> <pkval> <sk> <skval>
Where:
    tableName - the Amazon DynamoDB table from which an item is retrieved (for example, Music).
    pk - the key used in the Amazon DynamoDB table (for example, Artist).
    pkval - the key value that represents the item to get (for example, Acme Band).
    sk - the key used in the Amazon DynamoDB table (for example, SongTitle).
    skval - the key value that represents the item to get (for example, Happy Day).
Examples:
    run_get_item Music Artist Lanka SongTitle Lanka520520
$ run_get_item Music Artist 'Acme Band' SongTitle 'Happy Day'
Values: AlbumTitle: Songs About Life
Values: Artist: Acme Band
Values: Awards: 10
Values: SongTitle: Happy Day

$ run_get_item Music Artist 'Lanka' SongTitle 'Lanka520520'
No item found with the key Artist

list_tables.cpp

$ run_list_tables
Your DynamoDB Tables:
Music

put_item.cpp - remove item and update items

$ run_put_item
Usage:
    <tableName> <key> <keyVal> <albumtitle> <albumtitleval> <awards> <awardsval> <Songtitle> <songtitleval>

Where:
    tableName - the Amazon DynamoDB table in which an item is placed (for example, Music3).
    key - the key used in the Amazon DynamoDB table (for example, Artist).
    keyval - the key value that represents the item to get (for example, Famous Band).
    albumTitle - album title (for example, AlbumTitle).
    AlbumTitleValue - the name of the album (for example, Songs About Life ).
    Awards - the awards column (for example, Awards).
    AwardVal - the value of the awards (for example, 10).
    SongTitle - the song title (for example, SongTitle).
    SongTitleVal - the value of the song title (for example, Happy Day).
**Warning** This program will  place an item that you specify into a table!
$ run_put_item Music \
	Artist 'Lanka' \
	AlbumTitle "Lanka520" \
	Awards "1" \
	SongTitle "Lanka520520"
Successfully added Item!

$ aws dynamodb get-item --consistent-read \
    --table-name Music \
    --key '{ "Artist": {"S": "Lanka"}, "SongTitle": {"S": "Lanka520520"}}'
{
    "Item": {
        "AlbumTitle": {
            "S": "Lanka520"
        },
        "Awards": {
            "S": "1"
        },
        "Sponsor": {
            "L": [
                {
                    "S": "dog"
                },
                {
                    "S": "cat"
                },
                {
                    "S": "mouse"
                },
                {
                    "S": "stoat"
                },
                {
                    "S": "snake"
                }
            ]
        },
        "Artist": {
            "S": "Lanka"
        },
        "SongTitle": {
            "S": "Lanka520520"
        }
    }
}

query_items.cpp

$ run_query_items
Usage:
    query_items <table> <partitionKeyAttributeName>=<partitionKeyValue> [projection_expression]

Where:
    table - the table to get an item from.
    partitionKeyAttributeName  - Partition Key attribute of the table.
    partitionKeyValue  - Partition Key value to query.

Example:
    query_items HelloTable Name=Namaste
    query_items Players FirstName=Mike
    query_items SiteColors Background=white "default, bold"
$ run_query_items Music Artist='No One You Know'
Number of items retrieved from Query: 2
******************************************************
AlbumTitle: Somewhat Famous
Artist: No One You Know
Awards: 1
SongTitle: Call Me Today
******************************************************
AlbumTitle: Somewhat Famous
Artist: No One You Know
Awards: 2
SongTitle: Howdy

scan_table.cpp

$ run_scan_table
Usage:
    scan_table <table> [projection_expression]

Where:
    table - the table to Scan.

You can add an optional projection expression (a quote-delimited,
comma-separated list of attributes to retrieve) to limit the
fields returned from the table.

Example:
    scan_table HelloTable
    scan_table SiteColors "default, bold"
$ run_scan_table Music
Number of items retrieved from scan: 5
******************************************************
AlbumTitle: Somewhat Famous
Artist: No One You Know
Awards: 1
SongTitle: Call Me Today
******************************************************
AlbumTitle: Somewhat Famous
Artist: No One You Know
Awards: 2
SongTitle: Howdy
******************************************************
AlbumTitle: Lanka520
Artist: Lanka
Awards: 1
SongTitle: Lanka520520
******************************************************
AlbumTitle: Songs About Life
Artist: Acme Band
Awards: 10
SongTitle: Happy Day
******************************************************
AlbumTitle: Another Album Title
Artist: Acme Band
Awards: 8
SongTitle: PartiQL Rocks
$ run_update_item
Usage:
    run_update_item <tableName> <Artist-keyValue> <SongTitle-keyValue> <attribute=value>
Where:
    tableName       - name of the table to put the item in
    Artist-keyValue - the key value to update
    SongTitle-keyValue - the key value to update
    attribute=value - attribute=updated value
Examples:
    run_update_item Music Lanka Lanka520520 AlbumTitle=Lanka520twice
$ run_update_item Music Lanka Lanka520520 AlbumTitle=Lanka520twice
Item was updated

5.1.3. S3 applications

$ ls -al aws-doc-sdk-examples/cpp/example_code/s3/*

list_buckets.cpp

$ run_list_buckets
Found 2 buckets
HelperX
utilx9

5.2. awsX9

這是本人開發的,簡單好用!

$ s3sample
 Usage: s3sample <region> <bucket> <object> <local destination path>
Example: s3sample ap-northeast-1 st-1 utilx9 demo_000.c demo_000.c_local
注意注意注意request.WithBucket(argv[2]).WithKey(argv[3]);

WithBucket: bucket name
WithKey: object name not aws_access_key_id or aws_secret_access_key
^^^^^^^ 不熟悉 AWS一開始會認為是 key or pass結果呢object 才對只能說我不懂 AWS  nameing rules

5.4. aws_dynamo - AWS DynamoDB Library for C and C++

The AWS IoT Device SDK for Embedded C (C-SDK) is a collection of C source files under the MIT open source license that can be used in embedded applications to securely connect IoT devices to AWS IoT Core. It contains MQTT client, HTTP client, JSON Parser, AWS IoT Device Shadow, AWS IoT Jobs, and AWS IoT Device Defender libraries.

The AWS IoT SDKs and the aws-iot-device-sdk-cpp are separate from this SDK. The AWS IoT Device SDK for C++ v2 is available at aws-iot-device-sdk-cpp-v2 on GitHub.

Appendix

I. Study

II. Debug

III. Glossary

IV. Tool Usage

Author

Created and designed by Lanka Hsu.

License

HelperX is available under the BSD-3-Clause license. See the LICENSE file for more info.