Aries.storage: A Unified Storage Interface
Read and write files on Google Cloud Storage and Amazon S3 as if they are on local computer
The Aries storage sub-package provides a unified interface for accessing files and folders on local and cloud storage systems. The StorageFile class transform a file on cloud storage (e.g. Google Cloud Bucket) into a file-like object (stream). This enables us to read and write files on cloud storage like reading and writing on local disks. In addition, this package also includes high level APIs like copy, move and delete files and folders.
Motivation
As cloud platforms getting closer to our daily lives, file storage means more than just the hard drive on local computer. However, there is no standard cloud storage interface for reading and writing file on the cloud. The methods depends on the APIs provided by different providers. Also, reading and writing files on the cloud are so different from reading and writing files on the local computer. We have to treat them differently in the code. This package solves the problem by providing a unified way to access local and cloud storage. The IO interface is also designed to be the same as the way we access files on local computer. With this package, the modification on existing code to support cloud storage can be reduced significantly.
Implementation
Data access is provided through three classes: Aries.storage.StorageFile, Aries.storage.StorageFolder and Aries.storage.StoragePrefix. Each of them wraps an underlying "raw (or raw_io)" class, which contains platform dependent implementation. The Uniform Resource Identifier (URI)), e.g. file:///var/text.txt or gs://bucket_name/text.txt, is used to locate a file or folder. StorageFile and StorageFolder determine the underlying "raw" class automatically based on the scheme from the URI.
Currently, the following schemes are implemented:
- Local computer (
file://) - Google Cloud Storage (
gs://) - Amazon S3 Storage (
s3://)
The StorageFile Class
A StorageFile object can be initialized by
from Aries.storage import StorageFile
# uri: the Uniform Resource Identifier for a file
# local file path can also be used as uri.
uri = "/path/to/file.txt"
f = StorageFile(uri)
StorageFile() automatically determines the storage type by the scheme in the URI. For local file, URI can also be /var/text.txt without the scheme.
With a StorageFile, you can:
- Get the file size:
StorageFile("path/to/file").size - Get the md5 hex:
StorageFile("path/to/file").md5_hex - Get the last update time:
StorageFile("path/to/file").updated_time - Check if the file exist:
StorageFile("path/to/file").exist() - Create an empty file:
StorageFile("path/to/file").create() - Copy the file to another location:
StorageFile("path/to/file").copy("gs://path/to/destination") - Move the file to another location:
StorageFile("path/to/file").move("gs://path/to/destination") - Read the file (as bytes) into memory:
StorageFile("path/to/file").read() - Delete the file:
StorageFile("path/to/file").delete()
StorageFile is a file-like object implementing the I/O stream interface with BufferedIOBase and TextIOBase. The static StorageFile.init(uri, mode) method is designed to replace the built-in open() method.
However, initializing the StorageFile does NOT open the file. The StorageFile object provides open() and close() methods for opening and closing the file for read/write. The open() method returns the StorageFile instance itself.
Here is an example of using StorageFile with pandas:
from Aries.storage import StorageFile
import pandas as pd
df = pd.DataFrame([1, 3, 5])
uri = "gs://bucket_name/path/to/file.txt"
# Using StorageFile in pandas
f = StorageFile(uri).open('w'):
# f will be a file-like object
df.to_csv(f)
f.close()
The StorageFile.init() static method provides a shortcut for initializing and opening the file. This is designed to replace the built-in python open() method. The init() method returns a StorageFile instance. StorageFile also support context manager to open and close the file:
from Aries.storage import StorageFile
import pandas as pd
df = pd.DataFrame([1, 3, 5])
uri = "gs://bucket_name/path/to/file.txt"
# Using StorageFile in pandas
with StorageFile.init(uri, 'w') as f:
# f will be a file-like object
df.to_csv(f)
Once the file is opened, it can be used as a file-like object. The data can be accessed through methods like read() and write(). However, for Cloud Storage, the StorageFile might not have a fileno or file descriptor. In that case, it cannot be used when fileno is needed.
The init() and open() methods supports the same arguments as the Python built-in open() function. However, at this time, only the mode argument is used when opening cloud storage files.
High-Level APIs
The StorageFile class also supports high-level operations, including:
copy(), for copying the file to another location, e.g.StorageFile('/path/to/file.txt').copy('gs://bucket_name/path/to/file.txt')move(), for moving the file, e.g.StorageFile('/path/to/file.txt').move('s3://bucket_name/path/to/file.txt')delete(), for deleting the file, e.g.StorageFile('/path/to/file.txt').delete().
The copy() and move() methods also support cross-platform operations. For example:
# Move a file from local computer to Google cloud storage.
StorageFile('/path/to/file.txt').move('gs://bucket_name/path/to/file.txt')
The StorageFolder Class
The StorageFolder class provides the same high level APIs as the StorageFile class, as well as shortcuts for listing the files in a folder.
Metadata
Release files for Aries-storage 0.1.330
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| Aries-storage-0.1.330.tar.gz | 23.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| Aries_storage-0.1.330-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 52.0 kB
Release files / Aries-storage-0.1.330.tar.gz
| Download URL | Aries-storage-0.1.330.tar.gz |
|---|---|
| Size | 23.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4134e68ffdcb0ab837df31d583a0153ec83f14cc8f9304c14e8254867a029c5a
|
|
BLAKE2b-256 checksum How to use checksums |
6836399e7d168cdeccc5852ee67eea6b0c4da57b9d5f41714f16e1eb1a322b02
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
|
Release files / Aries_storage-0.1.330-py3-none-any.whl
| Download URL | Aries_storage-0.1.330-py3-none-any.whl |
|---|---|
| Size | 28.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
42f6dd4bac812f516255214089157ef711b5e074bff6a4bf5ae812409c19191e
|
|
BLAKE2b-256 checksum How to use checksums |
7cea2fbfcfb3e4c4bbb1ec47f36ea4878609f9a1084603516fb77cae2d6ff469
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
|
Security for Aries-storage 0.1.330
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.