[RUBY] Save twitter icon to s3

things to do

The link of the twitter icon obtained by logging in to twitter will be cut off immediately, so save it in s3.

Implementation details

Implementation of twitteromniauth

https://qiita.com/puremoru0315/items/f1d459b663fd3b715dee

The above article was easy to understand

Image saving process only at the time of new registration

The following process is written in omniauth

user.rb


class User < ApplicationRecord
  mount_uploader :image_url, ImageUploader
  #If there is a user related to the argument, it is returned, and if it exists, a new one is created.
  def self.find_or_create_from_auth_hash(auth_hash)
  #Substitute each data acquired by OmniAuth
      provider = auth_hash[:provider]
      uid = auth_hash[:uid]
      nickname = auth_hash[:info][:nickname]
      image = auth_hash[:info][:image]

      User.find_or_initialize_by(provider: provider, uid: uid) do |user|
        if user.new_record?
          user.nickname = nickname
          user.remote_image_url = image_url
          user.save
        end
      end
    end
end

find_or_initialize_by gets the user if it is looking for it, and creates a new one if it doesn't. Unlike find_or_create_by, find_or_create_by even saves. If you want to insert only new registration processing, use find_or_initialize_by, which allows you to save after conditional branching whether to create a new one with new_record ?.

Image save carrierwave

https://github.com/carrierwaveuploader/carrierwave#uploading-files-from-a-remote-location

The following processing is done in image saving

--Get the icon url from twitter --Dl the image from that url --And save the dropped image in s3

This process can be easily done with the remote_xxxx_url method.

Recommended Posts

Save twitter icon to s3
Save images to Amazon S3 (Local / Heroku)
How to save images on Heroku to S3 on AWS
Save twitter icon to s3
[Note] Download from S3, upload to S3
upload images to refile heroku S3
[Note] Download from S3, upload to S3
upload images to refile heroku S3
Attempts to automatically increase Twitter followers
Change the save destination of the image to S3 in the Rails app. Part 2