[英]How to upload base64 image to s3 from rails controller?
我正在使用一些數據和base64圖像對我的控制器進行ajax拒絕,現在我想將此圖像上傳到s3並用圖像url替換base64。 我正在關注這個https://sebastiandobrincu.com/blog/how-to-upload-images-to-rails-api-using-s3
def split_base64(uri_str)
if uri_str.match(%r{^data:(.*?);(.*?),(.*)$})
uri = Hash.new
uri[:type] = $1 # "image/gif"
uri[:encoder] = $2 # "base64"
uri[:data] = $3 # data string
uri[:extension] = $1.split('/')[1] # "gif"
return uri
else
return nil
end
end
def convert_data_uri_to_upload(image_url)
image_data = split_base64(image_url)
image_data_string = image_data[:data]
image_data_binary = Base64.decode64(image_data_string)
temp_img_file = Tempfile.new("")
temp_img_file.binmode
temp_img_file << image_data_binary
temp_img_file.rewind
img_params = {:filename => "image.#{image_data[:extension]}", :type => image_data[:type], :tempfile => temp_img_file}
uploaded_file = ActionDispatch::Http::UploadedFile.new(img_params)
end
return uploaded_file
end
現在從我的控制器傳遞convert_data_uri_to_upload(base64_image)
現在,我不知道在哪里編寫AWS憑證。 根據url,我必須在Fog.rb文件中寫入憑據,但是我沒有任何此類文件。 我在上載器文件夾中創建了一個ImageUploader,該文件夾擴展了CarrierWave並編寫了配置,但它也無法正常工作。
您可以按照以下步驟使用dotenv ruby gem: http ://www.rubydoc.info/gems/dotenv-rails/2.1.1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.