Skip to main content

A Python library that synchronizes local files with Google Cloud Storage in a concurrency-aware manner.

Project description

bucket-locker

Concurrency-safe local copies of Google Cloud Storage (GCS) blobs in Python.

Why?

You need this library if:

  • You are using a GCS bucket.
  • Your system, app, or service downloads files from that bucket, works on local copies, and then uploads them back.

What it does

bucket-locker helps you:

  • Avoid data races
    • between accesses to the same local copy (e.g. multiple threads/tasks), and
    • between different local copies of the same blob (e.g. multiple processes/instances).
  • Avoid redundant I/O
    • skip downloads if your local copy is already in sync,
    • skip uploads if the blob hasn’t changed.

Installation

pip install bucket-locker

Usage

import asyncio
from pathlib import Path
from bucket_locker import Locker, BlobNotFound

async def main():
    locker = Locker(bucket_name="my-bucket", local_dir=Path("/tmp/bucket"))

    # Safe read–write access
    try:
        async with locker.owned_local_copy("data.json") as handle:
            # work on the local file
            data = handle.path.read_text()
            handle.path.write_text(data + "\nextra line")
            # changes will be uploaded automatically on exit
    except BlobNotFound:
        print("Blob not found in GCS")

    # Safe read-only access: does not lock the blob
    async with locker.readonly_local_copy("config.yaml") as handle:
        config = handle.path.read_text()
        print(config)

asyncio.run(main())

License

This project is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bucket_locker-1.0.6.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bucket_locker-1.0.6-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file bucket_locker-1.0.6.tar.gz.

File metadata

  • Download URL: bucket_locker-1.0.6.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for bucket_locker-1.0.6.tar.gz
Algorithm Hash digest
SHA256 f611ba9743d9fbc90f09317441f1310e8217b7c1f7788145c8ced5e05797c58a
MD5 29dd2f6802e76c9f5781b95230d82f46
BLAKE2b-256 0e6cb7e522201c2669e5dcfad0183f88cc39cf9f26c37edacd2d0aba1e5313fc

See more details on using hashes here.

File details

Details for the file bucket_locker-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: bucket_locker-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for bucket_locker-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 1b8c90a53b5bd4e6d6d84d9d884e215d3bd9aa9c29c710ff1f6114132fa3499d
MD5 75c40107ef64e79e4f8a6ec2efac6e1d
BLAKE2b-256 dc3fa4b61a8ef9b303ceeb15a0cde776b7f44c0a323083f5afa72b077737712a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page