本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
的 Hello World 教程AmazonSDK for Ruby
本教程向您演示如何使用AmazonSDK for Ruby 的开发工具包来创建执行一些常见 Amazon S3 操作的命令行程序。
使用AmazonSDK for Ruby 的程序开发工具包
添加require
将语句添加到您的 Ruby 源文件的顶部,以便您可以使用提供的类和方法。AmazonSDK for Ruby。
require 'aws-sdk'
创建 Amazon S3 资源
在适当的区域中创建 Aws::S3::Resource 对象。以下示例在中创建 Amazon S3 资源对象:us-west-2
区域。请注意,区域不重要,因为 Amazon S3 资源不特定于区域。
s3 = Aws::S3::Resource.new(region: 'us-west-2')
创建存储桶
要在 Amazon S3 上存储任何内容,您需要一个存储桶来放入它们。
创建 Aws::S3::Bucket 对象。以下示例创建名为 my-bucket
的存储桶 my_bucket
。
my_bucket = s3.bucket('my-bucket') my_bucket.create
向存储桶中添加文件
使用 #upload_file
方法来向存储桶中添加文件。以下示例将名为 my_file
的文件添加到名为 my-bucket
的存储桶中。
name = File.basename 'my_file' obj = s3.bucket('my-bucket').object(name) obj.upload_file('my_file')
列出存储桶的内容
要列出存储桶的内容,请使用Aws::S3::Bucket:Objects
方法。以下示例列出了存储桶 my-bucket
的最多 50 个存储桶项目。
my_bucket.objects.limit(50).each do |obj| puts " #{obj.key} => #{obj.etag}" end
完整程序
以下是整个 hello-s3.rb
程序。
require 'aws-sdk' NO_SUCH_BUCKET = "The bucket '%s' does not exist!" USAGE = <<DOC Usage: hello-s3 bucket_name [operation] [file_name] Where: bucket_name (required) is the name of the bucket operation is the operation to perform on the bucket: create - creates a new bucket upload - uploads a file to the bucket list - (default) lists up to 50 bucket items file_name is the name of the file to upload, required when operation is 'upload' DOC # Set the name of the bucket on which the operations are performed # This argument is required bucket_name = nil if ARGV.length > 0 bucket_name = ARGV[0] else puts USAGE exit 1 end # The operation to perform on the bucket operation = 'list' # default operation = ARGV[1] if (ARGV.length > 1) # The file name to use with 'upload' file = nil file = ARGV[2] if (ARGV.length > 2) # Get an Amazon S3 resource s3 = Aws::S3::Resource.new(region: 'us-west-2') # Get the bucket by name bucket = s3.bucket(bucket_name) case operation when 'create' # Create a bucket if it doesn't already exist if bucket.exists? puts "The bucket '%s' already exists!" % bucket_name else bucket.create puts "Created new S3 bucket: %s" % bucket_name end when 'upload' if file == nil puts "You must enter the name of the file to upload to S3!" exit end if bucket.exists? name = File.basename file # Check if file is already in the bucket if bucket.object(name).exists? puts "#{name} already exists in the bucket" else obj = s3.bucket(bucket_name).object(name) obj.upload_file(file) puts "Uploaded '%s' to S3!" % name end else NO_SUCH_BUCKET % bucket_name end when 'list' if bucket.exists? # Enumerate the bucket contents and object etags puts "Contents of '%s':" % bucket_name puts ' Name => GUID' bucket.objects.limit(50).each do |obj| puts " #{obj.key} => #{obj.etag}" end else NO_SUCH_BUCKET % bucket_name end else puts "Unknown operation: '%s'!" % operation puts USAGE end
运行程序
要列出存储桶的内容,请使用以下任一命令,其中 bucket-name
是要列出的存储桶的名称。您不必包括 list
,因为它是默认操作。
ruby hello-s3.rb bucket-name list ruby hello-s3.rb bucket-name
要创建存储桶,请使用以下命令,其中 bucket-name
是要创建的存储桶的名称。
ruby hello-s3.rb bucket-name create
如果 Amazon S3 已经有名为的存储桶bucket-name
,服务会发出错误消息,并且不会创建另一个副本。
在创建存储桶后,您可以向该存储桶中上传对象。以下命令将 your_file.txt
添加到存储桶中。
ruby hello-s3.rb bucket-name upload your_file.txt
后续步骤
现在你已经完成了你的第一个Amazon适用 SDK for Ruby 的开发工具包,这里是扩展您刚才编写的代码的一些建议:
-
使用
buckets
收藏来自Aws::S3::Resource
类来获取存储桶的列表。 -
使用
#get
方法来自Bucket
类来从存储桶下载对象。 -
使用向存储桶中添加文件中的代码来确认项目存在于存储桶中,然后更新该存储桶项目。