Skip to content

Commit 389fc07

Browse files
committed
fix put_data 400
1 parent 2922821 commit 389fc07

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

qiniu/services/storage/uploader.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
def put_data(
13-
up_token, key, data, params=None, mime_type='application/octet-stream', check_crc=False, progress_handler=None):
13+
up_token, key, data, params=None, mime_type='application/octet-stream', check_crc=False, progress_handler=None, fname=None):
1414
"""上传二进制流到七牛
1515
1616
Args:
@@ -27,7 +27,7 @@ def put_data(
2727
一个ResponseInfo对象
2828
"""
2929
crc = crc32(data) if check_crc else None
30-
return _form_put(up_token, key, data, params, mime_type, crc, progress_handler)
30+
return _form_put(up_token, key, data, params, mime_type, crc, progress_handler, fname)
3131

3232

3333
def put_file(up_token, key, file_path, params=None,
@@ -81,6 +81,8 @@ def _form_put(up_token, key, data, params, mime_type, crc, progress_handler=None
8181
# name = key if key else file_name
8282

8383
fname = file_name
84+
if not fname or not fname.strip():
85+
fname = 'file_name'
8486

8587
r, info = http._post_file(url, data=fields, files={'file': (fname, data, mime_type)})
8688
if r is None and info.need_retry():

test_qiniu.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,14 @@ def remove_temp_file(file):
6464
pass
6565

6666

67+
def is_travis():
68+
return os.environ['QINIU_TEST_ENV'] == 'travis'
69+
70+
6771
class UtilsTest(unittest.TestCase):
6872

6973
def test_urlsafe(self):
70-
a = '你好\x96'
74+
a = 'hello\x96'
7175
u = urlsafe_base64_encode(a)
7276
assert b(a) == urlsafe_base64_decode(u)
7377

@@ -268,13 +272,14 @@ class UploaderTestCase(unittest.TestCase):
268272
q = Auth(access_key, secret_key)
269273

270274
def test_put(self):
271-
key = 'a\\b\\c"你好'
275+
key = 'a\\b\\c"hello'
272276
data = 'hello bubby!'
273277
token = self.q.upload_token(bucket_name)
274278
ret, info = put_data(token, key, data)
275279
print(info)
276280
assert ret['key'] == key
277281

282+
def test_put_crc(self):
278283
key = ''
279284
data = 'hello bubby!'
280285
token = self.q.upload_token(bucket_name, key)
@@ -359,6 +364,39 @@ def test_hasRead_WithoutSeek_retry2(self):
359364
assert ret is None
360365
qiniu.set_default(default_zone=qiniu.config.zone0)
361366

367+
def test_putData_without_fname(self):
368+
if is_travis():
369+
return
370+
localfile = create_temp_file(30 * 1024 * 1024)
371+
key = 'test_putData_without_fname'
372+
with open(localfile, 'rb') as input_stream:
373+
token = self.q.upload_token(bucket_name)
374+
ret, info = put_data(token, key, input_stream)
375+
print(info)
376+
assert ret is not None
377+
378+
def test_putData_without_fname1(self):
379+
if is_travis():
380+
return
381+
localfile = create_temp_file(30 * 1024 * 1024)
382+
key = 'test_putData_without_fname1'
383+
with open(localfile, 'rb') as input_stream:
384+
token = self.q.upload_token(bucket_name)
385+
ret, info = put_data(token, key, input_stream, self.params, self.mime_type, False, None, "")
386+
print(info)
387+
assert ret is not None
388+
389+
def test_putData_without_fname2(self):
390+
if is_travis():
391+
return
392+
localfile = create_temp_file(30 * 1024 * 1024)
393+
key = 'test_putData_without_fname2'
394+
with open(localfile, 'rb') as input_stream:
395+
token = self.q.upload_token(bucket_name)
396+
ret, info = put_data(token, key, input_stream, self.params, self.mime_type, False, None, " ")
397+
print(info)
398+
assert ret is not None
399+
362400

363401
class ResumableUploaderTestCase(unittest.TestCase):
364402

0 commit comments

Comments
 (0)