0
0

More than 3 years have passed since last update.

ローカルPCのNode.jsでaws-sdkの認証を通す

Last updated at Posted at 2019-12-13

はじめに

ローカルPCでAWSの色々なサービスを操作するツールを作りたいときに、
認証が通せず苦労しましたが、この方法で通せるようです。

ファイルなんか作らずにメモリ内に閉じたかったのですが、無理そうでした。

認証を通すコード

dynamo-utility.ts
import AWS = require('aws-sdk');
import Fs = require('fs');

export class DynamoUtility {
  dynamoDb: AWS.DynamoDB;
  documentClient: AWS.DynamoDB.DocumentClient;

  constructor(credential: any) {
    const tmpFile = './credential.json';
    // 認証ファイルを生成
    Fs.writeFileSync(tmpFile, JSON.stringify(credential));
    // 生成したファイルを読み込ませる
    AWS.config.loadFromPath(tmpFile);
    // ファイル削除
    Fs.unlinkSync(tmpFile);
    // クライアント生成
    this.documentClient = new AWS.DynamoDB.DocumentClient();
    this.dynamoDb = new AWS.DynamoDB();
  }
}
environment.ts
// ...
    credential: {
      accessKeyId: 'xxxxxxxxxxxxxxxxxxxx',
      secretAccessKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
      region: 'ap-northeast-1'
    },
// ...

さいごに

役に立ちましたら、記事にいいねをお願いします。

0
0
0

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
0