समस्या का विवरण - S3 बकेट की लोकेशन जानने के लिए Python में boto3 लाइब्रेरी का इस्तेमाल करें। उदाहरण के लिए, S3 में बकेट_1 का स्थान खोजें
इस समस्या को हल करने के लिए दृष्टिकोण/एल्गोरिदम
चरण 1 - अपवादों को संभालने के लिए boto3 और botocore अपवाद आयात करें।
चरण 2 - फ़ंक्शन में पैरामीटर के रूप में बकेट_नाम का उपयोग करें।
चरण 3 - boto3 लाइब्रेरी का उपयोग करके AWS सत्र बनाएं।
चरण 4 - S3 के लिए AWS क्लाइंट बनाएं।
चरण 5 - अब फ़ंक्शन get_bucket_location_of_s3 का उपयोग करें और बकेट नाम पास करें।
चरण 6 - यह S3 के बारे में विवरण वाली डिक्शनरी लौटाता है।
चरण 7 - फ़ाइल को हटाते समय कुछ गलत होने पर सामान्य अपवाद को संभालें।
उदाहरण
बकेट लोकेशन प्राप्त करने के लिए निम्न कोड का उपयोग करें -
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")) आउटपुट
{
'LocationConstraint': 'us-west-2'|'us-west-1'|'us-east-2'|'us-east1',
'ResponseMetadata': {
'...': '...',
},
}