Skip to content

Commit

Permalink
Merge pull request #421 from hnousiainen/aws_socks5_support
Browse files Browse the repository at this point in the history
rohmu/s3: support connectivity through proxies

#421
  • Loading branch information
rikonen authored Apr 21, 2021
2 parents 0202a0b + d4b8519 commit 2d160a4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,8 @@ specific to the storage type.

Dictionary specifying proxy information. The dictionary must contain keys ``type``,
``host`` and ``port``. Type can be either ``socks5`` or ``http``. Optionally,
``user`` and ``pass`` can be specified for proxy authentication. Only Google
Cloud Storage driver support proxies at the moment.
``user`` and ``pass`` can be specified for proxy authentication. Only S3 and
Google Cloud Storage drivers support proxies at the moment.

The following object storage types are supported:

Expand Down
21 changes: 20 additions & 1 deletion pghoard/rohmu/object_storage/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import time

import botocore.client
import botocore.config
import botocore.exceptions
import botocore.session

Expand Down Expand Up @@ -43,16 +44,34 @@ def __init__(
is_secure=False,
is_verify_tls=False,
segment_size=MULTIPART_CHUNK_SIZE,
encrypted=False
encrypted=False,
proxy_info=None
):
super().__init__(prefix=prefix)
botocore_session = botocore.session.get_session()
self.bucket_name = bucket_name
self.location = ""
self.region = region
if not host or not port:
custom_config = {}
if proxy_info:
username = proxy_info.get("user")
password = proxy_info.get("pass")
if username and password:
auth = f"{username}:{password}@"
else:
auth = ""
host = proxy_info["host"]
port = proxy_info["port"]
if proxy_info.get("type") == "socks5":
schema = "socks5"
else:
schema = "http"
proxy_url = f"{schema}://{auth}{host}:{port}"
custom_config["proxies"] = {"https": proxy_url}
self.s3_client = botocore_session.create_client(
"s3",
config=botocore.config.Config(**custom_config),
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=region,
Expand Down

0 comments on commit 2d160a4

Please sign in to comment.