Spaces:
Sleeping
Sleeping
Update aws_logger.py
Browse files1234567893 unmodified lines1314151617181920212223242526272829303132333435import boto3import jsonimport osBUCKET_NAME = os.getenv("S3_BUCKET_NAME")FILE_NAME = "trend_log.json"s3 = boto3.client("s3")3 unmodified linesreturntry:# Get existing logtry:obj = s3.get_object(Bucket=BUCKET_NAME, Key=FILE_NAME)existing_data = json.loads(obj["Body"].read().decode())except s3.exceptions.NoSuchKey:existing_data = []# Append new entryexisting_data.append(data)# Upload backs3.put_object(Bucket=BUCKET_NAME,Key=FILE_NAME,Body=json.dumps(existing_data),ContentType="application/json")print("Uploaded trend to S3")except Exception as e:print(f"Error uploading to S3: {e}")123456789103 unmodified lines141516171819202122232425262728293031323334353637import boto3import osimport csvimport ioBUCKET_NAME = os.getenv("S3_BUCKET_NAME")FILE_NAME = "trend_log.csv"s3 = boto3.client("s3")3 unmodified linesreturntry:# Try to download existing filetry: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 contentnew_row = f"{data['timestamp']},{data['price']},{data['trend']}\n"updated_content = existing_content + new_row# Upload updated CSVs3.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}")
aws_logger.py+14-12