AWS SDK for Ruby의 입문용 자습서 - AWS 루비용 SDK

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

AWS SDK for Ruby의 입문용 자습서

AWS SDK for Ruby를 사용하여 Amazon S3에 인사하세요. 다음 예제에서는 Amazon S3 버킷의 목록을 표시합니다.

코드 쓰기

다음 코드를 복사하여 새로운 소스 파일에 붙여 넣습니다. 파일 이름을 hello-s3.rb로 지정합니다.

require "aws-sdk-s3" # Wraps Amazon S3 resource actions. class BucketListWrapper attr_reader :s3_resource # @param s3_resource [Aws::S3::Resource] An Amazon S3 resource. def initialize(s3_resource) @s3_resource = s3_resource end # Lists buckets for the current account. # # @param count [Integer] The maximum number of buckets to list. def list_buckets(count) puts "Found these buckets:" @s3_resource.buckets.each do |bucket| puts "\t#{bucket.name}" count -= 1 break if count.zero? end true rescue Aws::Errors::ServiceError => e puts "Couldn't list buckets. Here's why: #{e.message}" false end end # Example usage: def run_demo wrapper = BucketListWrapper.new(Aws::S3::Resource.new) wrapper.list_buckets(25) end run_demo if $PROGRAM_NAME == __FILE__

AWS SDK for Ruby는 모듈식으로 설계되었으며 AWS 서비스와 구분됩니다. gem이 설치된 후 Ruby 소스 파일 상단에 있는 require 명령문을 통해 Amazon S3 서비스용 AWS SDK 클래스 및 메서드를 가져옵니다. 사용 가능한 AWS 서비스 gem의 전체 목록은 AWS SDK for Ruby README 파일의 Supported Services 표를 참조하세요.

require 'aws-sdk-s3'

프로그램 실행

명령 프롬프트를 열어 Ruby 프로그램을 실행합니다. Ruby 프로그램을 실행하는 일반적인 명령 구문은 다음과 같습니다.

ruby [source filename] [arguments...]

이 샘플 코드는 인수를 사용하지 않습니다. 이 코드를 실행하려면 명령 프롬프트에 다음을 입력합니다.

$ ruby hello-s3.rb

Windows 사용자를 위한 참고 사항

Windows에서 SSL 인증서를 사용하고 Ruby 코드를 실행하면 다음과 비슷한 오류가 표시될 수 있습니다.

C:\Ruby>ruby buckets.rb C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:921:in `connect': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (Seahorse::Client::NetworkingError) from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:921:in `block in connect' from C:/Ruby200-x64/lib/ruby/2.0.0/timeout.rb:66:in `timeout' from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:921:in `connect' from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:862:in `do_start' from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:857:in `start' ...

이 문제를 해결하려면 Ruby 소스 파일의 최초 AWS 호출 전 위치에 다음 줄을 추가하세요.

Aws.use_bundled_cert!

Ruby 프로그램에 aws-sdk-s3 gem만 사용하는 경우, 번들 인증서를 사용하려면 aws-sdk-core gem도 추가해야 합니다.

다음 단계

다른 많은 Amazon S3 작업을 테스트하려면 의 AWS코드 예제 리포지토리를 확인하십시오 GitHub.