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

Open
rhjones opened this Issue Nov 3, 2016 · 3 comments

Projects

None yet

3 participants

@rhjones
rhjones commented Nov 3, 2016

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.

  • brew install ImageMagick
  • Download & set up Rails API template according to instructions in that repo
  • Scaffold a very basic resource: bundle exec rails g scaffold movie title:string
  • Test basic POST/GET routes with CURL
  • Add paperclip gem using current instructions from Thoughbot: gem 'paperclip', '~> 5.0.0'
  • Run bundle install
  • Edit model and movie_params method in controller according to instructions in this repo
  • Use the following curl script to test uploads (@jrhorn424 @berziiii @gaand feedback on whether this is the correct approach would be particularly welcome—it's functional, but there may be a better way):
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"
  • Follow the instructions in the express-multer repo to create a new IAM user and S3 bucket and set the bucket policy (ignore the instructions in this repo re: groups & non-admin privileges)
  • Add gem 'aws-sdk' to gemfile and run bundle install
  • Add the following variables to .env file: S3_BUCKET_NAME, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION. The bucket name, access key, and secret access key are documented in the express-multer repo. The region is visible in the URL bar and looks something like us-east-1
  • Add the following to config/environments/development.rb and config/environments/production.rb:
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']
    }
  }
  • Retest CURL script. Uploads should be going to S3 bucket instead of to public/ directory.
@rhjones rhjones changed the title from Notes on how I got this to work to Notes on how I got curl uploads to S3 to work Nov 3, 2016
@rhjones
rhjones commented Nov 3, 2016

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
gaand commented Nov 3, 2016

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

@rhjones rhjones referenced this issue in ga-wdi-boston/capstone-project Nov 4, 2016
Open

Uploading images to S3 via Rails & Ember: a how to #287

@jrhorn424
Member

@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?

@styletabtech styletabtech referenced this issue in ga-wdi-boston/capstone-project Nov 6, 2016
Closed

[CURL] Uploading files to AWS using Paperclip #292

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment