gcs_fs¶
Google Cloud Storage bucket filesystem.
This module provides the GcsFs class, a filesystem-like interface to the objects of a Google Cloud Storage bucket: listing, streaming and partial reads, uploads, copies and removals are exposed over plain paths.
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.
Example
from py_misc_utils import gcs_fs
fs = gcs_fs.GcsFs('my-bucket')
# Upload and stream back.
fs.upload('data/file.bin', [b'chunk1', b'chunk2'])
data = b''.join(fs.download('data/file.bin'))
# Partial read without downloading the whole object.
head = fs.pread('data/file.bin', 0, 512)
# Flat listing with directory collapsing.
for dentry in fs.listdir('data'):
print(dentry.name, dentry.st_size)
GcsFs
¶
Filesystem-like access to a Google Cloud Storage bucket.
The class provides listing, streaming read/write, partial reads and object management operations over the objects of a bucket, exposing them as directory entries and file paths.
See py_misc_utils.fs.gcs_fs for the gs:// URL backend built on top of this class, and py_misc_utils.fs for the shared backend contract.
Source code in py_misc_utils/gcs_fs.py
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 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 | |
listdir(path)
¶
List the entries of a path in the bucket.
Blobs sharing a name prefix are collapsed into a single directory entry holding the aggregate creation and modification times.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path to list; an empty or '/' path lists the bucket root. |
required |
Yields:
| Type | Description |
|---|---|
|
DirEntry objects sorted with directories first, by name. |
Source code in py_misc_utils/gcs_fs.py
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 | |
open(path, mode='rb')
¶
Open a blob for reading or writing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Blob path to open. |
required | |
mode
|
Mode to open the blob with ('rb' or 'wb'). |
'rb'
|
Returns:
| Type | Description |
|---|---|
|
A file-like object backed by the blob. |
Source code in py_misc_utils/gcs_fs.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
upload(path, source)
¶
Upload data to a blob.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Blob path to upload to. |
required | |
source
|
Iterable of chunks of data to upload. |
required |
Source code in py_misc_utils/gcs_fs.py
168 169 170 171 172 173 174 175 176 177 178 179 180 | |
download(path, chunk_size=32 * 1024 ** 2)
¶
Download a blob as a sequence of data chunks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Blob path to download. |
required | |
chunk_size
|
Maximum size of each downloaded chunk. |
32 * 1024 ** 2
|
Yields:
| Type | Description |
|---|---|
|
Chunks of the blob content. |
Source code in py_misc_utils/gcs_fs.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
pread(path, offset, size)
¶
Read a byte range of a blob without downloading the whole object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Blob path to read from. |
required | |
offset
|
Start offset of the range to read. |
required | |
size
|
Number of bytes to read. |
required |
Returns:
| Type | Description |
|---|---|
|
The bytes read from the blob at the requested range. |
Source code in py_misc_utils/gcs_fs.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
exists(path)
¶
Check whether a blob exists in the bucket.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Blob path to check. |
required |
Returns:
| Type | Description |
|---|---|
|
True if the blob exists, False otherwise. |
Source code in py_misc_utils/gcs_fs.py
221 222 223 224 225 226 227 228 229 230 231 232 233 | |
stat(path)
¶
Describe a blob or a directory-like path prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Blob path to describe. |
required |
Returns:
| Type | Description |
|---|---|
|
A DirEntry describing the blob, or -- when the path does not map |
|
|
to a single blob but matches a group of shared-prefix blobs -- a |
|
|
directory entry with aggregate times, or None when the path does |
|
|
not exist. |
Source code in py_misc_utils/gcs_fs.py
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 | |
remove(path)
¶
Delete a blob from the bucket.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Blob path to remove. |
required |
Source code in py_misc_utils/gcs_fs.py
270 271 272 273 274 275 276 277 278 | |
rename(src_path, dest_path)
¶
Move a blob to a new path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_path
|
Blob path to move. |
required | |
dest_path
|
Destination blob path. |
required |
Source code in py_misc_utils/gcs_fs.py
280 281 282 283 284 285 286 287 288 289 290 291 | |
rmtree(path, ignore_errors=None)
¶
Delete all the blobs under a path prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path prefix whose blobs are to be removed. |
required | |
ignore_errors
|
Whether to suppress deletion errors; True suppresses them, False or None (the default) re-raises the first error. |
None
|
Source code in py_misc_utils/gcs_fs.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | |
copy(src_path, dest_path)
¶
Copy a blob to a new path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_path
|
Blob path to copy. |
required | |
dest_path
|
Destination blob path. |
required |
Source code in py_misc_utils/gcs_fs.py
311 312 313 314 315 316 317 318 319 320 321 | |