In Amazon S3 am I charged whenever I perform a get_key or when I actually get its contents?

Question:

i.e. will this code cause it to appear as 2 GET requests in my bill, or only 1?

key = bucket.get_key('some_invalid_key')
key = bucket.get_key('some_valid_key')
string = key.get_contents_to_string()
Asked By: wuxiekeji

||

Answers:

By default, get_key validate existence of the key on the bucket by HEAD request (which is rather small one, but the request counts to number of requests you do).

If you do not want to make this validation, set validate=False and boto will not make the request.

Docstring for get_key:

Type:        instancemethod
String form: <bound method Bucket.get_key of <Bucket: tmpdistributionplatform>>
File:        /home/javl/Envs/so/local/lib/python2.7/site-packages/boto/s3/bucket.py
Definition:  bucket.get_key(self, key_name, headers=None, version_id=None, response_headers=None, validate=True)
Docstring:
Check to see if a particular key exists within the bucket.  This
method uses a HEAD request to check for the existance of the key.
Returns: An instance of a Key object or None

:param key_name: The name of the key to retrieve
:type key_name: string

:param headers: The headers to send when retrieving the key
:type headers: dict

:param version_id:
:type version_id: string

:param response_headers: A dictionary containing HTTP
    headers/values that will override any headers associated
    with the stored object in the response.  See
    http://goo.gl/EWOPb for details.
:type response_headers: dict

:param validate: Verifies whether the key exists. If ``False``, this
    will not hit the service, constructing an in-memory object.
    Default is ``True``.
:type validate: bool

:rtype: :class:`boto.s3.key.Key`
:returns: A Key object from this bucket.

Conclusions

  • By default get_key generates one HEAD requests and you are charged for it.
  • You are allowed to prevent this request by setting validate=False
  • Amount of data transferred is anyway very small as HEAD only checks basic metadata and does not attempt to download all the content.
  • HEAD request falls into category GET and all others and cost $0.004 per 10,000 requests (as of now for US Standard zone), so generally it is negligible unless you do a lot of these requests.
  • to your question: your code will generate 2 HEAD and one GET requests and all three will be counted.
Answered By: Jan Vlcinsky