Skip to content

Instantly share code, notes, and snippets.

@SunDi3yansyah
Last active May 7, 2020 15:42
Show Gist options
  • Save SunDi3yansyah/e9ae7c04cf6c1fe2f25027ae2203f4e9 to your computer and use it in GitHub Desktop.
Save SunDi3yansyah/e9ae7c04cf6c1fe2f25027ae2203f4e9 to your computer and use it in GitHub Desktop.
Ruby on Rails Upload Without Database

Ruby on Rails Upload Without Database

Solution 1

uploader = CsvUploader.new
File.open(file_path) do |file|
  something = uploader.store!(file)
end
uploader.retrieve_from_store!(self.file_name)

Solution 2

my_file = File.open ('./my_file.png')
uploader = AvatarUploader.new
uploader.store!(my_file)                              # size: 1024x768

uploader.url # => '/url/to/my_file.png'               # size: 800x600
uploader.thumb.url # => '/url/to/thumb_my_file.png'   # size: 200x200

Solution 3

image = params[:image].original_filename
path = File.join('public', 'images', 'upload', image)
File.open(path, 'wb') { |f| f.write(params[:image].read) }

Solution 4 (AWS S3)

file = params[:image]
file_name = file.original_filename
upload_file = file.tempfile
s3 = Aws::S3::Resource.new(region: ENV['AWS_REGION'], access_key_id: ENV['AWS_ACCESS_KEY'], secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'])

obj = s3.bucket(ENV['AWS_BUCKET_NAME']).object(file_name)
obj.upload_file(upload_file, { acl: 'public-read' })
render json: { data: obj.public_url }, status: 200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment