Package: blobfile
blobfileThis is a library that provides a Python-like interface for reading local and remote files (only from blob storage), with an API similar toopen()as well as some of theos.pathandshutilfunctions.blobfilesupports local paths, Google Cloud Storage paths (gs://<bucket>), and Azure Blob Storage paths (az://<account>/<container>orhttps://<account>.blob.core.windows.net/<container>/).The main function isBlobFile, which lets you open local and remote files that act more or less like local ones.  There are also a few additional functions such asbasename,dirname, andjoin, which mostly do the same thing as theiros.pathnamesakes, only they also support GCS paths and ABS paths.This library is inspired by TensorFlow'sgfilebut does not have exactly the same interface.InstallationpipinstallblobfileUsage# write a file, then read it backimportblobfileasbfwithbf.BlobFile("gs://my-bucket-name/cats","wb")asf:f.write(b"meow!")print("exists:",bf.exists("gs://my-bucket-name/cats"))withbf.BlobFile("gs://my-bucket-name/cats","rb")asf:print("contents:",f.read())There are also someexamples processing many blobs in parallel.Here are the functions inblobfile: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 forstreamingisTruewhenmodeis 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 onclose()or during destruction.Appending is done by downloading the file during construction and uploading onclose().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 existingos.pathandshutilfunctions: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 havesurprising performance characteristicswhen 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 componentThere 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 aurllib3.PoolManagerto be used for requestsuse_streaming_read=False: if set toTrue, use a single read per file instead of reading a chunk at a time (not recommended for azure)default_buffer_size=io.DEFAULT_BUFFER_SIZE: the default buffer size to use for reading files (and writing local files)save_access_token_to_disk=True: if set toTrueto save access tokens to disk so that other processes can read the access tokens to avoid the small amount of time it usually takes to get a token (if the token is still valid).multiprocessing_start_method="spawn": the start method to use when creating processes for parallel workcreate_context- (same arguments asconfigure), creates a new instance ofblobfilewith a custom configuration instead of modifying the global configurationAuthenticationGoogle Cloud StorageThe following methods will be tried in order:Check the environment variableGOOGLE_APPLICATION_CREDENTIALSfor a path to service account credentials in JSON format.Check for "application default credentials".  To setup application default credentials, rungcloud auth application-default login.Check for a GCE metadata server (if running on GCE) and get credentials from that service.Azure BlobsThe following methods will be tried in order:IfAZURE_USE_IDENTITY=1is set, useDefaultAzureCredentialfrom theazure-identitypackage to acquire tokens. Note: your application must install theazure-identitypackage;blobfiledoes not specify it as a required dependency.Check the environment variableAZURE_STORAGE_KEYfor an azure storage account key (these are per-storage account shared keys described inhttps://docs.microsoft.com/en-us/azure/storage/common/storage-account-keys-manage)Check the environment variableAZURE_APPLICATION_CREDENTIALSwhich should point to JSON credentials for a service principal output by the commandaz ad sp create-for-rbac --name <name>Check the environment variablesAZURE_CLIENT_ID,AZURE_CLIENT_SECRET,AZURE_TENANT_IDcorresponding to a service principal described in the previous step but without the JSON file.Check the environment variableAZURE_STORAGE_CONNECTION_STRINGfor anAzure Storage connection stringUse credentials from theazcommand line tool if they can be found.If access using credentials fails, anonymous access will be tried.blobfilesupports public access for containers marked as public, but not individual blobs.PathsFor Google Cloud Storage and Azure Blobs, directories don't really exist.  These storage systems store the files in a single flat list.  The "/" separators are just part of the filenames and there is no need to call the equivalent ofos.mkdiron one of these systems.To make local behavior consistent with the remote storage systems, missing local directories will be created automatically when opening a file in write mode.LocalThese are just normal paths for the current machine, e.g./root/hello.txtGoogle Cloud StorageGCS paths have the formatgs://<bucket>/<blob>, you cannot perform any operations ongs://itself.Azure BlobsAzure Blobs URLs have the formataz://<account>/<container>orhttps://<account>.blob.core.windows.net/<container>/<blob>.  The highest you can go up the hierarchy isaz://<account>/<container>/,blobfilecannot perform any operations onaz://<account>/.  Thehttps://url is the output format by default, but theaz://urls are accepted as inputs and you can setoutput_az_paths=Trueto getaz://urls as output.ErrorsError- base class for library-specific exceptionsRequestFailure(Error)- a request has failed permanently, the status code can be found in the propertyresponse_status:intand an error code, if available, is inerror:Optional[str].RestartableStreamingWriteFailure(RequestFailure)- a streaming write has failed permanently, which requires restarting from the beginning of the stream.ConcurrentWriteFailure(RequestFailure)- a write failed because another process was writing to the same file at the same time.VersionMismatch(RequestFailure)- a write failed because the remote file did not match the version specified by the user.The following generic exceptions are raised from some functions to make the behavior similar to the original versions:FileNotFoundError,FileExistsError,IsADirectoryError,NotADirectoryError,OSError,ValueError,io.UnsupportedOperationLoggingblobfilewill keep retrying transient errors until they succeed or a permanent error is encountered (which will raise an exception).  In order to make diagnosing stalls easier,blobfilewill log when retrying requests.To route those log lines, useconfigure(log_callback=<fn>)to set a callback function which will be called whenever a log line should be printed.  The default callback prints to stdout with the prefixblobfile:.Using theloggingmoduleIf you use the pythonloggingmodule, you can haveblobfilelog there:bf.configure(log_callback=logging.getLogger("blobfile").warning)Whileblobfiledoes not use the pythonloggingmodule by default, it does use other libraries which use that module.  So if you configure the pythonloggingmodule, you may need to change the settings to adjust logging behavior:urllib3:logging.getLogger("urllib3").setLevel(logging.ERROR)filelock:logging.getLogger("filelock").setLevel(logging.ERROR)Also, as a tip, make sure to use a format that tells you the name of the logger:logging.basicConfig(format="%(asctime)s[%(name)s]%(levelname)s:%(message)s",level=logging.WARNING)This will let you see which package is producing log messages.SafetyThe library should be thread safe and fork safe with the following exceptions:ABlobFileinstance is not thread safe (only 1 thread should own aBlobFileinstance at a time)Calls tobf.configure()are not thread-safe and should ideally happen before performing any operationsConcurrent WritersGoogle Cloud Storage supports multiple writers for the same blob and the last one to finish should win.  However, in the event of a large number of simultaneous writers, the service will return 429 or 503 errors and most writers will stall.  In this case, write to different blobs instead.Azure Blobs doesn't support multiple writers for the same blob.  With the wayBlobFileis currently configured, the last writer to start writing will win.  Other writers will get aConcurrentWriteFailure.  In addition, all writers could fail if the file size is large and there are enough concurrent writers.  In this case, you can write to a temporary blob (with a random filename), copy it to the final location, and then delete the original.  The copy will be within a container so it should be fast.ChangesSeeCHANGESContributingCreatetesting bucketsfor each cloud provider with appropriate credentials.To make a new release:Update CHANGES.mdUpdate blobfile/VERSIONrm -rf build distpython -m build .twine upload dist/*Tag the release on GithubTestingThis will auto-format the code, check the types, and then run the tests:pythontesting/run.pyRun a single test:pythontesting/run.py-v-s-ktest_windowed_fileModifytesting/run.pyif you only want to do some of these things.  The tests are rather slow, ~7 minutes to run (even though large file tests are disabled) and require accounts with every cloud provider.