Spaces:
Sleeping
Sleeping
Update aws_logger.py
Browse files12234566789103 unmodified lines14151616171819192021212223232424252627262829303130313233343335363537import boto3import jsonimport osimport csvimport io
BUCKET_NAME = os.getenv("S3_BUCKET_NAME")FILE_NAME = "trend_log.json"FILE_NAME = "trend_log.csv"
s3 = boto3.client("s3")
3 unmodified lines return
try: # Get existing log # Try to download existing file try: obj = s3.get_object(Bucket=BUCKET_NAME, Key=FILE_NAME) existing_data = json.loads(obj["Body"].read().decode()) existing_content = obj['Body'].read().decode('utf-8') except s3.exceptions.NoSuchKey: existing_data = [] existing_content = "timestamp,price,trend\n"
# Append new entry existing_data.append(data) # Append new row to existing content new_row = f"{data['timestamp']},{data['price']},{data['trend']}\n" updated_content = existing_content + new_row
# Upload back # Upload updated CSV s3.put_object( Bucket=BUCKET_NAME, Key=FILE_NAME, Body=json.dumps(existing_data), ContentType="application/json" Body=updated_content.encode('utf-8'), ContentType='text/csv' ) print("Uploaded trend to S3") print("Uploaded CSV to S3") except Exception as e: print(f"Error uploading to S3: {e}") print(f"Error uploading CSV to S3: {e}")aws_logger.py+14-12