跨兼容API,用于访问Posix和OBS存储系统
项目描述
stor 提供了一个跨兼容的CLI和Python API,用于访问块和对象存储。 stor 是为了创建一个代码片段,以便您可以在本地或远程文件上工作,而无需编写专门的代码来处理故障模式、重试或暂时不可用。功能API(即 stor.copytree、stor.rmtree、stor.remove、stor.listdir)将在所有存储后端上以相同的语义工作。这使得您可以在本地开发/测试代码时使用文件,并在推送到远程时利用强大的且成本更低的对象存储。
查看stor的完整文档https://counsyl.github.io/stor/。
快速入门
pip install stor
stor 提供了一个CLI和一个Python库,用于使用单个、跨兼容的API操作Posix和OBS。
快速入门 - CLI
usage: stor [-h] [-c CONFIG_FILE] [--version] {list,ls,cp,rm,walkfiles,cat,cd,pwd,clear,url,convert-swiftstack} ... A command line interface for stor. positional arguments: {list,ls,cp,rm,walkfiles,cat,cd,pwd,clear,url,convert-swiftstack} list List contents using the path as a prefix. ls List path as a directory. cp Copy a source to a destination path. rm Remove file at a path. walkfiles List all files under a path that match an optional pattern. cat Output file contents to stdout. cd Change directory to a given OBS path. pwd Get the present working directory of a service or all current directories. clear Clear current directories of a specified service. url generate URI for path convert-swiftstack convert swiftstack paths optional arguments: -h, --help show this help message and exit -c CONFIG_FILE, --config CONFIG_FILE File containing configuration settings. --version Print version
您可以列出本地和远程目录
›› stor ls s3://stor-test-bucket s3://stor-test-bucket/b.txt s3://stor-test-bucket/counsyl-storage-utils s3://stor-test-bucket/file_test.txt s3://stor-test-bucket/counsyl-storage-utils/ s3://stor-test-bucket/empty/ s3://stor-test-bucket/lots_of_files/ s3://stor-test-bucket/small_test/
复制本地或远程文件或从stdin上传
›› echo "HELLO WORLD" | stor cp - swift://AUTH_stor_test/hello_world.txt starting upload of 1 objects upload complete - 1/1 0:00:00 0.00 MB 0.00 MB/s ›› stor cat swift://AUTH_stor_test/hello_world.txt HELLO WORLD ›› stor cp swift://AUTH_stor_test/hello_world.txt hello_world.txt ›› stor cat hello_world.txt HELLO WORLD
快速入门 - Python
列出目录中的文件,利用分隔符
>>> stor.listdir('s3://bestbucket')
[S3Path('s3://bestbucket/a/')
S3Path('s3://bestbucket/b/')]
列出存储桶中的所有对象
>>> stor.list('s3://bestbucket')
[S3Path('s3://bestbucket/a/1.txt')
S3Path('s3://bestbucket/a/2.txt')
S3Path('s3://bestbucket/a/3.txt')
S3Path('s3://bestbucket/b/1.txt')]
或在本地路径中
>>> stor.list('stor')
[PosixPath('stor/__init__.py'),
PosixPath('stor/exceptions.pyc'),
PosixPath('stor/tests/test_s3.py'),
PosixPath('stor/tests/test_swift.py'),
PosixPath('stor/tests/test_integration_swift.py'),
PosixPath('stor/tests/test_utils.py'),
PosixPath('stor/posix.pyc'),
PosixPath('stor/base.py'),
使用python文件对象从POSIX或OBS读取和写入文件
import stor
with stor.open('/my/exciting.json') as fp:
data1 = json.load(fp)
data1['read'] = True
with stor.open('s3://bestbucket/exciting.json') as fp:
json.dump(data1, fp)
测试使用stor的代码
“stor”的关键设计考虑因素是,您的代码应该能够透明地使用POSIX或任何对象存储系统来读取和更新文件。因此,我们建议您将测试代码结构化为指向本地文件系统路径,并仅使用功能API。例如,在您的生产设置中,您可以设置DATADIR = 's3://bestbucketever',在测试时,您可以使用DATADIR = '/somewhat/cool/path/to/test/data',而您的实际代码只需说
with stor.open(stor.join(DATADIR, experiment)) as fp:
data = json.load(fp)
简单!无需任何模拟!
运行测试
make test
贡献和语义版本化
我们使用语义版本化来传达我们对库进行API更改的情况。有关向“stor”贡献的更多详细信息,请参阅CONTRIBUTING.md。
项目详情
下载文件
下载您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。