Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __test_resume(self, content_size, uploaded_parts, expected_unfinished=0):
bucket = random.choice([self.bucket, self.rsa_crypto_bucket, self.kms_crypto_bucket])
part_size = 100 * 1024
num_parts = (content_size + part_size - 1) // part_size
key = 'resume-' + random_string(32)
content = random_bytes(content_size)
encryption_flag = isinstance(bucket, oss2.CryptoBucket)
context = None
pathname = self._prepare_temp_file(content)
if encryption_flag:
context = models.MultipartUploadCryptoContext(content_size, part_size)
upload_id = bucket.init_multipart_upload(key, upload_context=context).upload_id
else:
upload_id = bucket.init_multipart_upload(key).upload_id
for part_number in uploaded_parts:
start = (part_number - 1) * part_size
if part_number == num_parts:
end = content_size
else:
end = start + part_size
if encryption_flag:
bucket.upload_part(key, upload_id, part_number, content[start:end], upload_context=context)
else:
bucket.upload_part(key, upload_id, part_number, content[start:end])
"""断点续传上传
Args:
path (TYPE): file abspath
Returns:
TYPE: Description
"""
part_size = os.path.getsize(path) if os.path.getsize(
path) < 1024 * 1024 else os.path.getsize(path) // 10
success = False
retry = 10
while not success and retry>0:
retry -= 1
try:
oss2.resumable_upload(self.bucket, path.rsplit(os.sep, 1)[1], path, progress_callback=self.percentage,
# store=oss2.ResumableStore(root='/tmp'),
store=oss2.ResumableStore(root='/tmp' if checkOS()=='linux' else config.BASE_DIR),
multipart_threshold=1024 * 1024,
part_size=part_size,
num_threads=4)
success = True
return True
except oss2.exceptions.RequestError as e:
log.warn('上传失败,即将进行重试')
time.sleep(2)
continue
return False
def test_cname_object(self):
bucket = oss2.Bucket(oss2.Auth(OSS_ID, OSS_SECRET), OSS_CNAME, OSS_BUCKET, is_cname=True)
bucket.put_object('hello.txt', 'hello world')
policy_text = ''
policy_text += '{'
policy_text += '"Version":"1",'
policy_text += '"Statement":[{'
policy_text += '"Action":["oss:*"],'
policy_text += '"Effect":"Allow",'
policy_text += '"Principal":["{0}"],'.format(OSS_PAYER_UID)
policy_text += '"Resource": ["acs:oss:*:*:{0}","acs:oss:*:*:{0}/*"]'.format(OSS_BUCKET)
policy_text += '}]}'
self.bucket.put_bucket_policy(policy_text)
# Enable bucket request payment
result = self.bucket.put_bucket_request_payment(PAYER_REQUESTER)
self.assertEqual(result.status, 200)
self.payer_bucket = oss2.Bucket(oss2.Auth(OSS_PAYER_ID, OSS_PAYER_SECRET), OSS_ENDPOINT, OSS_BUCKET)
def test_upload_sequenial(self):
endpoint = "http://oss-cn-shanghai.aliyuncs.com"
auth = oss2.Auth(OSS_ID, OSS_SECRET)
bucket_name = OSS_BUCKET + "-test-upload-sequential"
bucket = oss2.Bucket(auth, endpoint, bucket_name)
bucket.create_bucket()
key = "test-upload_sequential"
content = random_bytes(10 * 100 * 1024)
pathname = self._prepare_temp_file(content)
oss2.resumable_upload(bucket, key, pathname, multipart_threshold=200 * 1024, part_size=100 * 1024, num_threads=6)
result = bucket.get_object(key)
self.assertIsNone(result.resp.headers.get('Content-MD5'))
# single thread in sequential mode.
params = {'sequential': ''}
oss2.resumable_upload(bucket, key, pathname, multipart_threshold=200 * 1024, part_size=100 * 1024, num_threads=1,
params=params)
ming-xxx-share
{0}
'''
for prefix in [u'日志%+/*', 'logging/', '日志%+/*']:
req_info = unittests.common.mock_response(do_request, response_text.format(oss2.to_string(prefix)))
result = unittests.common.bucket().get_bucket_logging()
self.assertRequest(req_info, request_text)
self.assertEqual(result.target_bucket, 'ming-xxx-share')
self.assertEqual(result.target_prefix, oss2.to_string(prefix))
{0}
{1}
'''
for index, error in [('index+中文.html', 'error.中文') ,(u'中-+()文.index', u'@#$%中文.error')]:
req_info = unittests.common.mock_response(do_request, response_text.format(oss2.to_string(index), oss2.to_string(error)))
result = unittests.common.bucket().get_bucket_website()
self.assertRequest(req_info, request_text)
self.assertEqual(result.index_file, oss2.to_string(index))
self.assertEqual(result.error_file, oss2.to_string(error))
Accept: */*
authorization: OSS ZCDmm7TPZKHtx77j:PAedG7U86ZxQ2WTB+GdpSltoiTI='''
response_text = '''HTTP/1.1 200 OK
Server: AliyunOSS
Date: Sat, 12 Dec 2015 00:35:53 GMT
Content-Type: text/plain
Content-Length: {0}
Connection: keep-alive
x-oss-request-id: 566B6BE93A7B8CFD53D4BAA3
Accept-Ranges: bytes
ETag: "D80CF0E5BE2436514894D64B2BCFB2AE"
Last-Modified: Sat, 12 Dec 2015 00:35:53 GMT
x-oss-object-type: Normal
{1}'''.format(len(content), to_string(content))
return request_text, response_text
self.retry_assert(lambda: same_logging(other_bucket.get_bucket_logging(),
self.bucket.bucket_name,
to_string(prefix)))
Content-Length: 218
Connection: keep-alive
x-oss-request-id: 566B6BE5FFDB697977D52407
{0}
{1}
'''
for index, error in [('index+中文.html', 'error.中文') ,(u'中-+()文.index', u'@#$%中文.error')]:
req_info = unittests.common.mock_response(do_request, response_text.format(oss2.to_string(index), oss2.to_string(error)))
result = unittests.common.bucket().get_bucket_website()
self.assertRequest(req_info, request_text)
self.assertEqual(result.index_file, oss2.to_string(index))
self.assertEqual(result.error_file, oss2.to_string(error))