From 2dbc2f8506b629311f303e035ab43dbf4d87a936 Mon Sep 17 00:00:00 2001 From: lewzylu <327874225@qq.com> Date: Mon, 9 Apr 2018 16:28:34 +0800 Subject: [PATCH] Added a parameter that can set the head (#122) --- coscmd/cos_client.py | 11 +++++++---- coscmd/cos_cmd.py | 2 +- requirements.txt | 13 +++++++------ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/coscmd/cos_client.py b/coscmd/cos_client.py index 9daf19b..a1bb840 100644 --- a/coscmd/cos_client.py +++ b/coscmd/cos_client.py @@ -245,7 +245,7 @@ def upload_folder(self, local_path, cos_path, _type='Standard', _encryption='', logger.debug("upload {file} success".format(file=to_printable_str(filepath))) return ret_code - def upload_file(self, local_path, cos_path, _type='Standard', _encryption='', _http_headers=''): + def upload_file(self, local_path, cos_path, _type='Standard', _encryption='', _http_headers='{}'): _http_header = yaml.safe_load(_http_headers) @@ -262,7 +262,8 @@ def single_upload(): for j in range(self._retry): try: http_header = _http_header - http_header['x-cos-storage-class'] = self._type + if http_header is None or http_header.has_key('x-cos-storage-class') is False: + http_header['x-cos-storage-class'] = self._type if _encryption != '': http_header['x-cos-server-side-encryption'] = self._encryption rt = self._session.put(url=url, @@ -280,7 +281,8 @@ def single_upload(): continue if j+1 == self._retry: return False - except Exception: + except Exception, e: + logger.warn(e) logger.warn("upload file failed") return False @@ -301,7 +303,8 @@ def init_multiupload(): logger.info("continue uploading from last breakpoint") return True http_header = _http_header - http_header['x-cos-storage-class'] = self._type + if http_header is None or http_header.has_key('x-cos-storage-class') is False: + http_header['x-cos-storage-class'] = self._type if _encryption != '': http_header['x-cos-server-side-encryption'] = self._encryption rt = self._session.post(url=url+"?uploads", auth=CosS3Auth(self._conf._secret_id, self._conf._secret_key), headers=http_header) diff --git a/coscmd/cos_cmd.py b/coscmd/cos_cmd.py index ece3b45..d31f5dd 100644 --- a/coscmd/cos_cmd.py +++ b/coscmd/cos_cmd.py @@ -467,7 +467,7 @@ def command_thread(): parser_upload.add_argument('-r', '--recursive', help="upload recursively when upload directory", action="store_true", default=False) parser_upload.add_argument('-t', '--type', help='specify x-cos-storage-class of files to upload', type=str, choices=['STANDARD', 'STANDARD_IA', 'NEARLINE'], default='STANDARD') parser_upload.add_argument('-e', '--encryption', help="set encryption", type=str, default='') - parser_upload.add_argument('-H', '--headers', help="set HTTP headers", type=str, default='') + parser_upload.add_argument('-H', '--headers', help="set HTTP headers", type=str, default='{}') parser_upload.set_defaults(func=Op.upload) parser_download = sub_parser.add_parser("download", help="download file from COS to local.") diff --git a/requirements.txt b/requirements.txt index 36a7060..922fac4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,7 @@ -requests>=2.12.4 -argparse>=1.4.0 -prettytable==0.7 -pytz==2017.2 -datetime==4.2 -tqdm==4.15.0 +requests>=2.12.4 +argparse>=1.4.0 +prettytable==0.7 +pytz==2017.2 +datetime==4.2 +tqdm==4.15.0 +pyyaml==3.12 \ No newline at end of file