crt_4.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. def create_s3_crt_client(
  2. region,
  3. botocore_credential_provider=None,
  4. num_threads=None,
  5. target_throughput=5 * GB / 8,
  6. part_size=8 * MB,
  7. use_ssl=True,
  8. verify=None,
  9. ):
  10. """
  11. :type region: str
  12. :param region: The region used for signing
  13. :type botocore_credential_provider:
  14. Optional[botocore.credentials.CredentialResolver]
  15. :param botocore_credential_provider: Provide credentials for CRT
  16. to sign the request if not set, the request will not be signed
  17. :type num_threads: Optional[int]
  18. :param num_threads: Number of worker threads generated. Default
  19. is the number of processors in the machine.
  20. :type target_throughput: Optional[int]
  21. :param target_throughput: Throughput target in Bytes.
  22. Default is 0.625 GB/s (which translates to 5 Gb/s).
  23. :type part_size: Optional[int]
  24. :param part_size: Size, in Bytes, of parts that files will be downloaded
  25. or uploaded in.
  26. :type use_ssl: boolean
  27. :param use_ssl: Whether or not to use SSL. By default, SSL is used.
  28. Note that not all services support non-ssl connections.
  29. :type verify: Optional[boolean/string]
  30. :param verify: Whether or not to verify SSL certificates.
  31. By default SSL certificates are verified. You can provide the
  32. following values:
  33. * False - do not validate SSL certificates. SSL will still be
  34. used (unless use_ssl is False), but SSL certificates
  35. will not be verified.
  36. * path/to/cert/bundle.pem - A filename of the CA cert bundle to
  37. use. Specify this argument if you want to use a custom CA cert
  38. bundle instead of the default one on your system.
  39. """
  40. event_loop_group = EventLoopGroup(num_threads)
  41. host_resolver = DefaultHostResolver(event_loop_group)
  42. bootstrap = ClientBootstrap(event_loop_group, host_resolver)
  43. provider = None
  44. tls_connection_options = None
  45. tls_mode = (
  46. S3RequestTlsMode.ENABLED if use_ssl else S3RequestTlsMode.DISABLED
  47. )
  48. if verify is not None:
  49. tls_ctx_options = TlsContextOptions()
  50. if verify:
  51. tls_ctx_options.override_default_trust_store_from_path(
  52. ca_filepath=verify
  53. )
  54. else:
  55. tls_ctx_options.verify_peer = False
  56. client_tls_option = ClientTlsContext(tls_ctx_options)
  57. tls_connection_options = client_tls_option.new_connection_options()
  58. if botocore_credential_provider:
  59. credentails_provider_adapter = CRTCredentialProviderAdapter(
  60. botocore_credential_provider
  61. )
  62. provider = AwsCredentialsProvider.new_delegate(
  63. credentails_provider_adapter
  64. )
  65. target_gbps = target_throughput * 8 / GB
  66. return S3Client(
  67. bootstrap=bootstrap,
  68. region=region,
  69. credential_provider=provider,
  70. part_size=part_size,
  71. tls_mode=tls_mode,
  72. tls_connection_options=tls_connection_options,
  73. throughput_target_gbps=target_gbps,
  74. )