Created
January 28, 2015 18:40
-
-
Save ashayh/0b550bee6fce417d852d to your computer and use it in GitHub Desktop.
access S3 using ruby fog with IAM roles
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
| require 'rubygems' | |
| require 'fog' | |
| # assuming your IAM role is named 'my-role' and has S3 access: | |
| uri = URI('http://169.254.169.254/latest/meta-data/iam/security-credentials/my-role') | |
| res = Net::HTTP.get_response(uri) | |
| aws_creds = JSON.parse(res.body) | |
| # use the credentials manually: | |
| con = Fog::Storage.new({ | |
| :provider => 'AWS', | |
| :aws_access_key_id => aws_creds['AccessKeyId'], | |
| :aws_secret_access_key => aws_creds['SecretAccessKey'], | |
| :aws_session_token => aws_creds['Token'] | |
| }) | |
| # or simply use the IAM Role name: | |
| con = Fog::Storage.new({ | |
| :provider => 'AWS', | |
| :use_iam_profile => 'my-role' | |
| }) | |
| directory = con.directories.get('my-s3-dir') | |
| file = directory.files.get('my-s3-sub-dir/my-file.txt') | |
| # load S3 file in memory: | |
| file_contents = file.body |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment