【发布时间】:2015-11-06 18:14:20
【问题描述】:
我正在进行从 MySQL 到 Postgres 的数据迁移,从一列中包含图像的数据库迁移到所有图像都上传到 Filepicker 的新数据库。我正在寻找使用 Filepicker REST API 来转换它,但在发送带有这些图像的 POST 请求时不断遇到 400 个错误请求。我找不到明确定义的答案,并且对这个工作领域很陌生 - 希望有人能够为我指出正确的方向。
我有以下代码尝试将此作为更大的 Kiba ELT 迁移的一部分(我很确定这与这个 qu 无关):
# ***************
# Image conversion
# ***************
# Write the old file to a temporary new one
file = File.new("oldimages/oldimage#{row[:id]}.jpg", 'w')
file.write(row[:image])
file.close
# Access this new file
file = File.read("oldimages/oldimage#{row[:id]}.jpg")
# Fix issue with invalid UTF8
puts "Valid file encoding before conversion? " + file.valid_encoding?.to_s
if !file.valid_encoding?
file = file.encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8')
end
puts "Valid file encoding after conversion? " + file.valid_encoding?.to_s
# Set URI
uri = "https://www.filepicker.io/api/store/S3?key=#{ENV['FILEPICKER_API_KEY']}"
puts "URI being used: " + uri
# Make post request to Filepicker sending the file to be uploaded
# This works: curl -X POST -F fileUpload=@oldimages/oldimage1.jpg https://www.filepicker.io/api/store/S3\?key\=ENV['FILEPICKER_API_KEY']
# Also works as { "url" => SOME_IMAGE_URL }
# ...... BUT THIS DOESN'T WORK
params = { "fileUpload" => "@oldimages/oldimage#{row[:id]}.jpg" }
puts "Here the params: " + params.to_s
res = RestClient.post uri, params
# Convert the response into a hash and grab the new Filepicker URL
puts "Response: " + res.to_s
puts "Response FP URL: " + eval(res.to_s)[:url]
fp_url = eval(res.to_s)[:url]
# Assign the Filepicker URL to the image
newrow[:filepicker_url] = fp_url
puts "Newrow FP URL AFTER assignment: " + newrow[:filepicker_url]
# ***************
# end image conversion
# ***************
因此,我可以通过将图像的 URL 作为 post 请求的参数传递,并在命令行上使用文件和 cURL 来实现此功能。但是,如果没有收到错误请求,我无法让这个 Ruby 找到文件。我觉得我已经尝试过无数次迭代这段代码,试图找到一个可行的解决方案,但到目前为止无济于事。
关于 here 主题的文件选择器文档。
是否将文件路径作为字符串传递给参数会破坏这一点?
任何帮助,我永远感激不尽。如果您可以使用任何额外的代码,请告诉我......
史蒂夫。
【问题讨论】:
标签: ruby-on-rails ruby post database-migration filepicker.io