Skip to content

Commit 93149dc

Browse files
authored
Merge pull request #1342 from awsdocs/pccornel-ruby-s3
Added new Amazon S3 code examples and units test for Ruby to upload an item to a folder and to upload multiple items. Also fixed several minor reported RuboCop issues
2 parents d612baa + 1adaa6c commit 93149dc

11 files changed

+488
-112
lines changed

ruby/example_code/s3/auth_federation_token_request_test.rb

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
# SPDX - License - Identifier: Apache - 2.0
33

44
# This code example allows a federated user with a limited set of
5-
# permissions to list the objects in the specified Amazon S3 bucket.
5+
# permissions to list the objects in an Amazon S3 bucket.
6+
7+
# Prerequisites:
8+
# - An existing Amazon S3 bucket.
69

710
# snippet-start:[s3.ruby.auth_federation_token_request_test.rb]
811
require 'aws-sdk-s3'
912
require 'aws-sdk-iam'
1013
require 'json'
1114

12-
# Checks to see whether the specified user exists in IAM; otherwise,
15+
# Checks to see whether a user exists in IAM; otherwise,
1316
# creates the user.
1417
#
1518
# @param iam [Aws::IAM::Client] An initialized IAM client.
@@ -18,9 +21,7 @@
1821
# @example
1922
# iam = Aws::IAM::Client.new(region: 'us-east-1')
2023
# user = get_user(iam, 'my-user')
21-
# unless user.user_name
22-
# exit 1
23-
# end
24+
# exit 1 unless user.user_name
2425
# puts "User's name: #{user.user_name}"
2526
def get_user(iam, user_name)
2627
puts "Checking for a user with the name '#{user_name}'..."
@@ -38,7 +39,7 @@ def get_user(iam, user_name)
3839
puts "Error while accessing or creating the user named '#{user_name}': #{e.message}"
3940
end
4041

41-
# Gets temporary AWS credentials for the specified IAM user and permissions.
42+
# Gets temporary AWS credentials for an IAM user with the specified permissions.
4243
#
4344
# @param sts [Aws::STS::Client] An initialized AWS STS client.
4445
# @param duration_seconds [Integer] The number of seconds for valid credentials.
@@ -58,9 +59,7 @@ def get_user(iam, user_name)
5859
# ]
5960
# }
6061
# )
61-
# unless credentials.access_key_id
62-
# exit 1
63-
# end
62+
# exit 1 unless credentials.access_key_id
6463
# puts "Access key ID: #{credentials.access_key_id}"
6564
def get_temporary_credentials(sts, duration_seconds, user_name, policy)
6665
response = sts.get_federation_token(
@@ -73,19 +72,17 @@ def get_temporary_credentials(sts, duration_seconds, user_name, policy)
7372
puts "Error while getting federation token: #{e.message}"
7473
end
7574

76-
# Lists the keys and ETags for the objects in the specified Amazon S3 bucket.
75+
# Lists the keys and ETags for the objects in an Amazon S3 bucket.
7776
#
78-
# @param s3 [Aws::S3::Client] An initialized Amazon S3 client.
77+
# @param s3_client [Aws::S3::Client] An initialized Amazon S3 client.
7978
# @param bucket_name [String] The bucket's name.
8079
# @return [Boolean] true if the objects were listed; otherwise, false.
8180
# @example
82-
# s3 = Aws::S3::Client.new(region: 'us-east-1')
83-
# unless can_list_objects_in_bucket?(s3, 'my-bucket')
84-
# exit 1
85-
# end
86-
def can_list_objects_in_bucket?(s3, bucket_name)
81+
# s3_client = Aws::S3::Client.new(region: 'us-east-1')
82+
# exit 1 unless list_objects_in_bucket?(s3_client, 'my-bucket')
83+
def list_objects_in_bucket?(s3_client, bucket_name)
8784
puts "Accessing the contents of the bucket named '#{bucket_name}'..."
88-
response = s3.list_objects_v2(
85+
response = s3_client.list_objects_v2(
8986
bucket: bucket_name,
9087
max_keys: 50
9188
)
@@ -102,7 +99,6 @@ def can_list_objects_in_bucket?(s3, bucket_name)
10299
return true
103100
rescue StandardError => e
104101
puts "Error while accessing the bucket named '#{bucket_name}': #{e.message}"
105-
return false
106102
end
107103
# snippet-end:[s3.ruby.auth_federation_token_request_test.rb]
108104

@@ -115,9 +111,7 @@ def can_list_objects_in_bucket?(s3, bucket_name)
115111
iam = Aws::IAM::Client.new(region: region)
116112
user = get_user(iam, user_name)
117113
118-
unless user.user_name
119-
exit 1
120-
end
114+
exit 1 unless user.user_name
121115
122116
puts "User's name: #{user.user_name}"
123117
sts = Aws::STS::Client.new(region: region)
@@ -133,14 +127,10 @@ def can_list_objects_in_bucket?(s3, bucket_name)
133127
}
134128
)
135129
136-
unless credentials.access_key_id
137-
exit 1
138-
end
130+
exit 1 unless credentials.access_key_id
139131
140132
puts "Access key ID: #{credentials.access_key_id}"
141-
s3 = Aws::S3::Client.new(region: region, credentials: credentials)
133+
s3_client = Aws::S3::Client.new(region: region, credentials: credentials)
142134
143-
unless can_list_objects_in_bucket?(s3, bucket_name)
144-
exit 1
145-
end
135+
exit 1 unless list_objects_in_bucket?(s3_client, bucket_name)
146136
=end
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX - License - Identifier: Apache - 2.0
33

4+
# Prerequisites:
5+
# - An Amazon S3 bucket.
6+
47
# snippet-start:[s3.ruby.auth_request_object_keys.rb]
58
require 'aws-sdk-s3'
69

7-
# Prints the list of objects in the specified Amazon S3 bucket.
10+
# Prints the list of objects in an Amazon S3 bucket.
811
#
9-
# @param s3 [Aws::S3::Client] An initialized Amazon S3 client.
12+
# @param s3_client [Aws::S3::Client] An initialized Amazon S3 client.
1013
# @param bucket_name [String] The bucket's name.
1114
# @return [Boolean] true if all operations succeed; otherwise, false.
1215
# @example
13-
# s3 = Aws::S3::Client.new(region: 'us-east-1')
14-
# unless can_list_bucket_objects?(s3, 'my-bucket')
15-
# exit 1
16-
# end
17-
def can_list_bucket_objects?(s3, bucket_name)
16+
# s3_client = Aws::S3::Client.new(region: 'us-east-1')
17+
# exit 1 unless list_bucket_objects?(s3_client, 'my-bucket')
18+
def list_bucket_objects?(s3_client, bucket_name)
1819
puts "Accessing the bucket named '#{bucket_name}'..."
19-
objects = s3.list_objects_v2(
20+
objects = s3_client.list_objects_v2(
2021
bucket: bucket_name,
2122
max_keys: 50
2223
)
@@ -37,13 +38,11 @@ def can_list_bucket_objects?(s3, bucket_name)
3738
end
3839
# snippet-end:[s3.ruby.auth_request_object_keys.rb]
3940

40-
# Full example:
41+
# Full example call:
4142
=begin
4243
region = 'us-east-1'
4344
bucket_name = 'my-bucket'
44-
s3 = Aws::S3::Client.new(region: region)
45+
s3_client = Aws::S3::Client.new(region: region)
4546
46-
unless can_list_bucket_objects?(s3, bucket_name)
47-
exit 1
48-
end
47+
exit 1 unless list_bucket_objects?(s3_client, bucket_name)
4948
=end
Lines changed: 50 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,64 @@
1-
#**
2-
#* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
#*
4-
#* This file is licensed under the Apache License, Version 2.0 (the "License").
5-
#* You may not use this file except in compliance with the License. A copy of
6-
#* the License is located at
7-
#*
8-
#* http://aws.amazon.com/apache2.0/
9-
#*
10-
#* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11-
#* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12-
#* specific language governing permissions and limitations under the License.
13-
#**
14-
# snippet-sourcedescription:[auth_request_test.rb uses the credentials in a shared AWS credentials file on a local computer to authenticate a request to get all of the object key names in a specific bucket.]
15-
# snippet-service:[s3]
16-
# snippet-keyword:[Ruby]
17-
# snippet-sourcesyntax:[ruby]
18-
# snippet-keyword:[Amazon S3]
19-
# snippet-keyword:[Code Sample]
20-
# snippet-keyword:[GET Bucket]
21-
# snippet-sourcetype:[full-example]
22-
# snippet-sourcedate:[2019-02-11]
23-
# snippet-sourceauthor:[AWS]
24-
# snippet-start:[s3.ruby.auth_request_test.rb]
25-
# This snippet example does the following:
26-
# Creates an instance of the Aws::S3::Resource class.
27-
# Makes a request to Amazon S3 by enumerating objects in a bucket using the bucket method of Aws::S3::Resource.
28-
# The client generates the necessary signature value from the credentials in the AWS credentials file on your computer,
29-
# and includes it in the request it sends to Amazon S3.
30-
# Prints the array of object key names to the terminal.
31-
# The credentials that are used for this example come from a local AWS credentials file on the computer that is running this application.
32-
# The credentials are for an IAM user who can list objects in the bucket that the user specifies when they run the application.
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX - License - Identifier: Apache - 2.0
333

34-
# Use the Amazon S3 modularized gem for version 3 of the AWS Ruby SDK.
35-
require 'aws-sdk-s3'
4+
# Prints a list of objects in an Amazon S3 bucket.
365

37-
# Usage: ruby auth_request_test.rb OPERATION BUCKET
38-
# Currently only the list operation is supported
6+
# Prerequisites:
7+
# - An Amazon S3 bucket.
398

40-
# The operation to perform on the bucket.
41-
operation = 'list' # default
42-
operation = ARGV[0] if (ARGV.length > 0)
9+
# snippet-start:[s3.ruby.auth_request_test.rb]
10+
require 'aws-sdk-s3'
4311

44-
if ARGV.length > 1
45-
bucket_name = ARGV[1]
46-
else
47-
exit 1
12+
# Prints command line usage information.
13+
def print_usage()
14+
puts 'Incorrect command line arguments provided or -h or --help specified.'
15+
puts 'Usage: auth_request_test.rb <bucket>'
16+
puts ' <bucket> The name of the bucket containing the objects to list.'
17+
puts 'Example: auth_request_test.rb my-bucket'
4818
end
4919

50-
# Get an Amazon S3 resource.
51-
s3 = Aws::S3::Resource.new(region: 'us-west-2')
52-
53-
# Get the bucket by name.
54-
bucket = s3.bucket(bucket_name)
55-
56-
case operation
20+
# Checks whether the correct command line arguments were provided.
21+
# On failure, prints command line usage information and then exits.
22+
def check_inputs()
23+
if ARGV.length != 1 || ARGV[0] == '-h' || ARGV[0] == '--help'
24+
print_usage
25+
exit 1
26+
end
27+
end
5728

58-
when 'list'
59-
if bucket.exists?
60-
# Enumerate the bucket contents and object etags.
61-
puts "Contents of '%s':" % bucket_name
62-
puts ' Name => GUID'
29+
# Prints the list of objects in the specified Amazon S3 bucket.
30+
#
31+
# @param s3_client [Aws::S3::Client] An initialized Amazon S3 client.
32+
# @param bucket_name [String] The bucket's name.
33+
# @return [Boolean] true if all operations succeed; otherwise, false.
34+
# @example
35+
# s3_client = Aws::S3::Client.new(region: 'us-east-1')
36+
# exit 1 unless can_list_bucket_objects?(s3_client, 'my-bucket')
37+
def list_bucket_objects?(s3_client, bucket_name)
38+
puts "Accessing the bucket named '#{bucket_name}'..."
39+
objects = s3_client.list_objects_v2(
40+
bucket: bucket_name,
41+
max_keys: 50
42+
)
6343

64-
bucket.objects.limit(50).each do |obj|
65-
puts " #{obj.key} => #{obj.etag}"
44+
if objects.count.positive?
45+
puts 'The object keys in this bucket are (first 50 objects):'
46+
objects.contents.each do |object|
47+
puts object.key
6648
end
6749
else
68-
puts "The bucket '%s' does not exist!" % bucket_name
50+
puts 'No objects found in this bucket.'
6951
end
7052

71-
else
72-
puts "Unknown operation: '%s'! Only list is supported." % operation
53+
return true
54+
rescue StandardError => e
55+
puts "Error while accessing the bucket named '#{bucket_name}': #{e.message}"
7356
end
7457
# snippet-end:[s3.ruby.auth_request_test.rb]
58+
59+
# Full example call:
60+
=begin
61+
check_inputs
62+
s3_client = Aws::S3::Client.new(region: 'us-east-1')
63+
exit 1 unless list_bucket_objects?(s3_client, ARGV[0])
64+
=end

ruby/example_code/s3/metadata.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,22 @@ files:
1515
- path: tests/test_auth_request_object_keys.rb
1616
services:
1717
- s3
18+
- path: auth_request_test.rb
19+
services:
20+
- s3
21+
- path: tests/test_auth_request_test.rb
22+
services:
23+
- s3
24+
- path: s3-ruby-example-upload-item-to-folder.rb
25+
services:
26+
- s3
27+
- path: tests/test_s3-ruby-example-upload-item-to-folder.rb
28+
services:
29+
- s3
30+
- path: s3-ruby-examples-upload-multiple-items.rb
31+
services:
32+
- s3
33+
- path: tests/test_s3-ruby-examples-upload-multiple-items.rb
34+
services:
35+
- s3
1836
...

0 commit comments

Comments
 (0)