Skip to content

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
class 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.
  """

  def __init__(self, bucket):
    """Initialize the GCS filesystem access.

    Args:
      bucket: Name of the GCS bucket to operate on.
    """
    self.bucket = bucket
    self._client = gcs.Client()

  def _blob_stat(self, blob, base_path=None):
    """Convert a GCS blob into a filesystem directory entry.

    Args:
      blob: Blob to describe.
      base_path: Optional prefix the blob is listed under; when set the
        entry describes the path relative to it, and blobs belonging to
        other prefixes yield no entry.

    Returns:
      A DirEntry describing the blob -- as a directory when the blob is
      a shared prefix of others -- or None when the blob is outside the
      base path.
    """
    name = blob.name
    if base_path is not None:
      if not name.startswith(base_path):
        return
      name = name[len(base_path):]
      spos = name.find('/')
      if spos > 0:
        name = name[:spos]
        mode = st.S_IFDIR
        size, etag = 0, None
      else:
        mode = st.S_IFREG
        size, etag = blob.size, blob.etag

      path = base_path + name
    else:
      spos = name.rfind('/')
      if spos >= 0:
        name = name[spos + 1:]
      mode = st.S_IFREG
      path = blob.name
      size, etag = blob.size, blob.etag

    return fsb.DirEntry(name=name,
                        path=path,
                        etag=etag,
                        st_mode=mode,
                        st_size=size,
                        st_ctime=blob.time_created.timestamp(),
                        st_mtime=blob.updated.timestamp())

  def _norm_path(self, path):
    """Normalize a path into a blob name prefix.

    Args:
      path: Path to normalize, as a blob name or directory-like path.

    Returns:
      The blob name prefix to use for listing operations, with '/' and
      empty paths normalized to the empty prefix.
    """
    if path:
      if path == '/':
        path = ''
      else:
        path = path + '/' if not path.endswith('/') else path

    return path

  def listdir(self, 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.

    Args:
      path: Path to list; an empty or '/' path lists the bucket root.

    Yields:
      DirEntry objects sorted with directories first, by name.
    """
    npath = self._norm_path(path)

    dentries = dict()
    for blob in self._client.list_blobs(self.bucket, prefix=npath):
      if (dentry := self._blob_stat(blob, base_path=npath)) is not None:
        xdentry = dentries.get(dentry.name)
        if xdentry is not None:
          dentry = dentry._replace(st_ctime=min(dentry.st_ctime, xdentry.st_ctime),
                                   st_mtime=max(dentry.st_mtime, xdentry.st_mtime))

        dentries[dentry.name] = dentry

    sorted_dentries = sorted(dentries.items(), key=lambda x: (x[1].st_mode, x[0]))
    for name, dentry in sorted_dentries:
      yield dentry

  def open(self, path, mode='rb'):
    """Open a blob for reading or writing.

    Args:
      path: Blob path to open.
      mode: Mode to open the blob with ('rb' or 'wb').

    Returns:
      A file-like object backed by the blob.
    """
    bucket = self._client.bucket(self.bucket)
    blob = bucket.blob(path)

    return blob.open(mode)

  def upload(self, path, source):
    """Upload data to a blob.

    Args:
      path: Blob path to upload to.
      source: Iterable of chunks of data to upload.
    """
    bucket = self._client.bucket(self.bucket)
    blob = bucket.blob(path)

    with blob.open('wb') as fd:
      for data in source:
        fd.write(data)

  def download(self, path, chunk_size=32 * 1024**2):
    """Download a blob as a sequence of data chunks.

    Args:
      path: Blob path to download.
      chunk_size: Maximum size of each downloaded chunk.

    Yields:
      Chunks of the blob content.
    """
    bucket = self._client.bucket(self.bucket)
    blob = bucket.blob(path)

    with blob.open('rb') as fd:
      while True:
        data = fd.read(chunk_size)
        if data:
          yield data
        if chunk_size > len(data):
          break

  def pread(self, path, offset, size):
    """Read a byte range of a blob without downloading the whole object.

    Args:
      path: Blob path to read from.
      offset: Start offset of the range to read.
      size: Number of bytes to read.

    Returns:
      The bytes read from the blob at the requested range.
    """
    bucket = self._client.bucket(self.bucket)
    blob = bucket.blob(path)

    return blob.download_as_bytes(start=offset,
                                  end=offset + size - 1,
                                  raw_download=True)

  def exists(self, path):
    """Check whether a blob exists in the bucket.

    Args:
      path: Blob path to check.

    Returns:
      True if the blob exists, False otherwise.
    """
    bucket = self._client.bucket(self.bucket)
    blob = bucket.blob(path)

    return blob.exists()

  def stat(self, path):
    """Describe a blob or a directory-like path prefix.

    Args:
      path: Blob path to describe.

    Returns:
      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.
    """
    bucket = self._client.bucket(self.bucket)
    blob = bucket.get_blob(path)
    if blob is not None:
      return self._blob_stat(blob)

    ctime = mtime = None
    for de in self.listdir(path):
      if ctime is None or de.st_ctime < ctime:
        ctime = de.st_ctime
      if mtime is None or de.st_mtime > mtime:
        mtime = de.st_mtime

    if ctime is not None and mtime is not None:
      bpath = path.removesuffix('/')
      name = os.path.basename(bpath)

      return fsb.DirEntry(name=name,
                          path=bpath,
                          st_mode=st.S_IFDIR,
                          st_size=0,
                          st_ctime=ctime,
                          st_mtime=mtime)

  def remove(self, path):
    """Delete a blob from the bucket.

    Args:
      path: Blob path to remove.
    """
    bucket = self._client.bucket(self.bucket)
    blob = bucket.blob(path)
    blob.delete()

  def rename(self, src_path, dest_path):
    """Move a blob to a new path.

    Args:
      src_path: Blob path to move.
      dest_path: Destination blob path.
    """
    bucket = self._client.bucket(self.bucket)
    src_blob = bucket.blob(src_path)

    bucket.copy_blob(src_blob, bucket, dest_path)
    bucket.delete_blob(src_path)

  def rmtree(self, path, ignore_errors=None):
    """Delete all the blobs under a path prefix.

    Args:
      path: Path prefix whose blobs are to be removed.
      ignore_errors: Whether to suppress deletion errors; True suppresses
        them, False or None (the default) re-raises the first error.
    """
    npath = self._norm_path(path)

    for blob in self._client.list_blobs(self.bucket, prefix=npath):
      try:
        blob.delete()
      except Exception as ex:
        alog.debug(f'Failed to remove "{blob.name}" from "{self.bucket}": {ex}')
        if ignore_errors in (None, False):
          raise

  def copy(self, src_path, dest_path):
    """Copy a blob to a new path.

    Args:
      src_path: Blob path to copy.
      dest_path: Destination blob path.
    """
    bucket = self._client.bucket(self.bucket)
    src_blob = bucket.blob(src_path)

    bucket.copy_blob(src_blob, bucket, dest_path)

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
def listdir(self, 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.

  Args:
    path: Path to list; an empty or '/' path lists the bucket root.

  Yields:
    DirEntry objects sorted with directories first, by name.
  """
  npath = self._norm_path(path)

  dentries = dict()
  for blob in self._client.list_blobs(self.bucket, prefix=npath):
    if (dentry := self._blob_stat(blob, base_path=npath)) is not None:
      xdentry = dentries.get(dentry.name)
      if xdentry is not None:
        dentry = dentry._replace(st_ctime=min(dentry.st_ctime, xdentry.st_ctime),
                                 st_mtime=max(dentry.st_mtime, xdentry.st_mtime))

      dentries[dentry.name] = dentry

  sorted_dentries = sorted(dentries.items(), key=lambda x: (x[1].st_mode, x[0]))
  for name, dentry in sorted_dentries:
    yield dentry

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
def open(self, path, mode='rb'):
  """Open a blob for reading or writing.

  Args:
    path: Blob path to open.
    mode: Mode to open the blob with ('rb' or 'wb').

  Returns:
    A file-like object backed by the blob.
  """
  bucket = self._client.bucket(self.bucket)
  blob = bucket.blob(path)

  return blob.open(mode)

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
def upload(self, path, source):
  """Upload data to a blob.

  Args:
    path: Blob path to upload to.
    source: Iterable of chunks of data to upload.
  """
  bucket = self._client.bucket(self.bucket)
  blob = bucket.blob(path)

  with blob.open('wb') as fd:
    for data in source:
      fd.write(data)

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
def download(self, path, chunk_size=32 * 1024**2):
  """Download a blob as a sequence of data chunks.

  Args:
    path: Blob path to download.
    chunk_size: Maximum size of each downloaded chunk.

  Yields:
    Chunks of the blob content.
  """
  bucket = self._client.bucket(self.bucket)
  blob = bucket.blob(path)

  with blob.open('rb') as fd:
    while True:
      data = fd.read(chunk_size)
      if data:
        yield data
      if chunk_size > len(data):
        break

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
def pread(self, path, offset, size):
  """Read a byte range of a blob without downloading the whole object.

  Args:
    path: Blob path to read from.
    offset: Start offset of the range to read.
    size: Number of bytes to read.

  Returns:
    The bytes read from the blob at the requested range.
  """
  bucket = self._client.bucket(self.bucket)
  blob = bucket.blob(path)

  return blob.download_as_bytes(start=offset,
                                end=offset + size - 1,
                                raw_download=True)

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
def exists(self, path):
  """Check whether a blob exists in the bucket.

  Args:
    path: Blob path to check.

  Returns:
    True if the blob exists, False otherwise.
  """
  bucket = self._client.bucket(self.bucket)
  blob = bucket.blob(path)

  return blob.exists()

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
def stat(self, path):
  """Describe a blob or a directory-like path prefix.

  Args:
    path: Blob path to describe.

  Returns:
    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.
  """
  bucket = self._client.bucket(self.bucket)
  blob = bucket.get_blob(path)
  if blob is not None:
    return self._blob_stat(blob)

  ctime = mtime = None
  for de in self.listdir(path):
    if ctime is None or de.st_ctime < ctime:
      ctime = de.st_ctime
    if mtime is None or de.st_mtime > mtime:
      mtime = de.st_mtime

  if ctime is not None and mtime is not None:
    bpath = path.removesuffix('/')
    name = os.path.basename(bpath)

    return fsb.DirEntry(name=name,
                        path=bpath,
                        st_mode=st.S_IFDIR,
                        st_size=0,
                        st_ctime=ctime,
                        st_mtime=mtime)

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
def remove(self, path):
  """Delete a blob from the bucket.

  Args:
    path: Blob path to remove.
  """
  bucket = self._client.bucket(self.bucket)
  blob = bucket.blob(path)
  blob.delete()

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
def rename(self, src_path, dest_path):
  """Move a blob to a new path.

  Args:
    src_path: Blob path to move.
    dest_path: Destination blob path.
  """
  bucket = self._client.bucket(self.bucket)
  src_blob = bucket.blob(src_path)

  bucket.copy_blob(src_blob, bucket, dest_path)
  bucket.delete_blob(src_path)

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
def rmtree(self, path, ignore_errors=None):
  """Delete all the blobs under a path prefix.

  Args:
    path: Path prefix whose blobs are to be removed.
    ignore_errors: Whether to suppress deletion errors; True suppresses
      them, False or None (the default) re-raises the first error.
  """
  npath = self._norm_path(path)

  for blob in self._client.list_blobs(self.bucket, prefix=npath):
    try:
      blob.delete()
    except Exception as ex:
      alog.debug(f'Failed to remove "{blob.name}" from "{self.bucket}": {ex}')
      if ignore_errors in (None, False):
        raise

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
def copy(self, src_path, dest_path):
  """Copy a blob to a new path.

  Args:
    src_path: Blob path to copy.
    dest_path: Destination blob path.
  """
  bucket = self._client.bucket(self.bucket)
  src_blob = bucket.blob(src_path)

  bucket.copy_blob(src_blob, bucket, dest_path)