PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/boto-2.5.2/boto/sqs/connection.py

#
Python | 414 lines | 384 code | 6 blank | 24 comment | 2 complexity | ce2eeb87076fe411e9e96a51250fda08 MD5 | raw file
  1. # Copyright (c) 2006-2009 Mitch Garnaat http://garnaat.org/
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a
  4. # copy of this software and associated documentation files (the
  5. # "Software"), to deal in the Software without restriction, including
  6. # without limitation the rights to use, copy, modify, merge, publish, dis-
  7. # tribute, sublicense, and/or sell copies of the Software, and to permit
  8. # persons to whom the Software is furnished to do so, subject to the fol-
  9. # lowing conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included
  12. # in all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
  16. # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  17. # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. # IN THE SOFTWARE.
  21. from boto.connection import AWSQueryConnection
  22. from boto.sqs.regioninfo import SQSRegionInfo
  23. from boto.sqs.queue import Queue
  24. from boto.sqs.message import Message
  25. from boto.sqs.attributes import Attributes
  26. from boto.sqs.batchresults import BatchResults
  27. from boto.exception import SQSError, BotoServerError
  28. class SQSConnection(AWSQueryConnection):
  29. """
  30. A Connection to the SQS Service.
  31. """
  32. DefaultRegionName = 'us-east-1'
  33. DefaultRegionEndpoint = 'sqs.us-east-1.amazonaws.com'
  34. APIVersion = '2011-10-01'
  35. DefaultContentType = 'text/plain'
  36. ResponseError = SQSError
  37. def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
  38. is_secure=True, port=None, proxy=None, proxy_port=None,
  39. proxy_user=None, proxy_pass=None, debug=0,
  40. https_connection_factory=None, region=None, path='/',
  41. security_token=None):
  42. if not region:
  43. region = SQSRegionInfo(self, self.DefaultRegionName,
  44. self.DefaultRegionEndpoint)
  45. self.region = region
  46. AWSQueryConnection.__init__(self, aws_access_key_id,
  47. aws_secret_access_key,
  48. is_secure, port,
  49. proxy, proxy_port,
  50. proxy_user, proxy_pass,
  51. self.region.endpoint, debug,
  52. https_connection_factory, path,
  53. security_token=security_token)
  54. def _required_auth_capability(self):
  55. return ['sqs']
  56. def _credentials_expired(self, response):
  57. if response.status != 401:
  58. return False
  59. error = BotoServerError('', '', body=response.read())
  60. return error.error_code == 'InvalidAccessKeyId'
  61. def create_queue(self, queue_name, visibility_timeout=None):
  62. """
  63. Create an SQS Queue.
  64. :type queue_name: str or unicode
  65. :param queue_name: The name of the new queue. Names are
  66. scoped to an account and need to be unique within that
  67. account. Calling this method on an existing queue name
  68. will not return an error from SQS unless the value for
  69. visibility_timeout is different than the value of the
  70. existing queue of that name. This is still an expensive
  71. operation, though, and not the preferred way to check for
  72. the existence of a queue. See the
  73. :func:`boto.sqs.connection.SQSConnection.lookup` method.
  74. :type visibility_timeout: int
  75. :param visibility_timeout: The default visibility timeout for
  76. all messages written in the queue. This can be overridden
  77. on a per-message.
  78. :rtype: :class:`boto.sqs.queue.Queue`
  79. :return: The newly created queue.
  80. """
  81. params = {'QueueName': queue_name}
  82. if visibility_timeout:
  83. params['Attribute.1.Name'] = 'VisibilityTimeout'
  84. params['Attribute.1.Value'] = int(visibility_timeout)
  85. return self.get_object('CreateQueue', params, Queue)
  86. def delete_queue(self, queue, force_deletion=False):
  87. """
  88. Delete an SQS Queue.
  89. :type queue: A Queue object
  90. :param queue: The SQS queue to be deleted
  91. :type force_deletion: Boolean
  92. :param force_deletion: Normally, SQS will not delete a queue
  93. that contains messages. However, if the force_deletion
  94. argument is True, the queue will be deleted regardless of
  95. whether there are messages in the queue or not. USE WITH
  96. CAUTION. This will delete all messages in the queue as
  97. well.
  98. :rtype: bool
  99. :return: True if the command succeeded, False otherwise
  100. """
  101. return self.get_status('DeleteQueue', None, queue.id)
  102. def get_queue_attributes(self, queue, attribute='All'):
  103. """
  104. Gets one or all attributes of a Queue
  105. :type queue: A Queue object
  106. :param queue: The SQS queue to be deleted
  107. :type attribute: str
  108. :type attribute: The specific attribute requested. If not
  109. supplied, the default is to return all attributes. Valid
  110. attributes are:
  111. * ApproximateNumberOfMessages|
  112. * ApproximateNumberOfMessagesNotVisible|
  113. * VisibilityTimeout|
  114. * CreatedTimestamp|
  115. * LastModifiedTimestamp|
  116. * Policy
  117. :rtype: :class:`boto.sqs.attributes.Attributes`
  118. :return: An Attributes object containing request value(s).
  119. """
  120. params = {'AttributeName' : attribute}
  121. return self.get_object('GetQueueAttributes', params,
  122. Attributes, queue.id)
  123. def set_queue_attribute(self, queue, attribute, value):
  124. params = {'Attribute.Name' : attribute, 'Attribute.Value' : value}
  125. return self.get_status('SetQueueAttributes', params, queue.id)
  126. def receive_message(self, queue, number_messages=1,
  127. visibility_timeout=None, attributes=None):
  128. """
  129. Read messages from an SQS Queue.
  130. :type queue: A Queue object
  131. :param queue: The Queue from which messages are read.
  132. :type number_messages: int
  133. :param number_messages: The maximum number of messages to read
  134. (default=1)
  135. :type visibility_timeout: int
  136. :param visibility_timeout: The number of seconds the message should
  137. remain invisible to other queue readers
  138. (default=None which uses the Queues default)
  139. :type attributes: str
  140. :param attributes: The name of additional attribute to return
  141. with response or All if you want all attributes. The
  142. default is to return no additional attributes. Valid
  143. values:
  144. * All
  145. * SenderId
  146. * SentTimestamp
  147. * ApproximateReceiveCount
  148. * ApproximateFirstReceiveTimestamp
  149. :rtype: list
  150. :return: A list of :class:`boto.sqs.message.Message` objects.
  151. """
  152. params = {'MaxNumberOfMessages' : number_messages}
  153. if visibility_timeout:
  154. params['VisibilityTimeout'] = visibility_timeout
  155. if attributes:
  156. self.build_list_params(params, attributes, 'AttributeName')
  157. return self.get_list('ReceiveMessage', params,
  158. [('Message', queue.message_class)],
  159. queue.id, queue)
  160. def delete_message(self, queue, message):
  161. """
  162. Delete a message from a queue.
  163. :type queue: A :class:`boto.sqs.queue.Queue` object
  164. :param queue: The Queue from which messages are read.
  165. :type message: A :class:`boto.sqs.message.Message` object
  166. :param message: The Message to be deleted
  167. :rtype: bool
  168. :return: True if successful, False otherwise.
  169. """
  170. params = {'ReceiptHandle' : message.receipt_handle}
  171. return self.get_status('DeleteMessage', params, queue.id)
  172. def delete_message_batch(self, queue, messages):
  173. """
  174. Deletes a list of messages from a queue in a single request.
  175. :type queue: A :class:`boto.sqs.queue.Queue` object.
  176. :param queue: The Queue to which the messages will be written.
  177. :type messages: List of :class:`boto.sqs.message.Message` objects.
  178. :param messages: A list of message objects.
  179. """
  180. params = {}
  181. for i, msg in enumerate(messages):
  182. prefix = 'DeleteMessageBatchRequestEntry'
  183. p_name = '%s.%i.Id' % (prefix, (i+1))
  184. params[p_name] = msg.id
  185. p_name = '%s.%i.ReceiptHandle' % (prefix, (i+1))
  186. params[p_name] = msg.receipt_handle
  187. return self.get_object('DeleteMessageBatch', params, BatchResults,
  188. queue.id, verb='POST')
  189. def delete_message_from_handle(self, queue, receipt_handle):
  190. """
  191. Delete a message from a queue, given a receipt handle.
  192. :type queue: A :class:`boto.sqs.queue.Queue` object
  193. :param queue: The Queue from which messages are read.
  194. :type receipt_handle: str
  195. :param receipt_handle: The receipt handle for the message
  196. :rtype: bool
  197. :return: True if successful, False otherwise.
  198. """
  199. params = {'ReceiptHandle' : receipt_handle}
  200. return self.get_status('DeleteMessage', params, queue.id)
  201. def send_message(self, queue, message_content, delay_seconds=None):
  202. params = {'MessageBody' : message_content}
  203. if delay_seconds:
  204. params['DelaySeconds'] = int(delay_seconds)
  205. return self.get_object('SendMessage', params, Message,
  206. queue.id, verb='POST')
  207. def send_message_batch(self, queue, messages):
  208. """
  209. Delivers up to 10 messages to a queue in a single request.
  210. :type queue: A :class:`boto.sqs.queue.Queue` object.
  211. :param queue: The Queue to which the messages will be written.
  212. :type messages: List of lists.
  213. :param messages: A list of lists or tuples. Each inner
  214. tuple represents a single message to be written
  215. and consists of and ID (string) that must be unique
  216. within the list of messages, the message body itself
  217. which can be a maximum of 64K in length, and an
  218. integer which represents the delay time (in seconds)
  219. for the message (0-900) before the message will
  220. be delivered to the queue.
  221. """
  222. params = {}
  223. for i, msg in enumerate(messages):
  224. p_name = 'SendMessageBatchRequestEntry.%i.Id' % (i+1)
  225. params[p_name] = msg[0]
  226. p_name = 'SendMessageBatchRequestEntry.%i.MessageBody' % (i+1)
  227. params[p_name] = msg[1]
  228. p_name = 'SendMessageBatchRequestEntry.%i.DelaySeconds' % (i+1)
  229. params[p_name] = msg[2]
  230. return self.get_object('SendMessageBatch', params, BatchResults,
  231. queue.id, verb='POST')
  232. def change_message_visibility(self, queue, receipt_handle,
  233. visibility_timeout):
  234. """
  235. Extends the read lock timeout for the specified message from
  236. the specified queue to the specified value.
  237. :type queue: A :class:`boto.sqs.queue.Queue` object
  238. :param queue: The Queue from which messages are read.
  239. :type receipt_handle: str
  240. :param queue: The receipt handle associated with the message whose
  241. visibility timeout will be changed.
  242. :type visibility_timeout: int
  243. :param visibility_timeout: The new value of the message's visibility
  244. timeout in seconds.
  245. """
  246. params = {'ReceiptHandle' : receipt_handle,
  247. 'VisibilityTimeout' : visibility_timeout}
  248. return self.get_status('ChangeMessageVisibility', params, queue.id)
  249. def change_message_visibility_batch(self, queue, messages):
  250. """
  251. A batch version of change_message_visibility that can act
  252. on up to 10 messages at a time.
  253. :type queue: A :class:`boto.sqs.queue.Queue` object.
  254. :param queue: The Queue to which the messages will be written.
  255. :type messages: List of tuples.
  256. :param messages: A list of tuples where each tuple consists
  257. of a :class:`boto.sqs.message.Message` object and an integer
  258. that represents the new visibility timeout for that message.
  259. """
  260. params = {}
  261. for i, t in enumerate(messages):
  262. prefix = 'ChangeMessageVisibilityBatchRequestEntry'
  263. p_name = '%s.%i.Id' % (prefix, (i+1))
  264. params[p_name] = t[0].id
  265. p_name = '%s.%i.ReceiptHandle' % (prefix, (i+1))
  266. params[p_name] = t[0].receipt_handle
  267. p_name = '%s.%i.VisibilityTimeout' % (prefix, (i+1))
  268. params[p_name] = t[1]
  269. return self.get_object('ChangeMessageVisibilityBatch',
  270. params, BatchResults,
  271. queue.id, verb='POST')
  272. def get_all_queues(self, prefix=''):
  273. """
  274. Retrieves all queues.
  275. :keyword str prefix: Optionally, only return queues that start with
  276. this value.
  277. :rtype: list
  278. :returns: A list of :py:class:`boto.sqs.queue.Queue` instances.
  279. """
  280. params = {}
  281. if prefix:
  282. params['QueueNamePrefix'] = prefix
  283. return self.get_list('ListQueues', params, [('QueueUrl', Queue)])
  284. def get_queue(self, queue_name):
  285. """
  286. Retrieves the queue with the given name, or ``None`` if no match
  287. was found.
  288. :param str queue_name: The name of the queue to retrieve.
  289. :rtype: :py:class:`boto.sqs.queue.Queue` or ``None``
  290. :returns: The requested queue, or ``None`` if no match was found.
  291. """
  292. params = {'QueueName': queue_name}
  293. try:
  294. return self.get_object('GetQueueUrl', params, Queue)
  295. except SQSError:
  296. return None
  297. lookup = get_queue
  298. #
  299. # Permissions methods
  300. #
  301. def add_permission(self, queue, label, aws_account_id, action_name):
  302. """
  303. Add a permission to a queue.
  304. :type queue: :class:`boto.sqs.queue.Queue`
  305. :param queue: The queue object
  306. :type label: str or unicode
  307. :param label: A unique identification of the permission you are setting.
  308. Maximum of 80 characters ``[0-9a-zA-Z_-]``
  309. Example, AliceSendMessage
  310. :type aws_account_id: str or unicode
  311. :param principal_id: The AWS account number of the principal
  312. who will be given permission. The principal must have an
  313. AWS account, but does not need to be signed up for Amazon
  314. SQS. For information about locating the AWS account
  315. identification.
  316. :type action_name: str or unicode
  317. :param action_name: The action. Valid choices are:
  318. * *
  319. * SendMessage
  320. * ReceiveMessage
  321. * DeleteMessage
  322. * ChangeMessageVisibility
  323. * GetQueueAttributes
  324. :rtype: bool
  325. :return: True if successful, False otherwise.
  326. """
  327. params = {'Label': label,
  328. 'AWSAccountId' : aws_account_id,
  329. 'ActionName' : action_name}
  330. return self.get_status('AddPermission', params, queue.id)
  331. def remove_permission(self, queue, label):
  332. """
  333. Remove a permission from a queue.
  334. :type queue: :class:`boto.sqs.queue.Queue`
  335. :param queue: The queue object
  336. :type label: str or unicode
  337. :param label: The unique label associated with the permission
  338. being removed.
  339. :rtype: bool
  340. :return: True if successful, False otherwise.
  341. """
  342. params = {'Label': label}
  343. return self.get_status('RemovePermission', params, queue.id)