Forum Discussion
Connect AWS S3 with Power BI using Python
- 1 year ago
azeemsaifi
You’ll need to useboto3to connect to your S3 bucket.import boto3
import pandas as pd
from io import StringIO # or BytesIO for binary files like Excel# Set up AWS credentials (ideally, use IAM roles or environment variables)
aws_access_key = "YOUR_ACCESS_KEY"
aws_secret_key = "YOUR_SECRET_KEY"
bucket_name = "your-bucket-name"
file_key = "folder/yourfile.csv" # S3 object key (path)# Connect to S3
s3 = boto3.client('s3', aws_access_key_id=aws_access_key,
aws_secret_access_key=aws_secret_key)# Get the file object
response = s3.get_object(Bucket=bucket_name, Key=file_key)
data = response['Body'].read().decode('utf-8') # for CSV# Load to pandas
df = pd.read_csv(StringIO(data))
print(df.head())Did I answer your question? Mark my post as a solution! Appreciate your Kudos !!
Hi azeemsaifi,
I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.
Thank you.