Tuyên bố sự cố - Sử dụng thư viện boto3 trong Python để lấy vị trí của thùng S3. Ví dụ:tìm vị trí của Bucket_1 trong S3
Phương pháp tiếp cận / Thuật toán để giải quyết vấn đề này
Bước 1 - Nhập các ngoại lệ boto3 và botocore để xử lý các ngoại lệ.
Bước 2 - Sử dụng bucket_name làm tham số trong hàm.
Bước 3 - Tạo phiên AWS bằng thư viện boto3.
Bước 4 - Tạo ứng dụng AWS cho S3.
Bước 5 - Bây giờ sử dụng hàm get_bucket_location_of_s3 và chuyển tên nhóm.
Bước 6 - Nó trả về từ điển chứa thông tin chi tiết về S3.
Bước 7 - Xử lý ngoại lệ chung nếu có sự cố xảy ra khi xóa tệp.
Ví dụ
Sử dụng mã sau để lấy vị trí nhóm -
import boto3
from botocore.exceptions import ClientError
def get_bucket_location_of_s3(bucket_name):
session = boto3.session.Session()
s3_client = session.client('s3')
try:
result = s3_client.get_bucket_location(Bucket=bucket_name,)
except ClientError as e:
raise Exception( "boto3 client error in get_bucket_location_of_s3: " + e.__str__())
except Exception as e:
raise Exception( "Unexpected error in get_bucket_location_of_s3 function: " + e.__str__())
return result
print(get_bucket_location_of_s3("Bucket_1")) Đầu ra
{
'LocationConstraint': 'us-west-2'|'us-west-1'|'us-east-2'|'us-east1',
'ResponseMetadata': {
'...': '...',
},
}