-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_usage.py
More file actions
130 lines (100 loc) · 4.01 KB
/
basic_usage.py
File metadata and controls
130 lines (100 loc) · 4.01 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
"""Basic usage example for OnedataRESTFSSpec."""
import fsspec
import os
from pathlib import Path
def main():
"""Demonstrate basic OnedataRESTFSSpec usage."""
# Configuration - replace with your actual values
onezone_host = os.environ.get('ONEDATA_ONEZONE_HOST', 'https://datahub.egi.eu')
token = os.environ.get('ONEDATA_TOKEN')
if not token:
print("Error: ONEDATA_TOKEN environment variable must be set")
print("Example: export ONEDATA_TOKEN='your_access_token_here'")
return
# Create filesystem instance
print(f"Connecting to Onedata at {onezone_host}...")
fs = fsspec.filesystem(
'onedata',
onezone_host=onezone_host,
token=token
)
try:
# List available spaces
print("\n=== Available Spaces ===")
spaces = fs.ls('/')
for space in spaces:
print(f" {space}")
if not spaces:
print(" No spaces found")
return
# Use the first available space for examples
example_space = spaces[0]
print(f"\nUsing space '{example_space}' for examples...")
# List files in the space
print(f"\n=== Files in space '{example_space}' ===")
try:
files = fs.ls(f'/{example_space}/', detail=True)
for file_info in files[:10]: # Show first 10 files
file_type = "DIR" if file_info['type'] == 'directory' else "FILE"
size = file_info.get('size', 0)
name = Path(file_info['name']).name
print(f" {file_type:4} {size:>10} {name}")
if len(files) > 10:
print(f" ... and {len(files) - 10} more files")
except Exception as e:
print(f" Error listing files: {e}")
# Example: Create a test file
test_file_path = f'/{example_space}/fsspec_test_file.txt'
test_content = "Hello from OnedataRESTFSSpec!\nThis is a test file.\n"
print(f"\n=== Writing test file ===")
try:
with fs.open(test_file_path, 'w') as f:
f.write(test_content)
print(f" Created: {test_file_path}")
# Read it back
with fs.open(test_file_path, 'r') as f:
read_content = f.read()
print(f" Content: {repr(read_content[:50])}...")
# Get file info
info = fs.info(test_file_path)
print(f" Size: {info['size']} bytes")
print(f" Type: {info['type']}")
except Exception as e:
print(f" Error with test file: {e}")
# Example: Check file operations
print(f"\n=== File Operations ===")
try:
exists = fs.exists(test_file_path)
print(f" File exists: {exists}")
is_file = fs.isfile(test_file_path)
print(f" Is file: {is_file}")
size = fs.size(test_file_path) if exists else 0
print(f" File size: {size} bytes")
except Exception as e:
print(f" Error checking file operations: {e}")
# Example: Directory operations
test_dir_path = f'/{example_space}/fsspec_test_dir'
print(f"\n=== Directory Operations ===")
try:
fs.makedirs(test_dir_path, exist_ok=True)
print(f" Created directory: {test_dir_path}")
is_dir = fs.isdir(test_dir_path)
print(f" Is directory: {is_dir}")
except Exception as e:
print(f" Error with directory operations: {e}")
# Cleanup (optional)
print(f"\n=== Cleanup ===")
try:
if fs.exists(test_file_path):
fs.rm(test_file_path)
print(f" Removed: {test_file_path}")
if fs.exists(test_dir_path):
fs.rmdir(test_dir_path)
print(f" Removed: {test_dir_path}")
except Exception as e:
print(f" Error during cleanup: {e}")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()