How to close Boto S3 connection?

Steve Saporta picture Steve Saporta · Apr 28, 2014 · Viewed 22.6k times · Source

I'm using Boto to connect to Amazon S3 in my Python program. I'm able to open a connection and upload files to a bucket. I figured I should then close the connection to release resources and, more important, to avoid any security risks from leaving an open connection hanging around. I assumed I should call the close() method. But I tested this as follows: 1. Open connection. 2. Close connection. 3. Upload file to bucket.

I figured step 3 would fail, but the upload worked! So what does close() do? If it doesn't really close the connection, what should I use in place of close()? Or is it just unnecessary to close the connection?

I've looked for the answer in the Boto tutorial, the Boto API reference, and this StackOverflow post, but no luck so far.

Thanks for your help.

Answer

garnaat picture garnaat · Apr 28, 2014

Your step 3 worked because boto has code that will automatically re-open closed connections and retry requests on errors. There is little to gain by manually closing the boto connections because they are just HTTP connections and will close automatically after a few minutes of idle time. I wouldn't worry about trying to close them.