-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyWrite.py
More file actions
57 lines (44 loc) · 1.32 KB
/
MyWrite.py
File metadata and controls
57 lines (44 loc) · 1.32 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
import boto3
import os
from dotenv import load_dotenv
import datetime
load_dotenv()
ACCESS_KEY = os.getenv('ACCESS_KEY')
SECRET_KEY = os.getenv('SECRET_KEY')
CLUSTER_URL = os.getenv('CLUSTER_URL')
BUCKET_LABEL = os.getenv('BUCKET_LABEL')
linode_obj_config = {
"aws_access_key_id": ACCESS_KEY,
"aws_secret_access_key": SECRET_KEY,
"endpoint_url":CLUSTER_URL,
}
def write(data):
cwd = ""
path = os.path.realpath(__file__)
path = path.split("/")
for f in range(len(path)-1):
cwd+="/"
cwd+=path[f]
cwd+="/"
try:
client = boto3.client("s3", **linode_obj_config)
ct = datetime.datetime.now()
# ts store timestamp of current time
ts = ct.timestamp()
stamp = round(ts)
filename = "daily_" + str(stamp) + ".txt"
#print(filename)
f = open(cwd + "/data/" + filename, "x")
f.write(str(data).replace("'",'"'))
f.close()
# print(client.list_objects(
# Bucket=BUCKET_LABEL,)["Contents"])
# client.put_object()
client.upload_file(
Filename=cwd + "/data/" +filename,
Bucket=BUCKET_LABEL,
Key=filename,
ExtraArgs={'ACL':'public-read'})
except Exception as error:
print(error)
print("Failed to write file to remote bucket.")