在 s3 中连接文件
项目描述
Python S3 连接
S3 Concat 用于将 s3 存储桶中的许多小文件连接成更少的大文件。
安装
pip install s3-concat
用法
命令行
$ s3-concat -h
进口
from s3_concat import S3Concat
bucket = 'YOUR_BUCKET_NAME'
path_to_concat = 'PATH_TO_FILES_TO_CONCAT'
concatenated_file = 'FILE_TO_SAVE_TO.json'
# Setting this to a size will always add a part number at the end of the file name
min_file_size = '50MB' # ex: FILE_TO_SAVE_TO-1.json, FILE_TO_SAVE_TO-2.json, ...
# Setting this to None will concat all files into a single file
# min_file_size = None ex: FILE_TO_SAVE_TO.json
# Init the job
job = S3Concat(bucket, concatenated_file, min_file_size,
content_type='application/json',
# session=boto3.session.Session(), # For custom aws session
# s3_client_kwargs={} # Use to pass arguments allowed by the s3 client: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html
)
# Add files, can call multiple times to add files from other directories
job.add_files(path_to_concat)
# Add a single file at a time
job.add_file('some/file_key.json')
# Only use small_parts_threads if you need to. See Advanced Usage section below.
job.concat(small_parts_threads=4)
高级用法
根据您的用例,您可能希望使用small_parts_threads
.
small_parts_threads
仅在您尝试连接的文件小于 5MB 时使用。由于 s3 multipart_upload api 的限制(请参阅下面的限制),任何小于 5MB 的文件都需要在本地下载,连接在一起,然后重新上传。通过设置此线程数,它将并行下载部分以更快地创建连接过程。
为这些参数设置的值取决于您的用例和运行它的系统。
限制
这使用 s3 的分段上传,其限制为https://docs.aws.amazon.com/AmazonS3/latest/dev/qfacts.html