简体   繁体   中英

Paperclip with S3?

So, I have an app that allows users to upload, comment, and vote on songs. The songs are being hosted on heroku at the moment but that isn't recommended by Heroku. So, I'd like to host on Amazon S3 as an robust alternative.

However, I'm having difficulty with getting the files to upload to S3 using the Paperclip gem. So far I've registered my AWS account and setup the access_key, secret_key, and the S3 BUCKET.

This is what I have so far: (and it's not working).

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

song.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

production.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 commands:

$ 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

show.html.erb

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

I have the same setup (for pictures, but nevermind that). Everything looks fine to me, except that I do not provide the :url and :path parameters in my model like you do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM