gcs_fs¶
Google Cloud Storage FileSystem implementation.
This module provides GcsFs, a FileSystem backend for gs:// URLs delegating the operations to the top-level py_misc_utils.gcs_fs module. Reads go through the block-cache machinery (GCS supports pread) while writes are staged locally and uploaded on close.
CacheHandler
¶
Bases: Handler
Object-cache handler creating per-bucket GcsFs instances.
Source code in py_misc_utils/fs/gcs_fs.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
create()
¶
Create a new GcsFs for the bucket.
Returns:
| Type | Description |
|---|---|
|
A top-level GcsFs bound to the handler bucket. |
Source code in py_misc_utils/fs/gcs_fs.py
41 42 43 44 45 46 47 | |
GcsReader
¶
Reader fetching data from GCS, supporting both pread and full reads.
Source code in py_misc_utils/fs/gcs_fs.py
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 | |
tag(sres)
classmethod
¶
Build a cache tag from a GCS stat result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sres
|
Stat result of the object, as returned by GcsFs.stat(). |
required |
Returns:
| Type | Description |
|---|---|
|
The object ETag, or a tag built from size and modification time. |
Source code in py_misc_utils/fs/gcs_fs.py
66 67 68 69 70 71 72 73 74 75 76 | |
support_blocks()
¶
Return True since GCS supports pread operations.
Source code in py_misc_utils/fs/gcs_fs.py
78 79 80 | |
read_block(bpath, offset, size)
¶
Read data into a local cache file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bpath
|
Local path of the cache file to write the contents to. |
required | |
offset
|
Offset in the object where reading starts; WHOLE_OFFSET streams the whole object. |
required | |
size
|
Number of bytes to read. |
required |
Returns:
| Type | Description |
|---|---|
|
The number of bytes written into the cache file. |
Source code in py_misc_utils/fs/gcs_fs.py
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 | |
GcsFs
¶
Bases: FsBase
FileSystem implementation for gs:// URLs.
GcsFs instances are pooled per bucket through the object cache. Paths are relative to the bucket root, so the leading slash of the URL path is stripped before dispatching the operations to the underlying GcsFs.
The credentials are resolved by the google-cloud-storage machinery - from the GOOGLE_APPLICATION_CREDENTIALS environment variable or the environment default - and can be pointed at an emulator with the STORAGE_EMULATOR_HOST environment variable.
See py_misc_utils.fs for the shared backend contract.
Example
from py_misc_utils import cached_file as chf
from py_misc_utils.fs import gcs_fs as gf
iface = chf.CacheInterface('/tmp/gfs-cache')
fs = gf.GcsFs(cache_dir='/tmp/gfs-cache', cache_iface=iface)
with fs.open('gs://my-bucket/data/file.bin', mode='rb') as f:
header = f.read(512)
Source code in py_misc_utils/fs/gcs_fs.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | |
remove(url)
¶
Remove an object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
gs:// URL of the object to remove. |
required |
Source code in py_misc_utils/fs/gcs_fs.py
225 226 227 228 229 230 231 232 | |
rename(src_url, dest_url)
¶
Rename an object within the same bucket.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_url
|
gs:// URL of the source object. |
required | |
dest_url
|
gs:// URL of the destination object. |
required |
Source code in py_misc_utils/fs/gcs_fs.py
234 235 236 237 238 239 240 241 242 243 | |
mkdir(url, mode=None)
¶
No-op since GCS has no real directories.
Source code in py_misc_utils/fs/gcs_fs.py
245 246 | |
makedirs(url, mode=None, exist_ok=None)
¶
No-op since GCS has no real directories.
Source code in py_misc_utils/fs/gcs_fs.py
248 249 | |
rmdir(url)
¶
No-op since GCS has no real directories.
Source code in py_misc_utils/fs/gcs_fs.py
251 252 | |
rmtree(url, ignore_errors=None)
¶
Recursively remove all the objects under a URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
gs:// URL of the prefix to remove. |
required | |
ignore_errors
|
When True errors raised while removing the objects are logged instead of propagating. |
None
|
Source code in py_misc_utils/fs/gcs_fs.py
254 255 256 257 258 259 260 261 262 263 264 | |
stat(url)
¶
Stat an object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
gs:// URL to stat. |
required |
Returns:
| Type | Description |
|---|---|
|
The DirEntry of the object. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the object does not exist. |
Source code in py_misc_utils/fs/gcs_fs.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
list(url)
¶
List the entries under a URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
gs:// URL of the prefix to enumerate. |
required |
Returns:
| Type | Description |
|---|---|
|
A list of DirEntry objects for the objects under the prefix. |
Source code in py_misc_utils/fs/gcs_fs.py
287 288 289 290 291 292 293 294 295 296 297 298 | |
open(url, mode, **kwargs)
¶
Open a URL for reading or writing.
Read modes are served through the block-cache interface, while write modes download the existing contents when truncation is not requested and wrap the stream into a WritebackFile uploading on close.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
gs:// URL to open. |
required | |
mode
|
Open mode string. |
required | |
**kwargs
|
Extra keyword arguments forwarded to the cache interface. |
{}
|
Returns:
| Type | Description |
|---|---|
|
A file-like object, wrapped into a TextIOWrapper in text modes. |
Source code in py_misc_utils/fs/gcs_fs.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |
put_file(url, data_gen)
¶
Upload data chunks to a URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
Destination gs:// URL of the upload. |
required | |
data_gen
|
Iterable of bytes chunks to upload. |
required |
Source code in py_misc_utils/fs/gcs_fs.py
360 361 362 363 364 365 366 367 368 369 | |
get_file(url)
¶
Read the contents of a URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
gs:// URL to read. |
required |
Returns:
| Type | Description |
|---|---|
|
A generator yielding the file contents as bytes chunks. |
Source code in py_misc_utils/fs/gcs_fs.py
371 372 373 374 375 376 377 378 379 380 381 382 | |
as_local(url, **kwargs)
¶
Return a local file with the contents of a URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
gs:// URL of the file to access. |
required | |
**kwargs
|
Extra keyword arguments forwarded to the cache interface. |
{}
|
Returns:
| Type | Description |
|---|---|
|
The path of the local file, downloaded through the cache interface. |
Source code in py_misc_utils/fs/gcs_fs.py
384 385 386 387 388 389 390 391 392 393 394 395 396 397 | |
link(src_url, dest_url)
¶
Copy an object, since GCS has no hard links.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_url
|
gs:// URL of the source object. |
required | |
dest_url
|
gs:// URL of the destination object. |
required |
Source code in py_misc_utils/fs/gcs_fs.py
399 400 401 402 403 404 405 406 | |
symlink(src_url, dest_url)
¶
Copy an object, since GCS has no symbolic links.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_url
|
gs:// URL of the source object. |
required | |
dest_url
|
gs:// URL of the destination object. |
required |
Source code in py_misc_utils/fs/gcs_fs.py
408 409 410 411 412 413 414 415 | |