コンテンツにスキップ

aws-sdk-ruby

You must generate an Access Key before getting started. All examples will utilize access_key_id and access_key_secret variables which represent the Access Key ID and Secret Access Key values you generated.


多くのRubyプロジェクトでは、これらの認証情報を環境変数に保存することもあります。

次の依存関係をGemfileに追加してください:

gem "aws-sdk-s3"

次に、Rubyを使用してR2バケットを操作できます:

require "aws-sdk-s3"
@r2 = Aws::S3::Client.new(
access_key_id: "#{access_key_id}",
secret_access_key: "#{secret_access_key}",
endpoint: "https://#{cloudflare_account_id}.r2.cloudflarestorage.com",
region: "auto",
)
# アカウント上のすべてのバケットをリスト表示
puts @r2.list_buckets
#=> {
#=> :buckets => [{
#=> :name => "your-bucket",
#=> :creation_date => "…",
#=> }],
#=> :owner => {
#=> :display_name => "…",
#=> :id => "…"
#=> }
#=> }
# バケット内の最初の20項目をリスト表示
puts @r2.list_objects(bucket:"your-bucket", max_keys:20)
#=> {
#=> :is_truncated => false,
#=> :marker => nil,
#=> :next_marker => nil,
#=> :name => "your-bucket",
#=> :prefix => nil,
#=> :delimiter =>nil,
#=> :max_keys => 20,
#=> :common_prefixes => [],
#=> :encoding_type => nil
#=> :contents => [
#=> …,
#=> …,
#=> …,
#=> ]
#=> }