anthropics/blobfile
Python
Captured source
source ↗anthropics/blobfile
Language: Python
License: Unlicense
Stars: 11
Forks: 7
Open issues: 0
Created: 2023-12-14T06:42:51Z
Pushed: 2024-02-02T00:07:02Z
Default branch: master
Fork: no
Archived: no
README:
blobfile
This is a library that provides a Python-like interface for reading local and remote files (only from blob storage), with an API similar to open() as well as some of the os.path and shutil functions. blobfile supports local paths, Google Cloud Storage paths (gs://), and Azure Blob Storage paths (az:/// or https://.blob.core.windows.net//).
The main function is BlobFile, which lets you open local and remote files that act more or less like local ones. There are also a few additional functions such as basename, dirname, and join, which mostly do the same thing as their os.path namesakes, only they also support GCS paths and ABS paths.
This library is inspired by TensorFlow's `gfile` but does not have exactly the same interface.
Installation
pip install blobfile
Usage
# write a file, then read it back
import blobfile as bf
with bf.BlobFile("gs://my-bucket-name/cats", "wb") as f:
f.write(b"meow!")
print("exists:", bf.exists("gs://my-bucket-name/cats"))
with bf.BlobFile("gs://my-bucket-name/cats", "rb") as f:
print("contents:", f.read())There are also some [examples processing many blobs in parallel](docs/parallel_examples.md).
Here are the functions in blobfile:
BlobFile- likeopen()but works with remote paths too, data can be streamed to/from the remote file. It accepts the following arguments:streaming:- The default for
streamingisTruewhenmodeis in"r", "rb"andFalsewhenmodeis in"w", "wb", "a", "ab". streaming=True:- Reading is done without downloading the entire remote file.
- Writing is done to the remote file directly, but only in chunks of a few MB in size.
flush()will not cause an early write. - Appending is not implemented.
streaming=False:- Reading is done by downloading the remote file to a local file during the constructor.
- Writing is done by uploading the file on
close()or during destruction. - Appending is done by downloading the file during construction and uploading on
close(). buffer_size: number of bytes to buffer, this can potentially make reading more efficient.cache_dir: a directory in which to cache files for reading, only valid ifstreaming=Falseandmodeis in"r", "rb". You are reponsible for cleaning up the cache directory.file_size: size of the file being opened, can be specified directly to avoid checking the file size when opening the file. While this will avoid a network request, it also means that you may get an error when first reading a file that does not exist rather than when opening it. Only valid for modes "r" and "rb". This valid will be ignored for local files.
Some are inspired by existing os.path and shutil functions:
copy- copy a file from one path to another, this will do a remote copy between two remote paths on the same blob storage serviceexists- returnsTrueif the file or directory existsglob/scanglob- return files matching a glob-style pattern as a generator. Globs can have surprising performance characteristics when used with blob storage. Character ranges are not supported in patterns.isdir- returnsTrueif the path is a directorylistdir/scandir- list contents of a directory as a generatormakedirs- ensure that a directory and all parent directories existremove- remove a filermdir- remove an empty directoryrmtree- remove a directory treestat- get the size and modification time of a filewalk- walk a directory tree with a generator that yields(dirpath, dirnames, filenames)tuplesbasename- get the final component of a pathdirname- get the path except for the final componentjoin- join 2 or more paths together, inserting directory separators between each component
There are a few bonus functions:
get_url- returns a url for a path (usable by an HTTP client without any authentication) along with the expiration for that url (or None)md5- get the md5 hash for a path, for GCS this is often fast, but for other backends this may be slow. On Azure, if the md5 of a file is calculated and is missing from the file, the file will be updated with the calculated md5.set_mtime- set the modified timestamp for a fileconfigure- set global configuration options for blobfilelog_callback=default_log_fn: a log callback functionlog(msg: string)to use instead of printing to stdout. If you useparallel=True, you probably want to use a log callback function that is pickleable.connection_pool_max_size=32: the max size for each per-host connection poolmax_connection_pool_count=10: the maximum count of per-host connection poolsazure_write_chunk_size=8 * 2 ** 20: the size of blocks to write to Azure Storage blobs in bytes, can be set to a maximum of 100MB. This determines both the unit of request retries as well as the maximum file size, which is50,000 * azure_write_chunk_size.google_write_chunk_size=8 * 2 ** 20: the size of blocks to write to Google Cloud Storage blobs in bytes, this only determines the unit of request retries.retry_log_threshold=0: set a retry count threshold above which to log failures to the log callback functionretry_common_log_threshold=2: set a retry count threshold above which to log very common failures to the log callback functionconnect_timeout=10: the maximum amount of time (in seconds) to wait for a connection attempt to a server to succeed, set to None to wait foreverread_timeout=30: the maximum amount of time (in seconds) to wait between consecutive read operations for a response from the server, set to None to wait foreveroutput_az_paths=True: outputaz://paths instead of using thehttps://for azureuse_azure_storage_account_key_fallback=False: fallback to storage account keys for azure containers, having this enabled requires listing your subscriptions and may run into 429 errors if you hit the low azure quotas for subscription listingget_http_pool=None: a function that returns a…
Excerpt shown — open the source for the full document.