问题描述
在我的Rails 4应用程序中,我试图下载一个常规的png文件,然后使用aws-sdk(使用gem 'aws-sdk', '~> 2'
)将其上载到s3存储桶中.
In my rails 4 application I'm trying to download and then upload a regular png file to my s3 bucket using the aws-sdk (using gem 'aws-sdk', '~> 2'
).
在开发环境中,代码可以正常工作.但是,如果尝试rails s -e production
或在heroku实例上测试上传,则在测试图像上传功能时会收到以下错误消息,
In development environment, the code works totally fine. But if I try rails s -e production
or if I test out the upload on my heroku instance, I get the following error when I test out the image upload functionality,
Seahorse::Client::NetworkingError (Connection reset by peer):
app/helpers/aws_s3.rb:73:in `upload_to_s3'
app/controllers/evaluations_controller.rb:19:in `test'
跟踪中提到的
我的upload_to_s3方法如下:
my upload_to_s3 method mentioned in the trace looks like:
def upload_to_s3(folder_name)
url = "http://i.imgur.com/WKeQQox.png"
filename = "ss-" + DateTime.now.strftime("%Y%d%m-%s") + "-" + SecureRandom.hex(4) + ".png"
full_bucket_path = Pathname(folder_name.to_s).join(filename).to_s
file = save_to_tempfile(url, filename)
s3 = Aws::S3::Resource.new(access_key_id: ENV["IAM_ID"], secret_access_key: ENV["IAM_SECRET"], region: 'us-east-1')
s3_file = s3.bucket(ENV["BUCKET"]).object(full_bucket_path)
s3_file.upload_file(file.path)
raise s3_file.public_url.to_s.inspect
end
两个环境之间的环境变量相同.我真的不知道该去哪里调试.为什么它在开发中起作用,但在生产中却不起作用?我有一种明显的失落感.
The environment variables are the same between both environments. I don't really know where else to turn to debug this. Why would it work in development, but not in production? I have a feeling I'm missing something pretty obvious.
更新:
让我进一步简化一下,因为我没有收到太多反馈.
Let's simplify this further since I'm not getting very much feedback.
s3 = Aws::S3::Resource.new
bucket = s3.bucket(ENV["BUCKET"])
bucket.object("some_file.txt").put(body:'Hello World!')
以上内容完全适用于我的开发环境,但不适用于我的生产环境.在生产中,当我调用put(body:'Hello World!')
时会出错.我知道这可能与写权限有关,但是再次,我检查了我的环境变量,它们是相同的.有一些我不知道应该检查的配置吗?
The above totally works in my development environment, but not my production environment. In production it faults when I call put(body:'Hello World!')
. I know this is probably related to write permissions or something, but again, I've checked my environment variables, and they are identical. Is there some configuration that I'm not aware of that I should be checking?
我尝试使用新的IAM用户.我还暂时将development.rb的全部内容复制到production.rb上,只是看开发或生产的配置是否正在影响它,但无济于事.我也跑了包更新.再次,没有运气.
I've tried using a new IAM user. I also temporarily copied the entire contents of development.rb over to production.rb just to see if the configuration for the development or production was affecting it, to no avail. I ran bundle update as well. Again, no luck.
我希望该错误更具描述性,但是无论我尝试如何,它只会显示Seahorse::Client::NetworkingError (Connection reset by peer)
.
I wish the error was more descriptive, but it just says Seahorse::Client::NetworkingError (Connection reset by peer)
no matter what I try.
推荐答案
好吧,自从我有最后期限以来,我从未找到解决此问题的方法,而不得不求助于其他选择.我假设这是Amazon端或aws-sdk gem的错误,因为我已经多次检查了配置,而且是正确的.
Well I never found the solution for this problem and had to resort to other options since I was on a deadline. I'm assuming it is a bug on Amazon's end or with the aws-sdk gem, because I have checked my configuration many times, and it is correct.
我的解决方法是使用雾宝石,这实际上非常方便.在将gem 'fog'
添加到我的gemfile并运行bundle install
之后,我的代码现在如下所示:
My workaround was to use the fog gem, which is actually very handy. after adding gem 'fog'
to my gemfile and running bundle install
my code now looks like this:
def upload_to_s3(folder_name)
filename = "ss-" + DateTime.now.strftime("%Y%d%m-%s") + "-" + SecureRandom.hex(4) + ".png"
full_bucket_path = Pathname(folder_name.to_s).join(filename).to_s
image_contents = open(url).read
connection = Fog::Storage.new({
:provider => 'AWS',
:aws_access_key_id => ENV["AWS_ACCESS_KEY_ID"],
:aws_secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"]
})
directory = connection.directories.get(ENV["BUCKET"])
file = directory.files.create(key: full_bucket_path, public: true)
file.body = image_contents
file.save
return file.public_url
end
这很简单并且易于实现.希望我知道aws-sdk宝石到底是怎么回事,但是对于其他有问题的人,请尝试一下雾气.
Which is simple enough and was a breeze to implement. Wish I knew what was messing up with the aws-sdk gem, but for anyone else who has problems, give fog a go.
这篇关于Seahorse :: Client :: NetworkingError使用Rails上传Amazon S3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!