开发者

Do I need to close my connections to Amazon S3 in my Rails App?

开发者 https://www.devze.com 2023-01-12 11:47 出处:网络
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 s

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号