Amazon AWS SDK Manuel d'utilisateur Page 14

  • Télécharger
  • Ajouter à mon manuel
  • Imprimer
  • Page
    / 29
  • Table des matières
  • MARQUE LIVRES
  • Noté. / 5. Basé sur avis des utilisateurs
Vue de la page 13
bucket_name = 'text-content'
obj_name = 'text-object.txt'
document = s3.buckets[bucket_name].objects[obj_name]
File.open(obj_name, "w") do |f|
f.write(document.read)
end
puts "'#{obj_name}' copied from S3."
You can either type or copy this code into a file on your machine. Name it whatever you like. For the
purposes of the tutorial, we'll call it get_object.rb.
This code initializes an AWS::S3 object without specifying any credentials, which initiates a search for
credentials using the default credential provider chain. The default credential provider chain looks for
credentials specified in the environment, and failing that, will look for credentials from EC2's metadata
service.
You can use this feature to test your code locally by specifying credentials in the environment. On an
EC2 instance, your code will work with the IMDS without any modification.
To test the program locally
1. Specify your AWS credentials in the local environment variables AWS_ACCESS_KEY_ID and
AWS_SECRET_ACCESS_KEY. For more information about how to do this, see Setting up AWS
Credentials (p. 5).
2. Substitute the names of an Amazon S3 bucket and text object associated with your AWS account
for the values of bucket_name and obj_name, respectively.
For instructions about how to create an Amazon S3 bucket and upload an object, see the Amazon
Simple Storage Service Getting Started Guide.
3. Open a terminal and change to the directory where you saved your application code. For example,
if you placed it in a directory called aws_ruby_test in your home directory, you would type:
cd ~/aws_ruby_test
4. Run the program with the Ruby interpreter:
ruby get_object.rb
Run the Program on EC2 using IAM Roles
Next, we'll run the program on an EC2 instance using IAM roles.To do this, we'll take the following actions:
1. Create an IAM role using the AWS Management Console (p. 12)
2. Launch an EC2 instance with the corresponding instance profile (p. 12)
3. Transfer the source to your EC2 instance (p. 14)
4. Run the program within the EC2 instance (p. 14)
Version v1.0.0
11
AWS SDK for Ruby Developer Guide
Walkthrough: Using IAM Roles to Retrieve an Amazon
S3 Object from an EC2 Instance
Vue de la page 13
1 2 ... 9 10 11 12 13 14 15 16 17 18 19 ... 28 29

Commentaires sur ces manuels

Pas de commentaire