16
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Salesforce の指定ログイン情報 (Named Credential) を利用した AWS へのリクエスト

Last updated at Posted at 2019-05-16

Summer '19 リリース(API バージョン 46.0)で、指定ログイン情報(Named Credential, 外部システムへの認証情報を保存する設定のこと)がAWS の署名バージョン 4 のリクエストをサポートするようになりました。指定ログイン情報を利用するとコールアウトの際に煩雑な署名計算を行う必要がなくなります。ここでは、S3 へのリクエストを例に、実際の設定方法を紹介します。

指定ログイン情報は、Salesforce の設定で [セキュリティ] - [指定ログイン情報] - [新規指定ログイン情報] から設定できます。

設定例

  • 表示ラベルと名前には任意の名称(名前はプログラム内で使用します)を設定
  • URL にはhttps://BUCKETNAME.s3.amazonaws.com を設定
  • ID 種別は指定ユーザ を選択
  • 認証プロトコルはAWS 署名バージョン4 を選択
  • AWS アクセスキーとAWSシークレットアクセスキーに、適切なIAMユーザのアクセスキーとシークレットキーを設定
  • AWS リージョンには該当するリージョンを設定 (ap-northeast-1 等)
  • AWS サービス にはs3 を設定
  • 認証ヘッダーを生成のチェックをON

aws_s3_namedcred.png

サンプルコード

HttpRequest req = new HttpRequest();
req.setEndpoint('callout:AWS_S3/hogedir/hogefile.txt'); //callout:の後ろは指定ログイン情報の名前とファイルパス
req.setMethod('GET');
Http http = new Http();
HttpResponse res = http.send(req);
System.debug(res); //System.HttpResponse[Status=OK, StatusCode=200]

おわりに

現状、全てのサービス・リクエストに対して利用できる訳ではなさそうです(例えばヘッダに x-amz-target が必要な場合は直接計算が必要だと思います)が、もし利用できる場合はシンプルにリクエストが実装できることが分かります。ぜひ試してみてください。

16
6
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
16
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?