繁体   English   中英

如何从Rails控制器上载base64映像到s3?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM