Spaces:
Sleeping
Sleeping
File size: 1,334 Bytes
64cb0aa b3f0530 2a33642 b3f0530 2a33642 64cb0aa 2a33642 b3f0530 64cb0aa 2a33642 64cb0aa b3f0530 64cb0aa b3f0530 64cb0aa b3f0530 64cb0aa b3f0530 64cb0aa b3f0530 64cb0aa b3f0530 64cb0aa b3f0530 64cb0aa b3f0530 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 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}") |