Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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

rhjones opened this issue Nov 3, 2016 · 3 comments

Comments

@rhjones
Copy link

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 Notes on how I got this to work Notes on how I got curl uploads to S3 to work Nov 3, 2016
@rhjones
Copy link
Author

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
Copy link

gaand commented Nov 3, 2016

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

@jrhorn424
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants