如何用Python将HEAD HTTP请求发送到Amazon S3?

时间:2022-08-04 15:32:42

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):

我正在尝试使用Python从S3资源中检索HTTP HEAD信息。不幸的是,响应总是返回403错误。我正在使用此处建议的代码,但不幸的是,这对S3请求不起作用。这是代码(省略主机和密钥详细信息):

>>> 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.

我也试过使用httplib2,但HEAD REQUEST只是挂起。

3 个解决方案

#1


If you are using a signed URL, the URL is signed by method (e.g. HEAD or GET). So you will need a different URL for each method.

如果您使用的是签名URL,则URL将按方法签名(例如HEAD或GET)。因此,每种方法都需要不同的URL。

#2


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.

403告诉您发送的请求有效(另一个故事是否正确),但由于某种原因您无权访问请求的页面。至少您知道正在发送有效的HTTP请求。

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.

我也不确定“HEAD”请求。你能不能使用“GET”或“POST”并自己提取标题?可能是“HEAD”没有被图书馆实施......我不确定 - 我通过快速谷歌搜索找到的文档非常不合适。

#3


403 HTTP code means forbidden. Probably, site administrator disabled this method.

403 HTTP代码意味着禁止。可能是站点管理员禁用了此方法。

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.

或者,您可以在python代码中使用conn.set_debuglevel(1)。

#1


If you are using a signed URL, the URL is signed by method (e.g. HEAD or GET). So you will need a different URL for each method.

如果您使用的是签名URL,则URL将按方法签名(例如HEAD或GET)。因此,每种方法都需要不同的URL。

#2


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.

403告诉您发送的请求有效(另一个故事是否正确),但由于某种原因您无权访问请求的页面。至少您知道正在发送有效的HTTP请求。

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.

我也不确定“HEAD”请求。你能不能使用“GET”或“POST”并自己提取标题?可能是“HEAD”没有被图书馆实施......我不确定 - 我通过快速谷歌搜索找到的文档非常不合适。

#3


403 HTTP code means forbidden. Probably, site administrator disabled this method.

403 HTTP代码意味着禁止。可能是站点管理员禁用了此方法。

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.

或者,您可以在python代码中使用conn.set_debuglevel(1)。