我是否需要在 Rails 应用程序中关闭与 Amazon S3 的连接?

发布于 2024-09-15 17:06:12 字数 550 浏览 5 评论 0原文

我正在将 Rails 应用程序迁移到 Heroku,并且需要更改文件上传功能以使用 Amazon S3 而不是本地存储。我正在使用 aws-s3 gem 并且可以正常工作,但只是想确保我做的事情正确并且不会给自己带来问题。

在我的上传代码中,我有以下内容;

AWS::S3::Base.establish_connection!(
    :access_key_id     => 'Not telling',
    :secret_access_key => 'Really not telling'
  )
AWS::S3::S3Object.store("#{self.name}", upload_file_field.read, 'my_bucket')

这工作得很好,但我担心我会打开某种与服务器的连接。完成后我是否需要关闭连接(例如使用 AWS::S3::Base.disconnect),或者我可以保持原样吗?

显然,我对与 S3 连接的幕后使用的协议不太了解,但我并不是特别想要 - 我只是想确保这能够正常工作而不会造成问题。

I'm migrating my rails app to Heroku and need to change my file upload functionality to use Amazon S3 instead of local storage. I am using the aws-s3 gem and have this working but just want to make sure that I'm doing things right and not creating problems for myself.

In my uploading code I have the following;

AWS::S3::Base.establish_connection!(
    :access_key_id     => 'Not telling',
    :secret_access_key => 'Really not telling'
  )
AWS::S3::S3Object.store("#{self.name}", upload_file_field.read, 'my_bucket')

This works perfectly but I'm concerned that I'm leaving some sort of connection to the server open. Do I need to close the connection once I'm done (e.g. with AWS::S3::Base.disconnect) or can I just leave this as it is?

Clearly I don't have a good understanding of the protocols being used behind the scenes with my connection to S3 but I don't particularly want to - I just want to make sure that this will work without causing problems.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨后咖啡店 2024-09-22 17:06:12

请在此处查看 API 文档,特别是断开连接方法:

http://amazon.rubyforge.org/doc/classes/AWS/S3/Connection/Management/ClassMethods.html#M000088

目前尚不清楚是否必须显式关闭每个连接。但是,它确实提到如果打开持久连接会提高性能,无论如何默认情况下都会这样做。

[...]

* :persistent - 是否使用与服务器的持久连接。

打开这个可以提供大约两个
折叠

性能提升,但持续时间较长
某些防火墙可能正在运行进程
找到长期存在的连接
可疑并关闭连接。
如果遇到连接错误,请尝试
设置:持续为 false。默认值
为真。

Check here for the docs for the API, and the disconnect method in particular:

http://amazon.rubyforge.org/doc/classes/AWS/S3/Connection/Management/ClassMethods.html#M000088

It's not clear about whether you must close each connection explicitly. However, it does mention a performance increase if you open a persistent connection, this is done by default anyway.

[...]

* :persistent - Whether to use a persistent connection to the server.

Having this on provides around a two
fold

performance increase but for long
running processes some firewalls may
find the long lived connection
suspicious and close the connection.
If you run into connection errors, try
setting :persistent to false. Defaults
to true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文