我正在尝试使用aws-s3 gem访问我的Amazon S3帐户,但无论我做什么,我甚至无法打印存储桶列表。
错误:
undefined method `bytesize' for #<AWS::S3::Bucket:0x1b9e488>
来自代码:
# hello_world.rb
require 'rubygems'
require 'sinatra'
require 'aws/s3'
get '/' do
connection = AWS::S3::Base.establish_connection!(
:access_key_id => '***',
:secret_access_key => '***'
)
AWS::S3::Service.buckets
end
我对Ruby没有太多经验,我只是错过了一些明显的东西吗?
答案 0 :(得分:9)
你真的应该在将你的AWS secred_access_key粘贴到stackoverflow之前对其进行模糊处理,或者至少在有人开始使用你的存储桶之前更改它...
答案 1 :(得分:8)
问题是你正在尝试输出整个存储桶集,但是没有字符串序列化方法,这就是你得到这个错误的原因。
试试这个:
<强> app.rb 强>
require 'rubygems'
require 'sinatra'
require 'aws/s3'
include AWS::S3
def s3_connect
Base.establish_connection!(
:access_key_id => 'THISISMYACCESSKEYITMAYNOTBETHEBESTBUTITISMINE',
:secret_access_key => 'HERPADERPSECRETKEYISSECRET'
)
end
get '/' do
s3_connect
@buckets = Service.buckets
erb :index
end
get '/bucket/:key' do
s3_connect
@bucket = Bucket.find(params[:key])
erb :bucket
end
查看:index.erb
<h1>Buckets</h1>
<ul>
<% @buckets.each do |b| %>
<li><a href='/bucket/<%= b.name %>'><%= b.name %></a> (<%= b.objects.length %> objects)</li>
<% end %>
</ul>
查看:bucket.erb
<h1>Bucket: <%= @bucket.name %> Objects</h1>
<% @bucket.objects.each do |obj| %>
Object: <%= obj.key %> <%= obj.about['content-length'] %> bytes<br/>
<% end %>