Spaces:
Sleeping

legion-ai / aws_logger.py
Loading404's picture
Update aws_logger.py
2a33642 verified
Raw History Blame Contribute Delete
1.33 kB
import boto3
import csv
import os
import io
from datetime import datetime
# Hardcoded AWS credentials
aws_access_key_id = "AKIA5L73LYIBZ3J73TTA"
aws_secret_access_key = "NfT3ijLJvjAgvggzMdKDWG9MCJScd0JcVO7qNxyl"
BUCKET_NAME = "your-s3-bucket-name" # You MUST replace this with your bucket name
FILE_NAME = "trend_log.csv"
s3 = boto3.client(
"s3",
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key
)
def upload_to_s3(data):
if not BUCKET_NAME:
print("S3 bucket name not set")
return
try:
# Try to download existing file
try:
obj = s3.get_object(Bucket=BUCKET_NAME, Key=FILE_NAME)
existing_content = obj['Body'].read().decode('utf-8')
except s3.exceptions.NoSuchKey:
existing_content = "timestamp,price,trend\n"
# Append new row to existing content
new_row = f"{data['timestamp']},{data['price']},{data['trend']}\n"
updated_content = existing_content + new_row
# Upload updated CSV
s3.put_object(
Bucket=BUCKET_NAME,
Key=FILE_NAME,
Body=updated_content.encode('utf-8'),
ContentType='text/csv'
)
print("Uploaded CSV to S3")
except Exception as e:
print(f"Error uploading CSV to S3: {e}")