LoginSignup
0
2

More than 1 year has passed since last update.

AWSのDynamoDBにCRUD処理を行う方法(基礎編)

Posted at

AWSのDynamoDBにCRUD事始

AWSのDynamoDBにCRUD処理を行うために備忘録です。
初心者の方の参考になるかと思います。
今回は、自分のPCからAWSのDynamoDBに対してCRUD処理を行います。

AWS側の処理

CURD処理を行うためにIAMで処理可能な権限を持つユーザーを作成します。

IAMでやること

① IAMに行きユーザーを作成する

② 「アクセスキー・プログラムによるアクセス」をチェックし次へ
Image from Gyazo

③ 作成したユーザーのDynamoDBにFull Accessできるポリシーを割り当てる
Image from Gyazo

④ タグの設定は現状行わない
Image from Gyazo

⑤ ユーザーを作成
Image from Gyazo

⑥ アクセスキーIDとシークレットアクセスキーを忘れずにメモする
Image from Gyazo

DynamoDBで行うこと

① テーブルを作成
Image from Gyazo

② パーテーションキーとソートキーを作成。今回はid(文字列)とpass(数値)に指定
Image from Gyazo

③ 作成したテーブルがアクティブになることを確認
Image from Gyazo

MyPC 側の処理

自分のPCからCRUD処理を行うために、まずは、AWS-sdkをインストールします。

npm i aws-sdk 

その上で次の処理をおこなっていきます。

Create

putを使用して、データをDynamoDBに送リます。

create.js
const AWS = require ("aws-sdk");
const client = new AWS.DynamoDB.DocumentClient({
    region : "自分のリージョンを設定",
    credentials : {
        secretAccessKey : "自分のシークレットキー",
        accessKeyId : "自分のアクセスキー"
    }
});


const data1 = {
    id : "test",
    pass : 1 ,
    url : "https://hogehoge"
}

 async function create(data){
    const result = await client.put({
        TableName : "CRUDtest", 
        Item : data
    }).promise();

    console.log(result);
}


Promise.resolve()
  .then(() => {
    return create(data1);
  })
  .then(() => {
    console.log("complete");
  })
  .catch((e) => {
    console.log("Error発生", e);
  });

コンソール上で、実行。

node create.js

するとDynamoDB上にデータが入る
Image from Gyazo

Read

getで欲しい情報のidとpassを指定して、情報をDynamoDBから取得します。

read.js
const AWS = require ("aws-sdk");
const client = new AWS.DynamoDB.DocumentClient({
    region : "自分のリージョンを設定",
    credentials : {
        secretAccessKey : "自分のシークレットキー",
        accessKeyId : "自分のアクセスキー"
    }
});

 async function read(){
    const result = await client.get({
        TableName : "CRUDtest", 
        Key : {
            id : "test",
            pass : 1
    }}).promise();

    console.log(result.Item.url);
}


Promise.resolve()
  .then(() => {
    return read();
  })
  .then(() => {
    console.log("complete");
  })
  .catch((e) => {
    console.log("Error発生", e);
  });

コンソール上で、実行。

node read.js

結果が取得できます。
Image from Gyazo

Update

今回はsetを使用してデータを更新します。

const AWS = require ("aws-sdk");
const client = new AWS.DynamoDB.DocumentClient({
    region : "自分のリージョンを設定",
    credentials : {
        secretAccessKey : "自分のシークレットキー",
        accessKeyId : "自分のアクセスキー"
    }
});

 async function update(){
    const result = await client.update({
        TableName : "CRUDtest", 
        Key : {
            id : "test",
            pass : 1
        },
        UpdateExpression: 'set #a = :x',
        ExpressionAttributeNames: {'#a' : 'detail'},
        ExpressionAttributeValues: {
        ':x' : "test"
        }
    }).promise();

    console.log(result);
}


Promise.resolve()
  .then(() => {
    return update();
  })
  .then(() => {
    console.log("complete");
  })
  .catch((e) => {
    console.log("Error発生", e);
  });

コンソール上で、実行。

node update.js

結果が取得できます。
今回は、detailという項目が追加され、そこにtestという文字データが追加されました。
Image from Gyazo

delete

const AWS = require ("aws-sdk");
const client = new AWS.DynamoDB.DocumentClient({
    region : "自分のリージョンを設定",
    credentials : {
        secretAccessKey : "自分のシークレットキー",
        accessKeyId : "自分のアクセスキー"
    }
});

 async function del(){
    const result = await client.delete({
        TableName : "CRUDtest", 
        Key : {
            id : "test",
            pass : 1
        }
    }).promise();

    console.log(result);
}


Promise.resolve()
  .then(() => {
    return del();
  })
  .then(() => {
    console.log("complete");
  })
  .catch((e) => {
    console.log("Error発生", e);
  });

コンソール上で、実行。

node delete.js

指定したデータが消去されます。
Image from Gyazo

0
2
1

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
0
2