]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
examples/boto3: Updating Python examples to work with Python 3 46650/head
authordux <faithuniterh@tutanota.com>
Tue, 12 Jul 2022 13:29:37 +0000 (13:29 +0000)
committerdux <faithuniterh@tutanota.com>
Tue, 12 Jul 2022 14:53:35 +0000 (14:53 +0000)
Signed-off-by: dux <faithuniterh@tutanota.com>
examples/boto3/topic_attributes.py
examples/boto3/topic_with_endpoint.py [changed mode: 0755->0644]

index 3caeb1fec26a32637ba982544fffa12b7f7b7aeb..3657459d8c824c170a834f2a1ed3b9a27842597a 100644 (file)
@@ -1,46 +1,23 @@
 import sys
-import urllib
-import hmac
-import hashlib
-import base64
-import xmltodict
-import http.client
-from urllib import parse as urlparse
-from time import gmtime, strftime
+import boto3
+from pprint import pprint
 
 if len(sys.argv) == 2:
     # topic arn as first argument
     topic_arn = sys.argv[1]
 else:
-    print ('Usage: ' + sys.argv[0] + ' <topic arn> [region name]')
+    print ('Usage: ' + sys.argv[0] + ' <topic arn>')
     sys.exit(1)
 
 # endpoint and keys from vstart
-endpoint = '127.0.0.1:8000'
+endpoint = 'http://127.0.0.1:8000'
 access_key='0555b35654ad1656d804'
 secret_key='h7GhxuBLTrlhVUyxSPUKUV8r/2EI4ngqJxD7iBdBYLhwluN30JaT3Q=='
 
-
-parameters = {'Action': 'GetTopic', 'TopicArn': topic_arn}
-body = urlparse.urlencode(parameters)
-string_date = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
-content_type = 'application/x-www-form-urlencoded; charset=utf-8'
-resource = '/'
-method = 'POST'
-string_to_sign = method + '\n\n' + content_type + '\n' + string_date + '\n' + resource 
-signature = base64.b64encode(hmac.new(secret_key.encode('utf-8'), string_to_sign.encode('utf-8'), hashlib.sha1).digest()).decode('ascii')
-headers = {'Authorization': 'AWS '+access_key+':'+signature,
-                   'Date': string_date,
-                   'Host': endpoint,
-                   'Content-Type': content_type}
-http_conn = http.client.HTTPConnection(endpoint)
-http_conn.request(method, resource, body, headers)
-response = http_conn.getresponse()
-data = response.read()
-status = response.status
-http_conn.close()
-dict_response = xmltodict.parse(data)
-
+# Add info to client to get the topi attirubutes of a given topi
+client = boto3.client('sns',
+        endpoint_url=endpoint,
+        aws_access_key_id=access_key,
+        aws_secret_access_key=secret_key)
 # getting attributes of a specific topic is an extension to AWS sns
-
-print(dict_response, status)
+pprint(client.get_topic_attributes(TopicArn=topic_arn))
old mode 100755 (executable)
new mode 100644 (file)
index 3137cee..b21fe46
@@ -1,21 +1,13 @@
 #!/usr/bin/python
-
 import boto3
-import sys
-import urlparse
 from botocore.client import Config
+import sys
 
-if len(sys.argv) == 3:
+if len(sys.argv) == 2:
     # topic name as first argument
     topic_name = sys.argv[1]
-    # region name as second argument
-    region_name = sys.argv[2]
-elif len(sys.argv) == 2:
-    # topic name as first argument
-    topic_name = sys.argv[1]
-    region_name = ""
 else:
-    print('Usage: ' + sys.argv[0] + ' <topic name> [region name]')
+    print('Usage: ' + sys.argv[0] + ' <topic name> ')
     sys.exit(1)
 
 # endpoint and keys from vstart
@@ -26,7 +18,6 @@ secret_key='h7GhxuBLTrlhVUyxSPUKUV8r/2EI4ngqJxD7iBdBYLhwluN30JaT3Q=='
 client = boto3.client('sns',
         endpoint_url=endpoint,
         aws_access_key_id=access_key,
-        region_name=region_name,
         aws_secret_access_key=secret_key,
         config=Config(signature_version='s3'))
 
@@ -34,8 +25,6 @@ client = boto3.client('sns',
 # radosgw-admin realm zonegroup list
 
 # this is standard AWS services call, using custom attributes to add AMQP endpoint information to the topic
-
-endpoint_args = 'push-endpoint=amqp://127.0.0.1:5672&amqp-exchange=ex1&amqp-ack-level=broker'
-attributes = {nvp[0] : nvp[1] for nvp in urlparse.parse_qsl(endpoint_args, keep_blank_values=True)}
+attributes = {"push-endpoint": "amqp://localhost:5672", "amqp-exchange": "ex1", "amqp-ack-level": "broker"}
 
 print(client.create_topic(Name=topic_name, Attributes=attributes))