Wednesday, March 30, 2011

How to send a HEAD HTTP request in Python to Amazon S3?

I'm trying to retrieve HTTP HEAD information from a S3 resource using Python. Unfortunately the response always returns a 403 error. I am using code that was suggested here but unfortunately this does not work for S3 requests. Here is the code (host and key details omitted):

>>> import httplib
>>> conn = httplib.HTTPConnection("www.mybucket.us")
>>> conn.request("HEAD", "/mykey?myparamterers")
>>> res = conn.getresponse()
>>> res.status
>>> 403

The request also sends a signed expiration as part of the query string.

I have also tried using httplib2 but the HEAD REQUEST simply hangs.

From stackoverflow
  • 403 tells you that the request sent was valid (whether it was correct is another story), but that you don't have permission to access the requested page for some reason. At least you know that a valid HTTP request is being sent.

    Can you check the server log at all? That might help shed some light on the problem...

    I'm not sure about the "HEAD" request either. Can you not use "GET" or "POST" and extract the header yourself? It might be that "HEAD" is not implemented by the library... I am not sure - the documentation I've been able to find by quick googling is woefully inadequate.

    Richard Dorman : The reason for doing the HEAD REQUEST is to avoid doing an unnecessary GET. If I do an HTTP GET on the same url I get a 200 response. This should be the same for HTTP HEAD.
  • 403 HTTP code means forbidden. Probably, site administrator disabled this method.

    try telnet

    telnet www.mybucket.us 80
    HEAD http://www.mybucket.us/mykey?myparamterers
    Host: www.mybucket.us
    <ENTER>
    <ENTER>
    

    and watch for server response.

    Alternatively, you could use conn.set_debuglevel(1) in python code.

0 comments:

Post a Comment