Last active
March 29, 2025 22:33
-
-
Save jacoyutorius/679c91a6aa0f9647dfcb5f0f96b176a4 to your computer and use it in GitHub Desktop.
Amazon Cognitoのidentity poolから一時発行されたゲストユーザーの認証情報を取得するスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# 必要な gem 名 | |
gem_name = 'aws-sdk-cognitoidentity' | |
# gem が未インストールならインストール | |
begin | |
gem gem_name | |
rescue LoadError | |
puts "🔧 '#{gem_name}' が見つかりません。インストールします..." | |
system("gem install #{gem_name}") or abort("❌ gem のインストールに失敗しました") | |
Gem.clear_paths | |
end | |
# gem を読み込む | |
require gem_name | |
# ↓ AWS 認証処理(ここから下は自由にカスタマイズ可能) | |
identity_pool_id = '' | |
region = 'ap-northeast-1' | |
client = Aws::CognitoIdentity::Client.new(region: region) | |
identity_id = client.get_id(identity_pool_id: identity_pool_id).identity_id | |
credentials = client.get_credentials_for_identity(identity_id: identity_id).credentials | |
puts "✅ AWS 一時認証情報を取得しました:" | |
puts "- Access Key ID: #{credentials.access_key_id}" | |
puts "- Secret Access Key: #{credentials.secret_key}" | |
puts "- Session Token: #{credentials.session_token[0..10]}...(省略)" | |
puts "- 有効期限: #{credentials.expiration}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment