0

所以,我有一个允许用户上传、评论和投票歌曲的应用程序。这些歌曲目前正在 heroku 上托管,但 Heroku 不推荐这样做。因此,我想在 Amazon S3 上托管作为一个强大的替代方案。

但是,我很难使用 Paperclip gem 将文件上传到 S3。到目前为止,我已经注册了我的 AWS 账户并设置了 access_key、secret_key 和 S3 BUCKET。

这就是我到目前为止所拥有的:(而且它不起作用)。

# config/initializers/paperclip.rb 
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'

歌曲.rb

class Song < ActiveRecord::Base

  acts_as_voteable

  belongs_to :user
  has_many :comments, :dependent => :destroy
  has_many :genre_songs
  has_many :genres, through: :genre_songs

has_attached_file :track,
                  :url  => "/assets/songs/:id/:style/:basename.:extension",
                  :path => ":rails_root/public/assets/songs/:id/:style/:basename.:extension"

  validates_attachment :track, :presence => true

  validates_presence_of :url

  validates :title, length: { minimum: 10 }
  validates :url, length: { maximum: 300 }

  def self.tagged_with(name)
    Genre.find_by_name!(name).songs
  end

  def tag_list
    genres.map(&:name).join(", ")
  end

  def tag_list=(names)
    self.genres = names.split(",").map do |n|
      Genre.where(name: n.strip).first_or_create!
    end
  end
end

生产.rb

# config/environments/production.rb
config.paperclip_defaults = {
  :storage => :s3,
  :s3_credentials => {
    :bucket => ENV['AWS_BUCKET'],
    :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
    :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
  }
}

Heroku 命令:

$ heroku config:set AWS_BUCKET=your_bucket_name
$ heroku config:set AWS_ACCESS_KEY_ID=your_access_key_id
$ heroku config:set AWS_SECRET_ACCESS_KEY=your_secret_access_key

显示.html.erb

<p>
<%= audio_tag (@song.track.url), controls: "controls", alt: "Please use chrome, ie, or safari", preload: :auto %>   
</p>
4

1 回答 1

0

我有相同的设置(用于图片,但没关系)。对我来说一切都很好,除了我没有像你那样在我的模型中提供:url和参数。:path

于 2013-08-01T18:34:33.563 回答