写入 S3 的消息时间

Message time to write to S3

使用 boto3 写入 S3(可能还有所有操作)大约需要 300 毫秒,即使对于 EC2 实例上的少量数据也是如此。

分析显示阅读时间占主导地位,例如,_SSLSocket.read() - 可能是在建立安全连接或只是为了服务器响应时。

有什么办法可以缩短手术时间吗?也许可以使用较低级别的 API 调用?

%prun -s time client.put_object(Bucket='MDtemp', Key='temporary2', Body=b'Hello')



   2197 function calls (2188 primitive calls) in 0.094 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.045    0.045    0.045    0.045 {built-in method select}
       26    0.045    0.002    0.045    0.002 {method 'read' of '_ssl._SSLSocket' objects}
        2    0.000    0.000    0.000    0.000 {method 'write' of '_ssl._SSLSocket' objects}
       26    0.000    0.000    0.045    0.002 socket.py:364(readinto)
        2    0.000    0.000    0.000    0.000 socket.py:200(makefile)
       26    0.000    0.000    0.045    0.002 ssl.py:737(recv_into)
       27    0.000    0.000    0.000    0.000 message.py:463(get)
        1    0.000    0.000    0.001    0.001 awsrequest.py:195(_handle_expect_response)
        2    0.000    0.000    0.000    0.000 {method 'readline' of '_io._IOBase' objects}
      8/6    0.000    0.000    0.001    0.000 hooks.py:175(_emit)
        2    0.000    0.000    0.000    0.000 socket.py:352(__init__)
      245    0.000    0.000    0.000    0.000 {method 'lower' of 'str' objects}
        1    0.000    0.000    0.092    0.092 connectionpool.py:317(_make_request)
        1    0.000    0.000    0.093    0.093 sessions.py:539(send)
       26    0.000    0.000    0.045    0.002 ssl.py:611(read)
        3    0.000    0.000    0.000    0.000 _collections_abc.py:575(update)
        8    0.000    0.000    0.045    0.006 {method 'readline' of '_io.BufferedReader' objects}
        2    0.000    0.000    0.000    0.000 feedparser.py:217(_parsegen)
      150    0.000    0.000    0.000    0.000 {built-in method isinstance}
        1    0.000    0.000    0.000    0.000 parsers.py:639(_parse_non_payload_attrs)
        8    0.000    0.000    0.000    0.000 client.py:1092(putheader)
        1    0.000    0.000    0.000    0.000 feedparser.py:470(_parse_headers)
        1    0.000    0.000    0.000    0.000 client.py:284(parse_headers)
        7    0.000    0.000    0.000    0.000 parse.py:286(urlparse)
       23    0.000    0.000    0.000    0.000 parse.py:98(_coerce_args)
       27    0.000    0.000    0.000    0.000 socket.py:405(readable)
        1    0.000    0.000    0.094    0.094 {built-in method exec}
       40    0.000    0.000    0.000    0.000 _policybase.py:269(_sanitize_header)
        1    0.000    0.000    0.046    0.046 awsrequest.py:146(_send_output)
        1    0.000    0.000    0.000    0.000 auth.py:532(canonical_standard_headers)
        1    0.000    0.000    0.045    0.045 client.py:347(_read_status)
        ...

您可以使用 BASH 完成此操作,您必须调用外部应用程序,例如 AWS 命令​​行界面 (CLI)。

访问:http://docs.aws.amazon.com/cli/latest/reference/s3/

您是否考虑过使用 s3cmd?它是linux的命令行工具,非常有效,但我从未测量过它的性能。

s3fs 现在支持 asynchronous operations 使用 aiobotocore 库。这会重用连接并启用并发性,因此等待时间可以分摊到许多调用上,从而大大降低平均延迟。如果尚未建立 SSL 连接,单次调用的总时间不会受到太大影响。