Amazon S3 上传限速

版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc 引言 最近公司有一批数据要上传到白山云的对象存储中,白山云的对象存储兼容AWS S3的API 因此我使用的是boto3, 然而网络管理员要求对上行带宽有限制 解决方法 1. 使用Trickle 见参考资料1 2. 使用iptables 见参考资料2 3. 使用SDK 使用Trickle,要求上传程序只对使用Glibc库应用有效, 而使用iptables,配置还是比较麻烦的,而且iptables只能按照对端的IP进行限制,而我们访问对象存储,实际使用的是域名,如果域名解析的IP发生变化,我们无法及时感知。 仔细阅读代码和文档之后,我发现boto3原生已经支持限速了 max_bandwidth: The maximum bandwidth that will be consumed in uploading and downloading file content. The value is in terms of bytes per second. # -*- coding: utf-8 -*- import boto3 from boto3.s3.transfer import TransferConfig # from s3transfer.manager import TransferConfig access_key = "xxx" secret_key = "xxx" cli = boto3.client( 's3', aws_access_key_id=access_key, aws_secret_access_key=secret_key, endpoint_url='http://ss.bscstorage.com' ) config = TransferConfig( multipart_threshold=30 * 1024 * 1024, multipart_chunksize=8 * 1024 * 1024, max_concurrency=10, ) # 50MB/s # 单位是byte config.max_bandwidth = 50 * 1024 * 1024 resp = cli.upload_file( '/tmp/VSCode-darwin-stable.zip', 'test', 'test-key-xx2', ExtraArgs={ 'ContentType': 'text/plain', # 请替换为合适的文件类型 'ACL': 'private', }, Config=config ) 参考资料 Linux 下使用Trickle限制下载/上传带宽 linux下使用iptables和tc限制流量 请我喝瓶饮料

January 18, 2018 · 1 min