0
votes

I have written a python script to connect to an amazon s3 server but it seems to fail when attempting to create a bucket (a time out error). I have omitted the secret key and id key for obvious reasons. Can anyone see what is wrong with this script? Thanks in advance

import boto
import sys, os
from boto.s3.key import Key
from boto.s3.connection import S3Connection
from boto.exception import S3ResponseError

LOCAL_PATH = '/Users/****/test'
aws_access_key_id = '****'
aws_secret_access_key = '****'
bucket_name = aws_access_key_id.lower() + '****'

class TimeoutException(Exception):
    pass

conn = boto.connect_s3(aws_access_key_id, aws_secret_access_key)
try:
    print "bucket name " + bucket_name;
    bucket = conn.get_bucket( bucket_name)
except TimeoutException:
    sys.exit("Connection timed out; this usually means you're offline.")
except S3ResponseError, exception_data:
    sys.exit(exception_data.error_message)

this is the error message i get:

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 941, in request self._send_request(method, url, body, headers) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 975, in _send_request self.endheaders(body) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 937, in endheaders self._send_output(message_body) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 797, in _send_output self.send(msg) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 759, in send self.connect() File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1140, in connect self.timeout, self.source_address) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection raise err socket.timeout: timed out

1

1 Answers

1
votes

You say that you are trying to create a bucket but the get_bucket() method does not create a bucket, it returns an existing bucket. If you want to create a new bucket, use create_bucket() instead. The normal approach would be to first use get_bucket() to see if a bucket exists and if it does not, then call create_bucket().

Also, I don't understand what this code is intended to do:

try:
    print "bucket name " + bucket_name;
    bucket = conn.get_bucket( bucket_name)
except TimeoutException:
    sys.exit("Connection timed out; this usually means you're offline.")
except S3ResponseError, exception_data:
    sys.exit(exception_data.error_message)

The TimeoutException class is a class you have created locally and the call to get_bucket() will never raise that exception because it doesn't know anything about it. The call to get_bucket() should either return an existing bucket or raise an S3ResponseError in normal operation.

The fact that you are getting a timeout error from the socket module seems to suggest that there is something wrong with your networking setup. Are you behind a proxy server? Can you perform any operation against the S3 service (e.g. list keys in a bucket, etc.)?