Skip to content

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

@rhjones

Description

@rhjones

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions