समस्या विवरण - Python में boto3 लाइब्रेरी का उपयोग करके S3 बकेट का ओनरशिप कंट्रोल विवरण प्राप्त करें।
उदाहरण के लिए, S3 में Bucket_1 का ओनरशिप कंट्रोल विवरण प्राप्त करें।
इस समस्या को हल करने का दृष्टिकोण/एल्गोरिदम
चरण 1 - अपवादों को संभालने के लिए boto3 और botocore एक्सेप्शन्स आयात करें।
चरण 2 - फंक्शन में पैरामीटर के रूप में bucket_name का उपयोग करें।
चरण 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' } ] } }