समस्या का विवरण - एक S3 बकेट का स्वामित्व नियंत्रण विवरण प्राप्त करने के लिए Python में boto3 लाइब्रेरी का उपयोग करें।
उदाहरण के लिए, S3 में बकेट_1 का स्वामित्व नियंत्रण विवरण देखें।
इस समस्या को हल करने के लिए दृष्टिकोण/एल्गोरिदम
चरण 1 - अपवादों को संभालने के लिए boto3 और botocore अपवाद आयात करें।
चरण 2 - फ़ंक्शन में पैरामीटर के रूप में बकेट_नाम का उपयोग करें।
चरण 3 - boto3 लाइब्रेरी का उपयोग करके AWS सत्र बनाएं।
चरण 4 - S3 के लिए AWS क्लाइंट बनाएं।
चरण 5 - अब फ़ंक्शन get_bucket_ownership_controls का उपयोग करें और बकेट नाम पास करें।
चरण 6 - यह S3 के बारे में विवरण वाली डिक्शनरी लौटाता है।
चरण 7 - फ़ाइल को हटाते समय कुछ गलत होने पर सामान्य अपवाद को संभालें।
उदाहरण
बकेट का स्वामित्व विवरण प्राप्त करने के लिए निम्न कोड का उपयोग करें -
import boto3
from botocore.exceptions import ClientError
def get_bucket_ownership_control_of_s3(bucket_name):
session = boto3.session.Session()
s3_client = session.client('s3')
try:
result = s3_client.get_bucket_ownership_controls(Bucket=bucket_name,)
except ClientError as e:
raise Exception( "boto3 client error in get_bucket_ownership_control_of_s3: " + e.__str__())
except Exception as e:
raise Exception( "Unexpected error in get_bucket_ownership_control_of_s3: " + e.__str__())
return result
print(get_bucket_ownership_control_of_s3("Bucket_1")) आउटपुट
{
'OwnershipControls': {
'Rules': [
{
'ObjectOwnership': 'BucketOwnerPreferred'|'ObjectWriter'
},
]
}
}