【发布时间】:2017-01-13 20:58:42
【问题描述】:
我正在使用一些数据和 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 凭证。根据网址,我必须在 Fog.rb 文件中写入凭据,但我没有任何此类文件。我在上传器文件夹中创建了一个 ImageUploader,它扩展了 CarrierWave 并编写了配置,但它也无法正常工作。
【问题讨论】:
标签: ruby-on-rails ruby amazon-s3 carrierwave