File system connectors.
pip install git+https://github.com/ai-forever/fsconnectors.git
or
git clone https://github.com/ai-forever/fsconnectors
cd fsconnectors/
pip install .
from fsconnectors import LocalConnector
lc = LocalConnector()
with lc.open('./file.txt', 'w') as f:
f.write('Hello world!')
entries = lc.listdir('.')
from fsconnectors import S3Connector
sc = S3Connector.from_yaml('path/to/config.yaml')
with sc.open('bucket/file.txt', 'w') as f:
f.write('Hello world!')
entries = sc.listdir('bucket')
Important
All async connectors should be used in async context manager with connect
method
import asyncio
from fsconnectors import AsyncLocalConnector, AsyncS3Connector
async def foo():
async with AsyncLocalConnector().connect() as lc:
async with AsyncS3Connector( # or use from_yaml method
endpoint_url='endpoint_url',
aws_access_key_id='aws_access_key_id',
aws_secret_access_key='aws_secret_access_key'
).connect() as sc:
async with lc.open('./file.txt', 'rb') as lf:
async with sc.open('bucket/file.txt', 'wb') as sf:
await sf.write(await lf.read())
asyncio.run(foo())
open(path, mode)
- open file- parameters:
path: str
- path to filemode: str
- open modemultipart: bool = False
- use multipart writer (only for S3 connectors)
- returns:
Union[ContextManager, AsyncContextManager]
- file-like object
- parameters:
mkdir(path)
- make directory- parameters:
path: str
- directory path
- parameters:
copy(src_path, dst_path, recursive)
- copy file or directory- parameters:
src_path: str
- source pathdst_path: str
- destination pathrecursive: bool = False
- recursive
- parameters:
move(src_path, dst_path, recursive)
- move file or directory- parameters:
src_path: str
- source pathdst_path: str
- destination pathrecursive: bool = False
- recursive
- parameters:
remove(path, recursive)
- delete file or directory- parameters:
path: str
- path to file or directoryrecursive: bool = False
- recursive
- parameters:
listdir(path, recursive)
- list directory content- parameters:
path: str
- directory pathrecursive: bool = False
- recursive
- returns:
List[str]
- list of directory contents
- parameters:
scandir(path, recursive)
- list directory content with metadata- parameters:
path: str
- directory pathrecursive: bool = False
- recursive
- returns:
List[FSEntry]
- list of directory contents with metadata
- parameters:
File system entry metadata
name: str
- entry namepath: str
- entry pathtype: Literal['file', 'dir']
- entry typesize: Optional[int] = None
- entry size in bytes (only for files)last_modified: Optional[datetime.datetime] = None
- last modified (only for files)
python -m fsconnectors upload [-h] --s3_path S3_PATH --local_path LOCAL_PATH --config_path CONFIG_PATH [--workers WORKERS] [--multipart]
optional arguments:
-h, --help show this help message and exit
--s3_path S3_PATH S3 folder path
--local_path LOCAL_PATH local folder path
--config_path CONFIG_PATH path to configuration file
--workers WORKERS max workers
--multipart use multipart upload
python -m fsconnectors download [-h] --s3_path S3_PATH --local_path LOCAL_PATH --config_path CONFIG_PATH [--workers WORKERS]
optional arguments:
-h, --help show this help message and exit
--s3_path S3_PATH S3 folder path
--local_path LOCAL_PATH local folder path
--config_path CONFIG_PATH path to configuration file
--workers WORKERS max workers