【问题标题】:How to upload base64 image to s3 from rails controller?如何从 Rails 控制器将 base64 图像上传到 s3?
【发布时间】: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


    【解决方案1】:

    您可以按照以下方式使用 dotenv ruby​​ gem:http://www.rubydoc.info/gems/dotenv-rails/2.1.1

    【讨论】:

    • 虽然理论上这可以回答这个问题,it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
    猜你喜欢
    • 1970-01-01
    • 2013-08-27
    • 2015-01-26
    • 2014-06-26
    • 2017-08-01
    • 1970-01-01
    • 2020-02-19
    • 2016-04-14
    • 1970-01-01
    相关资源
    最近更新 更多