두 개의 리소스, 수집기 및 항목이있는 레일즈 앱을 만들고 있는데 둘 다 Paperclip을 통해 업로드 된 이미지가 필요합니다. Amazon S3는 스토리지로 사용됩니다.Rails 4 + Paperclip + Amazon S3 : 이미지는 원본으로 업로드되지만 스타일은 없음 (엄지 및 중간 크기)
수집기에 대한 아바타 이미지를 설정하고 완벽하게 작동합니다. 즉, s3 setup/config 부분이 잘 작동해야합니다. 그러나 동일한 코드를 사용하여 항목의 이미지를 설정하면 원본 이미지 만 s3에 업로드됩니다. 내 s3 콘솔에 파일을 확인하기 위해 갔다. 썸네일과 중간 폴더가 만들어졌지만 둘 다 비어있다.
참고 : 새 코드는 업데이트 II 섹션을 참조하십시오. 그래도 같은 문제가 있습니다. 여기
내 항목 모델에서 내 코드입니다 :
여기class Item < ActiveRecord::Base
belongs_to :collector
has_attached_file :item_img,
:styles => { :thumb => '320x320>', :medium => '640x640>'},
:storage => :s3,
:url => ':s3_domain_url',
:path => '/:class/:attachment/:id_partition/:style/:filename',
:bucket => "my-project-name",
:s3_credentials => S3_CREDENTIALS
has_attached_file :item_img
# Validate the attached image is image/jpg, image/png, etc
validates_attachment_content_type :item_img, :content_type => /\Aimage\/.*\Z/
validates_attachment_file_name :item_img, :matches => [/png\Z/, /jpe?g\Z/]
validates_with AttachmentSizeValidator, :attributes => :item_img, :less_than => 1.megabytes
end
(완벽하게 잘 작동) 내 수집기 모델에서 내 코드입니다 :
class Collector < ActiveRecord::Base
has_many :items, dependent: :destroy
# This method associates the attribute ":avatar" with a file attachment
has_attached_file :avatar,
:styles => { :thumb => '50x50>', :medium => '200x200>'},
:default_url => ":style/missing.jpg",
:storage => :s3,
:url => ':s3_domain_url',
:path => '/:class/:attachment/:id_partition/:style/:filename',
:bucket => "my-project-name",
:s3_credentials => S3_CREDENTIALS
# Validate the attached image is image/jpg, image/png, etc
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
validates_attachment_file_name :avatar, :matches => [/png\Z/, /jpe?g\Z/]
validates_with AttachmentSizeValidator, :attributes => :avatar, :less_than => 1.megabytes
validates :email, email_format: { message: "This doesn't look like an email address. Please try again."}
end
어떤 잠재적으로 문제가 발생할 수는? 다른 파일에서 더 많은 코드가 필요한 경우 알려주십시오.
업데이트 I : 내가지고있어 오류가 해당 파일이 어쨌든 S3 콘솔 스타일에 따라 업로드되지 않은 점을 감안, 가지 놀라운 금지 (403) 액세스입니다.
업데이트 II :이 질문을 게시하고 클립 + S3 문제에 대한 많은 글을 읽은 후가 , 난 아직도이 어떤 해결책을 찾지 못했습니다. 그래서 s3에서 개발 및 생산을위한 새로운 버킷을 다시 만들기로 결정했습니다. 클립과 s3와 관련된 모든 코드를 검토하는 동안 코드가 상당히 변경되어 코드가 수정되었습니다.
개발를 들어,S3_CREDENTIALS
에 액세스하려면
S3_CREDENTIALS = {
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
:bucket => ENV['S3_BUCKET_NAME']
}
, 내가 저장된 모든 : 나는 config/initializers
에서 s3.rb 파일이
source 'https://rubygems.org'
ruby '2.1.2'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.6'
# Use postgresql as the database for Active Record
gem 'pg'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'jquery-ui-rails'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring', group: :development
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'
gem 'httparty'
gem 'actionview', '~> 4.1.5', require: "action_view"
gem 'activesupport', '~> 4.1.6'
gem 'paperclip'
gem 'aws-sdk'
gem 'newrelic_rpm'
gem 'validates_email_format_of'
group :development, :test do
gem 'rspec-rails', '~> 3.0.0'
gem 'pry'
gem 'pry-rails'
gem 'shotgun'
gem 'annotate'
gem 'capybara'
gem 'cucumber-rails', :require => false
gem 'launchy', '~> 2.4.2'
end
group :test do
gem 'shoulda-matchers', require: false
end
group :production do
gem 'rails_12factor'
gem 'rails_serve_static_assets'
end
: 모든
첫째, 여기 내 Gemfile입니다 내 .bash_profile의 정보는 다음과 같습니다 :
export S3_BUCKET_NAME=“my-project-name_dev”
export AWS_ACCESS_KEY_ID=“asdfasdfasdfasdf”
export AWS_SECRET_ACCESS_KEY=“ASDFASDFASDFASDFASDFASDFASDF”
프로 duction, 위의 세 가지 자격 증명을 Heroku의 "Config Variables"로 저장했습니다. config/initializers/paperclib.rb
를 들어
, 나는 다음과 같은 코드가 있습니다 모델에 관해서는
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
Paperclip::Attachment.default_options[:s3_host_name] = 's3.amazonaws.com'
을, 나는 이전 버전들에서 경로를 단축하고, 또한 개발과 생산 모두 작동하도록 :bucket
을 변경 : 그것은 이상한 문제가 아니라이 게시물에 대한 내 주요 관심사입니다 개발에 missing :bucket option
말한다 있지만
class Collector < ActiveRecord::Base
has_many :items, dependent: :destroy
has_attached_file :avatar,
:styles => { :thumb => '50x50>', :medium => '200x200>'},
:default_url => ":style/missing.jpg",
:url => ':s3_domain_url',
:path => '/:class/:style/:filename',
:storage => :s3,
:bucket => ENV['S3_BUCKET_NAME'],
:s3_credentials => S3_CREDENTIALS
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
validates_attachment_file_name :avatar, :matches => [/png\Z/, /jpe?g\Z/]
validates_with AttachmentSizeValidator, :attributes => :avatar, :less_than => 1.megabytes
validates :email, email_format: { message: "This doesn't look like an email address. Please try again."}
end
수집기 모델에 대한 위의 코드는, 생산에 완벽하게 잘 작동합니다.나는 아직도 항목과 같은 문제를 겪고 :
class Item < ActiveRecord::Base
belongs_to :collector
has_attached_file :item_img,
:styles => { :small => '320x320>', :large => '640x640>' },
:url => ':s3_domain_url',
:path => '/:class/:style/:filename',
:storage => :s3,
:bucket => ENV['S3_BUCKET_NAME'],
:s3_credentials => S3_CREDENTIALS
has_attached_file :item_img
# Validate the attached image is image/jpg, image/png, etc
validates_attachment_content_type :item_img, :content_type => /\Aimage\/.*\Z/
validates_attachment_file_name :item_img, :matches => [/png\Z/, /jpe?g\Z/]
validates_with AttachmentSizeValidator, :attributes => :item_img, :less_than => 1.megabytes
end
별난 일이 예전에 정의 된 경우에도 새로운 버킷을 생성 한 후,로 저장된 이미지는 "원래"이미지가 여전히 이전 :path
걸릴 것입니다 코드 : :path => '/:class/:attachment/:id_partition/:style/:filename'
, 어떻게 새로 정의했는지 : :path => '/:class/:style/:filename'
. 어쩌면 paperclip.rb의 기본 클립 클립 때문일 수도 있지만, 콜렉터 모델과 동일한 방식으로 기본값을 무시할 것이라고 생각했습니다. 여전히 똑같은 오래된 문제가 지속됩니다. 모든 스타일이 사라지고 폴더도 사라집니다. 원본 만 "원본"폴더에 업로드됩니다.
자세한 정보가 필요한 경우 알려주십시오.
이미지를 업로드 할 때 서버 로그를 검사하여 오류가 있는지 확인 했습니까? – qubit
그리고 이것은 생산 또는 개발에 있습니까? – qubit
@voidwalker 나는 문제라고 생각되는 이미지에 대해 403 Access Forbidden Errors를 얻었습니다. 그러나 이미지가 처음에 스타일에 따라 업로드되지 않으면 해당 링크를 방문하는 것이 금지됩니다. – practicemakesperfect