Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix surplus http bug & add test #329

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions examples/upload_with_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
q = Auth(access_key, secret_key)

# 要上传的空间
bucket_name = 'bucket_name'
bucket_name = 'Bucket_Name'

# 上传到七牛后保存的文件名
key = 'a.jpg'
key = 'my-python-logo.png'

# 生成上传 Token,可以指定过期时间等
token = q.upload_token(bucket_name, key, 3600)
Expand All @@ -30,11 +30,12 @@
# 备用*.qiniu.com域名 不支持https上传
# 要求https上传时,如果客户指定的两个host都错误,且sdk自动查询的第一个*.qiniup.com上传域名因意外不可用导致访问到备用*.qiniu.com会报ssl错误
# 建议https上传时查看上面文档,指定正确的host
# 可以填 None 不可填空字符串

zone = Zone(
up_host='https://up.qiniup.com',
up_host_backup='https://upload.qiniup.com',
io_host='http://iovip.qbox.me',
up_host='http://upload.qiniup.com',
up_host_backup='http://up.qiniup.com',
io_host='https://iovip.qbox.me',
scheme='https')
set_default(default_zone=zone)

Expand Down
9 changes: 3 additions & 6 deletions qiniu/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,13 @@ def get_bucket_hosts(self, ak, bucket):
hosts[self.scheme].update({'io': []})

if self.up_host is not None:
hosts[self.scheme]['up'].append(self.scheme + "://" + self.up_host)
hosts[self.scheme]['up'].append(self.up_host)

if self.up_host_backup is not None:
hosts[self.scheme]['up'].append(
self.scheme + "://" + self.up_host_backup)
hosts[self.scheme]['up'].append(self.up_host_backup)

if self.io_host is not None:
hosts[self.scheme]['io'].append(self.scheme + "://" + self.io_host)
hosts[self.scheme]['io'].append(self.io_host)

if len(hosts[self.scheme]) == 0 or self.io_host is None:
hosts = compat.json.loads(self.bucket_hosts(ak, bucket))
Expand All @@ -113,7 +112,6 @@ def get_bucket_hosts(self, ak, bucket):
'ioHosts': scheme_hosts['io'],
'deadline': int(time.time()) + hosts['ttl']
}

self.set_bucket_hosts_to_cache(key, bucket_hosts)
return bucket_hosts

Expand All @@ -127,7 +125,6 @@ def get_bucket_hosts_to_cache(self, key):

if (self.host_cache[key]['deadline'] > time.time()):
ret = self.host_cache[key]

return ret

def set_bucket_hosts_to_cache(self, key, val):
Expand Down
8 changes: 2 additions & 6 deletions qiniu/services/storage/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,19 @@ def _form_put(up_token, key, data, params, mime_type, crc, progress_handler=None
fields['key'] = key

fields['token'] = up_token

if config.get_default('default_zone').up_host:
url = config.get_default('default_zone').up_host
else:
url = config.get_default('default_zone').get_up_host_by_token(up_token)
# name = key if key else file_name

fname = file_name
if not fname or not fname.strip():
fname = 'file_name'

# last modify time
if modify_time and keep_last_modified:
fields['x-qn-meta-!Last-Modified'] = rfc_from_timestamp(modify_time)

r, info = http._post_file(url, data=fields, files={'file': (fname, data, mime_type)})
if r is None and info.need_retry():
if info.connect_failed:
Expand Down Expand Up @@ -165,8 +164,7 @@ def __init__(self, up_token, key, input_stream, data_size, params, mime_type,
self.modify_time = modify_time or time.time()
self.file_name = file_name
self.keep_last_modified = keep_last_modified
# print(self.modify_time)
# print(modify_time)


def record_upload_progress(self, offset):
record_data = {
Expand All @@ -176,7 +174,6 @@ def record_upload_progress(self, offset):
}
if self.modify_time:
record_data['modify_time'] = self.modify_time
# print(record_data)
self.upload_progress_recorder.set_upload_record(self.file_name, self.key, record_data)

def recovery_from_record(self):
Expand Down Expand Up @@ -253,7 +250,6 @@ def file_url(self, host):
"x-qn-meta-!Last-Modified/{0}".format(urlsafe_base64_encode(rfc_from_timestamp(self.modify_time))))

url = '/'.join(url)
# print url
return url

def make_file(self, host):
Expand Down
32 changes: 32 additions & 0 deletions test_qiniu.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,38 @@ def test_putData_without_fname2(self):
print(info)
assert ret is not None

def test_put_zone(self):
zone = Zone(
up_host='http://upload.qiniup.com',
up_host_backup='http://up.qiniup.com',
io_host='https://iovip.qbox.me',
scheme='https')
set_default(default_zone=zone)
key = 'big'
token = self.q.upload_token(bucket_name, key)
localfile = create_temp_file(4 * 1024 * 1024 + 1)
progress_handler = lambda progress, total: progress
ret, info = put_file(token, key, localfile, self.params, self.mime_type, progress_handler=progress_handler)
print(info)
assert ret['key'] == key
remove_temp_file(localfile)

def test_put_zone_without_host(self):
zone = Zone(
up_host=None,
up_host_backup=None,
io_host=None,
scheme='https')
set_default(default_zone=zone)
key = 'big'
token = self.q.upload_token(bucket_name, key)
localfile = create_temp_file(4 * 1024 * 1024 + 1)
progress_handler = lambda progress, total: progress
ret, info = put_file(token, key, localfile, self.params, self.mime_type, progress_handler=progress_handler)
print(info)
assert ret['key'] == key
remove_temp_file(localfile)


class ResumableUploaderTestCase(unittest.TestCase):
mime_type = "text/plain"
Expand Down