Skip to content

Commit

Permalink
Handle problem of s3 https.
Browse files Browse the repository at this point in the history
  • Loading branch information
cuihaikuo committed Mar 15, 2019
1 parent 87a2fd7 commit 4570958
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
32 changes: 18 additions & 14 deletions seafobj/backends/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
from boto.s3.key import Key

class S3Conf(object):
def __init__(self, key_id, key, bucket_name, host, port, use_v4_sig, aws_region):
def __init__(self, key_id, key, bucket_name, host, port, use_v4_sig, aws_region, use_https, path_style_request):
self.key_id = key_id
self.key = key
self.bucket_name = bucket_name
self.host = host
self.port = port
self.use_v4_sig = use_v4_sig
self.aws_region = aws_region
self.use_https = use_https
self.path_style_request = path_style_request

class SeafS3Client(object):
'''Wraps a s3 connection and a bucket'''
Expand All @@ -22,28 +24,30 @@ def __init__(self, conf):
self.bucket = None

def do_connect(self):
if self.conf.path_style_request:
calling_format=boto.s3.connection.OrdinaryCallingFormat()
else:
calling_format=boto.s3.connection.SubdomainCallingFormat()

if self.conf.host is None:
# If version 4 signature is used, boto requires 'host' parameter
# Also there is a bug in AWS Frankfurt that causes boto doesn't work.
# The current work around is to give specific service address, like
# s3.eu-central-1.amazonaws.com instead of s3.amazonaws.com.
if self.conf.use_v4_sig:
self.conn = boto.connect_s3(self.conf.key_id, self.conf.key,
host='s3.%s.amazonaws.com' % self.conf.aws_region)
host='s3.%s.amazonaws.com' % self.conf.aws_region,
is_secure=self.conf.use_https,
calling_format=calling_format)
else:
self.conn = boto.connect_s3(self.conf.key_id, self.conf.key)
self.conn = boto.connect_s3(self.conf.key_id, self.conf.key, is_secure=self.conf.use_https,
calling_format=calling_format)
else:
if self.conf.use_v4_sig:
self.conn = boto.connect_s3(self.conf.key_id, self.conf.key,
host='%s' % self.conf.host)
else:
self.conn = boto.connect_s3(
aws_access_key_id=self.conf.key_id,
aws_secret_access_key=self.conf.key,
port=self.conf.port,
host=self.conf.host,
is_secure=False,
calling_format=boto.s3.connection.OrdinaryCallingFormat())
self.conn = boto.connect_s3(self.conf.key_id, self.conf.key,
host='%s' % self.conf.host,
port=self.conf.port,
is_secure=self.conf.use_https,
calling_format=calling_format)

self.bucket = self.conn.get_bucket(self.conf.bucket_name)

Expand Down
20 changes: 18 additions & 2 deletions seafobj/objstore_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@ def get_s3_conf(cfg, section):
raise InvalidConfigError('aws_region is not configured')
aws_region = cfg.get(section, 'aws_region')

use_https = False
if cfg.has_option(section, 'use_https'):
use_https = cfg.getboolean(section, 'use_https')

path_style_request = False
if cfg.has_option(section, 'path_style_request'):
path_style_request = cfg.getboolean(section, 'path_style_request')

from seafobj.backends.s3 import S3Conf
conf = S3Conf(key_id, key, bucket, host, port, use_v4_sig, aws_region)
conf = S3Conf(key_id, key, bucket, host, port, use_v4_sig, aws_region, use_https, path_style_request)

return conf

Expand Down Expand Up @@ -79,8 +87,16 @@ def get_s3_conf_from_json(cfg):
raise InvalidConfigError('aws_region is not configured')
aws_region = cfg('aws_region')

use_https = False
if cfg.has_key('use_https'):
use_https = cfg['use_https'].lower() == 'true'

path_style_request = False
if cfg.has_key('path_style_request'):
path_style_request = cfg['path_style_request'].lower() == 'true'

from seafobj.backends.s3 import S3Conf
conf = S3Conf(key_id, key, bucket, host, port, use_v4_sig, aws_region)
conf = S3Conf(key_id, key, bucket, host, port, use_v4_sig, aws_region, use_https, path_style_request)

return conf

Expand Down

0 comments on commit 4570958

Please sign in to comment.