Skip to content

Commit

Permalink
Add verification that the key cannot be empty to the __do_object inte…
Browse files Browse the repository at this point in the history
…rface (#344)
  • Loading branch information
zhuxiaolong37 authored and huiguangjun committed May 29, 2023
1 parent 7176abe commit c09d31b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
14 changes: 9 additions & 5 deletions oss2/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ def list_objects(self, prefix='', delimiter='', marker='', max_keys=100, headers
logger.debug(
"Start to List objects, bucket: {0}, prefix: {1}, delimiter: {2}, marker: {3}, max-keys: {4}".format(
self.bucket_name, to_string(prefix), delimiter, to_string(marker), max_keys))
resp = self.__do_object('GET', '',
resp = self.__do_bucket('GET',
params={'prefix': prefix,
'delimiter': delimiter,
'marker': marker,
Expand Down Expand Up @@ -560,7 +560,7 @@ def list_objects_v2(self, prefix='', delimiter='', continuation_token='', start_
"Start to List objects, bucket: {0}, prefix: {1}, delimiter: {2}, continuation_token: {3}, "
"start-after: {4}, fetch-owner: {5}, encoding_type: {6}, max-keys: {7}".format(
self.bucket_name, to_string(prefix), delimiter, continuation_token, start_after, fetch_owner, encoding_type, max_keys))
resp = self.__do_object('GET', '',
resp = self.__do_bucket('GET',
params={'list-type': '2',
'prefix': prefix,
'delimiter': delimiter,
Expand Down Expand Up @@ -1278,7 +1278,7 @@ def batch_delete_objects(self, key_list, headers=None):
headers = http.CaseInsensitiveDict(headers)
headers['Content-MD5'] = utils.content_md5(data)

resp = self.__do_object('POST', '',
resp = self.__do_bucket('POST',
data=data,
params={'delete': '', 'encoding-type': 'url'},
headers=headers)
Expand All @@ -1305,7 +1305,7 @@ def delete_object_versions(self, keylist_versions, headers=None):
headers = http.CaseInsensitiveDict(headers)
headers['Content-MD5'] = utils.content_md5(data)

resp = self.__do_object('POST', '',
resp = self.__do_bucket('POST',
data=data,
params={'delete': '', 'encoding-type': 'url'},
headers=headers)
Expand Down Expand Up @@ -1463,7 +1463,7 @@ def list_multipart_uploads(self,

headers = http.CaseInsensitiveDict(headers)

resp = self.__do_object('GET', '',
resp = self.__do_bucket('GET',
params={'uploads': '',
'prefix': prefix,
'delimiter': delimiter,
Expand Down Expand Up @@ -2829,6 +2829,10 @@ def delete_bucket_callback_policy(self):


def __do_object(self, method, key, **kwargs):
if not self.bucket_name:
raise ClientError("Bucket name should not be null or empty.")
if not key:
raise ClientError("key should not be null or empty.")
return self._do(method, self.bucket_name, key, **kwargs)

def __do_bucket(self, method, **kwargs):
Expand Down
35 changes: 34 additions & 1 deletion tests/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -1484,7 +1484,40 @@ def test_async_process_object(self):
except:
self.assertFalse(True, 'should not here')


def test_restore_object(self):
auth = oss2.Auth(OSS_ID, OSS_SECRET)
bucket_name = self.OSS_BUCKET + "-test-do-object-check"
key = 'a.txt'

bucket = oss2.Bucket(auth, OSS_ENDPOINT, bucket_name)
bucket.create_bucket(oss2.BUCKET_ACL_PRIVATE, oss2.models.BucketCreateConfig(oss2.BUCKET_STORAGE_CLASS_ARCHIVE))

wait_meta_sync()

put_result = bucket.put_object(key, 'content')
self.assertEqual(put_result.status, 200)
try:
bucket.delete_object('')
except oss2.exceptions.ClientError as e:
self.assertEqual(e.body, 'ClientError: key should not be null or empty.')

try:
bucket = oss2.Bucket(auth, OSS_ENDPOINT, bucket_name)

bucket.create_bucket(oss2.BUCKET_ACL_PRIVATE, oss2.models.BucketCreateConfig(oss2.BUCKET_STORAGE_CLASS_ARCHIVE))

wait_meta_sync()

bucket.delete_object(None)

except oss2.exceptions.ClientError as e:
self.assertEqual(e.body, 'ClientError: key should not be null or empty.')


del_result = bucket.delete_object(key)
self.assertEqual(del_result.status, 204)
bucket.delete_bucket()

class TestSign(TestObject):
"""
这个类主要是用来增加测试覆盖率,当环境变量为oss2.AUTH_VERSION_2,则重新设置为oss2.AUTH_VERSION_1再运行TestObject,反之亦然
Expand Down

0 comments on commit c09d31b

Please sign in to comment.