我有一个rails应用5.2,Ruby 2.3.4和fog-google(1.3.3)。
自从上传和下载文件很长时间以来,它在开发,测试和产品环境方面一直表现出色。
但是现在雾存储拒绝上传文件。
如果我要用滑轨c放好
config = Rails.application.config.x.settings["cloud_storage"]
fogStorage = Fog::Storage.new(
provider: "Google",
google_storage_access_key_id: config["access_key_id"],
google_storage_secret_access_key: config["secret_access_key"]
)
storageBucket = fogStorage.directories.new key: config["bucket"]
file_name = File.join(Rails.root,"DSC00174.jpg")
file = File.open(file_name, "r")
image = storageBucket.files.new(
key: "logo_client_image_test/AAA.jpg",
body: file.read,
public: true
content_type:'image/jpeg'
)
image.save
puts image.public_url
我收到此消息
Excon::Error::RequestEntityTooLarge: Expected(200) <=> Actual(413 Request Entity Too Large)
excon.error.response
:body => "<!DOCTYPE html>\n<html lang=en>\n <meta charset=utf-8>\n <meta name=viewport content=\"initial-scale=1, minimum-scale=1, width=device-width\">\n <title>Error 413 (Request Entity Too Large)!!1</title>\n <style>\n *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}\n </style>\n <a href=//www.google.com/><span id=logo aria-label=Google></span></a>\n <p><b>413.</b> <ins>That\xE2\x80\x99s an error.</ins>\n <p>Your client issued a request that was too large.\n <script>\n (function() { var c=function(a,d,b){a=a+\"=deleted\"+(\"; path=\"+d);null!=b&&(a+=\"; domain=\"+b);document.cookie=a+\"; expires=Thu, 01 Jan 1970 00:00:00 GMT\"};var g=function(a){var d=e,b=location.hostname;c(d,a,null);c(d,a,b);for(var f=0;;){f=b.indexOf(\".\",f+1);if(0>f)break;c(d,a,b.substring(f+1))}};var h;if(4E3<unescape(encodeURI(document.cookie)).length){for(var k=document.cookie.split(\";\"),l=[],m=0;m<k.length;m++){var n=k[m].match(/^\\s*([^=]+)/);n&&l.push(n[1])}for(var p=0;p<l.length;p++){var e=l[p];g(\"/\");for(var q=location.pathname,r=0;;){r=q.indexOf(\"/\",r+1);if(0>r)break;var t=q.substring(0,r);g(t);g(t+\"/\")}\"/\"!=q.charAt(q.length-1)&&(g(q),g(q+\"/\"))}h=!0}else h=!1;\nh&&setTimeout(function(){if(history.replaceState){var a=location.href;history.replaceState(null,\"\",\"/\");location.replace(a)}},1E3); })();\n\n</script>\n <ins>That\xE2\x80\x99s all we know.</ins>\n"
:cookies => [
]
:headers => {
"Content-Length" => "2398"
"Content-Type" => "text/html; charset=UTF-8"
"Date" => "Thu, 19 Jul 2018 10:11:50 GMT"
"Referrer-Policy" => "no-referrer"
}
:host => "locabri.storage.googleapis.com"
:local_address => "192.168.1.44"
:local_port => 51136
:path => "/logo_client_image_test%2FAAADSC00174.jpg"
:port => 443
:reason_phrase => "Request Entity Too Large"
:remote_ip => "216.58.206.240"
:status => 413
:status_line => "HTTP/1.0 413 Request Entity Too Large\r\n"
文件大小为98ko。 我可以使用在线界面将文件手动传输到Google存储桶。
我最近所做的唯一更改是将Rails升级到5.2。和配置文件没有更改。
请帮助,请随时向我澄清
解决方案
我自己找到了解决方案。在Rails 5.2中,主体必须是file而不是file.read
image = storageBucket.files.new(
key: "logo_client_image_test/AAA.jpg",
body: file,
public: true
content_type:'image/jpeg'
)
希望这会对某人有所帮助