Skip to content

Commit

Permalink
Merge pull request #92 from dt3310321/s3
Browse files Browse the repository at this point in the history
S3
  • Loading branch information
dt3310321 authored Jul 25, 2019
2 parents 28f145e + b0f0eb2 commit 78fbf23
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
sudo: false
language: python
python:
- '2.6'
- '2.7'
- '3.5'
- '3.6'
Expand Down
12 changes: 7 additions & 5 deletions qcloud_cos/cos_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ def send_request(self, method, url, bucket, timeout=30, **kwargs):
kwargs['headers'] = format_values(kwargs['headers'])
if 'data' in kwargs:
kwargs['data'] = to_bytes(kwargs['data'])
try:
for j in range(self._retry):
for j in range(self._retry + 1):
try:
if method == 'POST':
res = self._session.post(url, timeout=timeout, **kwargs)
elif method == 'GET':
Expand All @@ -223,9 +223,11 @@ def send_request(self, method, url, bucket, timeout=30, **kwargs):
res = self._session.head(url, timeout=timeout, **kwargs)
if res.status_code < 400: # 2xx和3xx都认为是成功的
return res
except Exception as e: # 捕获requests抛出的如timeout等客户端错误,转化为客户端错误
logger.exception('url:%s, exception:%s' % (url, str(e)))
raise CosClientError(str(e))
except Exception as e: # 捕获requests抛出的如timeout等客户端错误,转化为客户端错误
logger.exception('url:%s, retry_time:%d exception:%s' % (url, j, str(e)))
if j < self._retry:
continue
raise CosClientError(str(e))

if res.status_code >= 400: # 所有的4XX,5XX都认为是COSServiceError
if method == 'HEAD' and res.status_code == 404: # Head 需要处理
Expand Down
2 changes: 1 addition & 1 deletion qcloud_cos/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

__version__ = '5.1.7.1'
__version__ = '5.1.7.2'
16 changes: 14 additions & 2 deletions ut/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
SecretId=SECRET_ID,
SecretKey=SECRET_KEY,
)
client = CosS3Client(conf)
client = CosS3Client(conf, retry=3)


def _create_test_bucket(test_bucket):
Expand Down Expand Up @@ -638,6 +638,18 @@ def test_list_multipart_uploads():
)


def test_upload_file_from_buffer():
import io
data = io.BytesIO(6*1024*1024*b'A')
response = client.upload_file_from_buffer(
Bucket=test_bucket,
Key='test_upload_from_buffer',
Body=data,
MaxBufferSize=5,
PartSize=1
)


def test_upload_file_multithreading():
"""根据文件大小自动选择分块大小,多线程并发上传提高上传速度"""
file_name = "thread_1GB"
Expand All @@ -650,7 +662,7 @@ def test_upload_file_multithreading():
Bucket=test_bucket,
Key=file_name,
LocalFilePath=file_name,
MAXThread=10,
MAXThread=5,
EnableMD5=True
)
ed = time.time() # 记录结束时间
Expand Down

0 comments on commit 78fbf23

Please sign in to comment.