Hướng dẫn dùng boto3 git python

Hướng dẫn dùng boto3 git python

Đã đăng vào thg 5 21, 2019 10:18 SA 1 phút đọc

Boto 3 là SDK AWS cho Python. Boto3 được viết bằng Python - vì vậy boto3 có thể thực hiện công cụ thao tác hưu hiệu với AWS. Boto3 đặc biệt dễ dàng sử dụng, khi làm việc với boto3 chúng ta có cảm giác giống như làm việc trên AWS CLI. Trong bài viết này, Tôi sẽ sử dụng SQS và boto 3 để thực hiện các thao tác cơ bản như gửi và nhận message.

Amazon Simple Queue Service (SQS) là một dịch vụ hàng đợi ( queue ) lưu trữ thông điệp ( message ) nhanh chóng, đáng tin cậy, có khả năng mở rộng và quản lý một cách đầy đủ. Amazon SQS giúp bạn có thể di chuyển dữ liệu giữa các thành phần phân tán của ứng dụng của bạn để thực hiện các nhiệm vụ khác nhau.

Setup

Giả sử các bạn đã cài sẵn python trên máy của các bạn. để cài boto3 chỉ cần chạy lệnh sau:

pip3 install boto3

xác nhận boto đã cài thành công hay chưa:

pip3 show boto3

Name: boto3
Version: 1.4.4
Summary: The AWS SDK for Python
Home-page: https://github.com/boto/boto3
Author: Amazon Web Services
Author-email: UNKNOWN
License: Apache License 2.0
Location: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
Requires: s3transfer, jmespath, botocore

Sử dụng

giả sử các bạn đã config đúng hết các kết nối với aws sqs rồi nhé. SQS hoạt động theo mô hình FIFO (First-In-First-Out), vào trước ra trước, chúng ta không thể order hay query vào SQS dc, mỗi lần gọi vào nó sẽ trả ra một message tương ứng với thời gian message này vào:

Sử dụng:

import boto3
# khởi tạo boto3 client
client = boto3.client('sqs')
  1. Tạo một sqs queue url
client.create_queue(QueueName='test_queue')

Response: 
{
    'QueueUrl': 'string'
}
  1. Lấy danh sách queue_url
queues = client.list_queues(QueueNamePrefix='test_queue')

Response: 
{
    'QueueUrls': [
        'string',
    ]
}
  1. Gửi message vào queue url
client.send_message(QueueUrl=test_queue_url, MessageBody='This is test message')

Response: 
{
    'MD5OfMessageBody': 'string',
    'MD5OfMessageAttributes': 'string',
    'MessageId': 'string',
    'SequenceNumber': 'string'
}

gửi 100 message vào queue:

for i in range(0,100):
    enqueue_response = client.send_message(QueueUrl=test_queue_url, MessageBody='This is test message #'+str(i))
    print('Message ID : ',enqueue_response['MessageId'])
  1. Nhận message từ queue
 messages = client.receive_message(QueueUrl=test_queue_url,MaxNumberOfMessages=10)
 
 Response: 
 
 {
    'Messages': [
        {
            'MessageId': 'string',
            'ReceiptHandle': 'string',
            'MD5OfBody': 'string',
            'Body': 'string',
            'Attributes': {
                'string': 'string'
            },
            'MD5OfMessageAttributes': 'string',
            'MessageAttributes': {
                'string': {
                    'StringValue': 'string',
                    'BinaryValue': b'bytes',
                    'StringListValues': [
                        'string',
                    ],
                    'BinaryListValues': [
                        b'bytes',
                    ],
                    'DataType': 'string'
                }
            }
        },
    ]
}

Tài liệu

  • https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.receive_message

All rights reserved

AWS (Amazon Web Services) là dịch vụ cloud nổi tiếng. Nó cung cấp cho chúng ta rất nhiều các dịch vụ như dịch vụ máy chủ EC2, lưu trữ S3, Load Balancing, … Ngoài ra AWS còn cung cấp rất nhiều API để quản lý các dịch vụ này bằng các ngôn ngữ khác nhau. Sau đây là 1 số ví dụ sử dụng Python để kết nối và quản lý các dịch vụ của AWS.

Để sử dụng AWS API các bạn cần cài đặt thư viện Boto3 của Python

Cài đặt thư viện Boto3

Để cài đặt boto3 chúng ta sử dụng trình quản lý package pip3 của Python

pip3 install boto3

Kiểm tra version sau khi cài đặt bằng lệnh pip3 show boto3

Kết nối tới dịch vụ AWS bằng Python 3

Kết nối tới dịch vụ EC2

Source code tham khảo

import boto3


ACCESS_KEY = ''
SECRET_KEY = ''
REGION_NAME = 'ap-northeast-1'


session = boto3.Session(
    aws_access_key_id=ACCESS_KEY,
    aws_secret_access_key=SECRET_KEY,
    region_name=REGION_NAME,
)


ec2Client = session.client('ec2')
ec2Resource = session.resource('ec2')


response = ec2Client.describe_instances()
for reservation in response["Reservations"]:
    for instance in reservation["Instances"]:
        # This sample print will output entire Dictionary object
        # print(instance)
        # You can also create a resource object from the instance item as well
        ec2 = ec2Resource.Instance(instance["InstanceId"])
        # print(ec2)

Kết nối tới dịch vụ S3

Source code tham khảo

import boto3


ACCESS_KEY = ''
SECRET_KEY = ''
REGION_NAME = 'ap-northeast-1'


session = boto3.Session(
    aws_access_key_id=ACCESS_KEY,
    aws_secret_access_key=SECRET_KEY,
    region_name=REGION_NAME,
)


s3Client = session.client('s3')
# List all buckets on your account.
response = s3Client.list_buckets()


for bucket in response['Buckets']:
    print(bucket)

Kết nối tới dịch vụ Route53

Source code tham khảo

import boto3


ACCESS_KEY = ''
SECRET_KEY = ''
REGION_NAME = 'ap-northeast-1'


session = boto3.Session(
    aws_access_key_id=ACCESS_KEY,
    aws_secret_access_key=SECRET_KEY,
    region_name=REGION_NAME,
)

r53Client = session.client('route53')
response = r53Client.list_hosted_zones()


for hostedZone in response['HostedZones']:
    response = r53Client.get_hosted_zone(
        Id=hostedZone['Id']
    )
    print(hostedZone)
    print(response)

Để kết nối tới dịch vụ  của AWS các  bạn cần 2 tham số là ACCESS_KEY và SECRET_KEY, để tạo 2 key này vui lòng tham khảo bài viết sau:

  • Hướng dẫn tạo và quản lý tài khoản IAM

Nguồn: vinasupport.com