SongsController中的AWS :: S3 :: PermanentRedirect #index"您尝试访问的存储区必须使用指定的端点进行寻址"

时间:2014-11-24 05:07:03

标签: ruby-on-rails ruby ruby-on-rails-4 amazon-s3

我按照本网站的教程:http://code.tutsplus.com/tutorials/create-a-simple-music-streaming-app-with-ruby-on-rails--net-18437#disqus_thread创建一个简单的音乐流媒体应用程序。

但是在运行rails服务器时出现错误“未初始化的常量SongsController :: BUCKET”。谁能解释为什么我得到这个错误以及如何解决它,谢谢?我使用rails 4.0.5

我创建了一个新的rails项目,我只使用以下文件,这是我的代码:

Gemfile(我添加此行)

gem 'aws-s3', '~> 0.6.3'

application.rb(所有代码)

require File.expand_path('../boot', __FILE__)

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module AudioDemo
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de
    AWS::S3::Base.establish_connection!(
      :access_key_id     => 'my acess key',
      :secret_access_key => 'my secret key'
    )
    ENV['BUCKET']='my bucket name'
  end
end

songs_controller.rb(所有代码)

class SongsController < ApplicationController
  def index
    @songs = AWS::S3::Bucket.find(ENV['BUCKET']).objects
  end

  def upload
    begin
        AWS::S3::S3Object.store(sanitize_filename(params[:mp3file].original_filename), 
          params[:mp3file].read, ENV['BUCKET'], :access => :public_read)
        redirect_to root_path
    rescue
        render :text => "Couldn't complete the upload"
    end
  end

  def delete
    if (params[:song])
        AWS::S3::S3Object.find(params[:song], ENV['BUCKET']).delete
        redirect_to root_path
    else
        render :text => "No song was found to delete!"
    end
  end

  private

    def sanitize_filename(file_name)
        just_filename = File.basename(file_name)
        just_filename.sub(/[^\w\.\-]/,'_')
    end
end

的routes.rb

root :to => "songs#index"
  get "upload" => "songs#upload", :as => "upload"
  get "delete" => "songs#delete", :as => "delete"

index.html.rb

 <ul>
<% @songs.each do |song| %>
   <li><%= song.key %> - <%= link_to "Delete",  "songs/delete/?song=" + song.key, :confirm => 'Are you sure you want to delete ' + song.key + '?' %></li>
<% end %>
</ul>

<h2>Upload a new MP3:</h2>
<%= form_tag upload_path, :method => "post", :multipart => true do %>
    <%= file_field_tag "mp3file" %>
    <%= submit_tag "Upload" %>
<% end %>

1 个答案:

答案 0 :(得分:1)

所以,你可以这样做:

AWS::S3::Base.establish_connection!(
      :access_key_id     => 'my access key',
      :secret_access_key => 'my secret key'
)
ENV['BUCKET']='mybucket'

所以,现在你可以用控制器中的ENV [&#39; BUCKET&#39;]代替BUCKET。