ga-wdi-boston / rails-paperclip

Setting up Paperclip within a Rails app, both with local storage and with S3.
2 stars 10 forks source link

Notes on how I got curl uploads to S3 to work #15

Open rhjones opened 7 years ago

rhjones commented 7 years ago

Attempting to use this for my capstone project. I have uploads working through CURL to AWS S3, and @berziiii asked me to share my notes on the steps I've taken so far. Some of these are a bit shortcut-y—focused on getting a file uploaded. I haven't attempted anything through the browser so far; I'll follow up once I have that working.

curl -v http://localhost:4741/movies \
  -H 'Content-Type: multipart/form-data' \
  -H 'Accept: application/json' \
  -F "movie[title]=BestMovie" \
  -F "movie[poster]=@demo.png;type=image/png"
config.paperclip_defaults = {
    storage: :s3,
    s3_region: ENV['AWS_REGION'],
    s3_credentials: {
      bucket: ENV['S3_BUCKET_NAME'],
      access_key_id: ENV['AWS_ACCESS_KEY_ID'],
      secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
    }
  }
rhjones commented 7 years ago

I wanted the S3 image URL to come back as part of the JSON sent after a successful upload. To do this:

Add an upload_url to the model that uses Paperclip's image.url method to grab the upload's URL:

def upload_url
  upload.url
end

And add :upload_url to the serializer

URLs come back as http://s3.amazonaws.com/.../1478185207.png?1478185207, which feels redundant to me. You can trim off the second timestamp by setting use_timestamp to false in the model:

has_attached_file :upload, :default_url => '/images/no-pattern-image.png', :use_timestamp => false

gaand commented 7 years ago

The time stamp may be used by aws to decide whether to return the file or just "no change".

jrhorn424 commented 7 years ago

@berziiii and I are thinking this might be a good repo to update and refresh since it's been used by a number of cohorts. Thoughts @gaand?