Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีใช้ Boto3 ดึงรายละเอียด Security Configuration เฉพาะตัวจาก AWS Glue Security

ข้อกำหนดปัญหา: ใช้ไลบรารี Boto3 ใน Python เพื่อดึงรายละเอียดการตั้งค่าความปลอดภัยที่ระบุใน AWS Glue Security

ตัวอย่าง: ดึงรายละเอียด Security Configuration ชื่อ 'job-security-settings' ใน AWS Glue Security

ขั้นตอนการแก้ปัญหา

  1. ขั้นตอน 1: นำเข้า boto3 และ botocore exceptions
  2. ขั้นตอน 2: security_name เป็นพารามิเตอร์บังคับที่ต้องการดึงรายละเอียด
  3. ขั้นตอน 3: สร้าง AWS session ด้วย Boto3
  4. ขั้นตอน 4: สร้าง AWS client สำหรับ Glue
  5. ขั้นตอน 5: ใช้ฟังก์ชัน get_security_configuration ส่ง security_name เป็น Name parameter
  6. ขั้นตอน 6: คืนค่าการตั้งค่าของ Security ที่ระบุ
  7. ขั้นตอน 7: จัดการข้อยกเว้นทั่วไป

ตัวอย่างโค้ด

import boto3
from botocore.exceptions import ClientError

def get_detail_security_configuration(security_name):
    session = boto3.session.Session()
    glue_client = session.client('glue')
    try:
        response = glue_client.get_security_configuration(Name=security_name)
        return response
    except ClientError as e:
        raise Exception("boto3 client error in get_detail_security_configuration: " + e.__str__())
    except Exception as e:
        raise Exception("Unexpected error in get_detail_security_configuration: " + e.__str__())

print(get_detail_security_configuration("job-security-settings"))

ผลลัพธ์

{'SecurityConfiguration': {'Name': 'job-security-settings', 'CreatedTimeStamp': datetime.datetime(2020, 9, 24, 1, 53, 21, 265000, tzinfo=tzlocal()), 'EncryptionConfiguration': {'S3Encryption': [{'S3EncryptionMode': 'SSE-KMS', 'KmsKeyArn': 'arn:aws:kms:us-east1:**************:key/************-bd27-f3ec3b590d0f'}]}}, 'ResponseMetadata': {'RequestId': 'b1***************-afd048ed7d07', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Mon, 01 Mar 2021 05:48:47 GMT', 'content-type': 'application/x-amz-json-1.1', 'contentlength': '417', 'connection': 'keep-alive', 'x-amzn-requestid': 'b1*******************-afd048ed7d07'}, 'RetryAttempts': 0}}