2009-08-20 75 views
2

我试图使用aws-s3 gem访问我的Amazon S3帐户,但无论我做什么,我都无法打印存储桶列表。在Sinatra中使用AWS :: 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 

我不是太有经验丰富的红宝石,我只是缺少明显的东西?

回答

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 %> 
+0

谢谢,Ruby是令人沮丧的。 – arbales 2009-08-20 20:06:12

9

你真的应该在计算器粘贴之前,混淆你的AWS secred_access_key,或至少现在改变有人开始用你的水桶打之前...

相关问题