forked from hugolz/storage_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
78 lines (50 loc) · 2.39 KB
/
Copy pathexample.py
File metadata and controls
78 lines (50 loc) · 2.39 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import requests
import os
import sys
def upload(file_name: str):
samples_dir = os.path.join(os.path.dirname(__file__), 'samples')
path = os.path.join(samples_dir, file_name)
print(f"Uploading .{f'{path}'.replace(os.getcwd(), "")}")
with open(path, "r") as file:
response = requests.put(f"http://127.0.0.1:42070/{file_name}", data = file) # Assuming a server is running at that address
print(f"Upload status code: {response.status_code}")
print(f"Upload response text: {response.text}")
if response.status_code == 201: # Created
return response.text, None
else:
return (), "Upload failed"
def download(uuid: str, file_name: str):
out_dir = os.path.join(os.path.dirname(__file__), 'out')
print(f"Downloading {uuid}")
response = requests.get(f"http://127.0.0.1:42070/{uuid}") # Assuming a server is running at that address
print(f"Download status code: {response.status_code}")
if response.status_code !=200:
return
path = os.path.join(out_dir, file_name)
print(f"File received\nWriting to .{f'{path}'.replace(os.getcwd(), "")}")
with open(path, "wb") as file:
for chunk in response.iter_content(chunk_size=8192): # 8 KB chunks
file.write(chunk)
print("Done\n")
def delete(uuid: str):
print(f"Deleting {uuid}")
response = requests.delete(f"http://127.0.0.1:42070/{uuid}") # Assuming a server is running at that address
print(f"Delete status code: {response.status_code}\n")
def main():
file_name = "100mb.data"
if not os.path.exists(os.path.join(os.path.dirname(__file__), 'samples', file_name)):
print("Please generate the sample file before running the script (sh ./examples/generate_sample.sh)")
sys.exit(1)
print("If you care about speed, check the server's log, python is a bit slow 😅\n")
uuid, err = upload(file_name)
if err != None:
print(err)
sys.exit(1)
print() # New line for readability
download(uuid, file_name)
delete(uuid)
examples_path = os.path.join(os.path.dirname(__file__))
examples_path_short = f"{examples_path}".replace(os.getcwd(), "")
print(f"You can use `diff .{examples_path_short}/samples/{file_name} .{examples_path_short}/out/{file_name}`\nIf you see no output, it means that the files are identical")
if __name__ == "__main__":
main()