-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path09_binary_files.py
More file actions
49 lines (36 loc) · 1.21 KB
/
09_binary_files.py
File metadata and controls
49 lines (36 loc) · 1.21 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
#!/usr/bin/env python3
"""Binary file operations"""
import os
import sys
import random
import string
from koyeb import Sandbox
def main():
api_token = os.getenv("KOYEB_API_TOKEN")
if not api_token:
print("Error: KOYEB_API_TOKEN not set")
return 1
sandbox = None
suffix = "".join(random.choices(string.ascii_lowercase + string.digits, k=8))
try:
sandbox = Sandbox.create(
image="koyeb/sandbox",
name=f"binary-files-{suffix}",
wait_ready=True,
api_token=api_token,
)
fs = sandbox.filesystem
# Write binary data (encoding="base64" handles the encoding automatically)
binary_data = b"Binary data: \x00\x01\x02\x03\xff\xfe\xfd"
fs.write_file("/tmp/binary.bin", binary_data, encoding="base64")
# Read binary data (encoding="base64" decodes and returns bytes)
file_info = fs.read_file("/tmp/binary.bin", encoding="base64")
print(f"Original: {binary_data}")
print(f"Read back: {file_info.content}")
assert binary_data == file_info.content
return 0
finally:
if sandbox:
sandbox.delete()
if __name__ == "__main__":
sys.exit(main())