Reading Amazon S3 bucket using access keys and secret keys in Python

python @ Freshers.in

To read an object from an Amazon S3 bucket using access keys and secret keys in Python, you can use the boto3 library, which is the official AWS SDK for Python. First, you’ll need to install boto3 if you haven’t already:

pip install boto3

Then, you can use the following sample code to read an object from S3:

import boto3

# AWS credentials
access_key = 'ASRIDKKW31DSADRFD'
secret_key = 'VMSDOEOWEFJSLDJTGECKKDFSERFDDSDFA'

# S3 bucket and object key
bucket_name = 'your_bucket_name'
object_key = 'your_object_key'

# Create an S3 client with the provided credentials
s3 = boto3.client('s3', aws_access_key_id=access_key, aws_secret_access_key=secret_key)

try:
    # Read the object from S3
    response = s3.get_object(Bucket=bucket_name, Key=object_key)
    
    # Print the content of the object
    content = response['Body'].read().decode('utf-8')
    print("Content of the object:")
    print(content)
except Exception as e:
    print("Error:", e)
Author: user