samedi 25 mai 2019

Why does my paperclip configuration not work for?

I have a Photo model, I am running paperclip (>= 3.4.0), ruby 2.3.7, Rails 3.2.

In my photo.rb, I have the following:

class Photo < ActiveRecord::Base
  attr_accessible :position, :source_uri, :img, :published

  belongs_to :property

  has_attached_file :img,
    :styles => { :large => "590x380>", :medium => "380x200>", :small => "280x150>", :thumb => "100x75>" },
    :url => "#{:source_uri}" 

  def source_uri=(uri)
    self.img_file_name = "#{random_base_name}#{ext_from_mime}"
    super
  end

  def source_uri
    URI.unescape(super)
  end

In my config/environments/production.rb, I have this:

  config.paperclip_defaults = {
    :storage => :s3,
    :bucket => ENV['MY_S3_BUCKET'],
    :path => "uploads/myapp/:class/:attachment/:id/:style/:filename",
    :s3_credentials => "#{Rails.root}/config/aws.yml",
    :default_url => ":attachment/:class/:style/missing.png"
  }

In my aws.yml, I have the following:

development:
  access_key_id:
  secret_access_key:
  bucket:

staging:
  access_key_id: <%= ENV["S3_ACCESS_KEY_ID"] %>
  secret_access_key: <%= ENV["S3_SECRET_ACCESS_KEY"] %>
  bucket: <%= ENV["S3_BUCKET"] %>

production:
  access_key_id: <%= ENV["MY_S3_ACCESS_KEY_ID"] %>
  secret_access_key: <%= ENV["MY_S3_SECRET_ACCESS_KEY"] %>
  bucket: <%= ENV["MY_S3_BUCKET"] %>

When I upload an image, it seems to work based on the logs:

  SQL (0.7ms)  INSERT INTO "photos" ("created_at", "featured", "img_content_type", "img_file_name", "img_file_size", "img_fingerprint", "img_updated_at", "position", "property_id", "published", "source_uri", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id"  [["created_at", Sat, 25 May 2019 05:52:17 UTC +00:00], ["featured", nil], ["img_content_type", nil], ["img_file_name", "65798ASDASD4"], ["img_file_size", nil], ["img_fingerprint", nil], ["img_updated_at", nil], ["position", nil], ["property_id", 18713], ["published", true], ["source_uri", "https://s3-us-west-2.amazonaws.com/propbmedia/BenjaminJohnson/31680-1.jpg"], ["updated_at", Sat, 25 May 2019 05:52:17 UTC +00:00]]
   (0.9ms)  COMMIT

However, if I were to poke around in my console, I would get the following:

irb(main):027:0> q.photos.first.source_uri
  Photo Load (11.2ms)  SELECT "photos".* FROM "photos" WHERE "photos"."property_id" = 16759 ORDER BY position ASC LIMIT 1
=> "https://s3-us-west-2.amazonaws.com/propbmedia/BenjaminJohnson/10192-16.jpg"
irb(main):028:0> p.photos.first.source_uri
=> "https://s3-us-west-2.amazonaws.com/propbmedia/BenjaminJohnson/33449-7.jpg"
irb(main):029:0> q.photos.first.img.url
  Photo Load (8.2ms)  SELECT "photos".* FROM "photos" WHERE "photos"."property_id" = 16759 ORDER BY position ASC LIMIT 1
=> "http://s3.amazonaws.com/johnson-benjamin/uploads/hojo/photos/imgs/208532/original/9086718487708773"
irb(main):030:0> p.photos.first.img.url
=> "http://s3.amazonaws.com/johnson-benjamin/uploads/hojo/photos/imgs/241108/original/7782737090666986"

Note that the first two URLs in the last snippet work, but the last 2 return 'access denied' errors.

I also tried to upload an avatar to S3, using Comfy CMS and it worked. Granted, I don't think that is managed by Paperclip (I could be wrong), but for some reason it uploads it somewhere else. Here is an example URL for the avatar that works - http://s3.amazonaws.com/johnson-benjamin/uploads/hojo/agents/avatars/21/large/10PP-COMING-SOON.jpg?1558767320

So what could be wrong with my main property.photos model?

Why is that?

Aucun commentaire:

Enregistrer un commentaire