|
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 |
33 | 3 |
|
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. |
36 | 5 |
|
37 | | -# Usage: ruby auth_request_test.rb OPERATION BUCKET |
38 | | -# Currently only the list operation is supported |
| 6 | +# Prerequisites: |
| 7 | +# - An Amazon S3 bucket. |
39 | 8 |
|
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' |
43 | 11 |
|
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' |
48 | 18 | end |
49 | 19 |
|
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 |
57 | 28 |
|
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 | + ) |
63 | 43 |
|
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 |
66 | 48 | end |
67 | 49 | else |
68 | | - puts "The bucket '%s' does not exist!" % bucket_name |
| 50 | + puts 'No objects found in this bucket.' |
69 | 51 | end |
70 | 52 |
|
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}" |
73 | 56 | end |
74 | 57 | # 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 |
0 commit comments