From 66b1f9b951813c035049a5bab723876f88aa06f0 Mon Sep 17 00:00:00 2001 From: ShenYuhan Date: Mon, 14 Sep 2020 14:11:40 +0800 Subject: [PATCH] fix bugs when delete bos file (#805) --- visualdl/io/bfile.py | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/visualdl/io/bfile.py b/visualdl/io/bfile.py index 3c0eec48..65b988e3 100644 --- a/visualdl/io/bfile.py +++ b/visualdl/io/bfile.py @@ -127,10 +127,16 @@ class LocalFileSystem(object): fp.write(file_content) def append(self, filename, file_content, binary_mode=False): - self._write(filename, file_content, "ab" if binary_mode else "a") + try: + self._write(filename, file_content, "ab" if binary_mode else "a") + except FileNotFoundError: + self.makedirs(os.path.dirname(filename)) def write(self, filename, file_content, binary_mode=False): - self._write(filename, file_content, "ab" if binary_mode else "a") + try: + self._write(filename, file_content, "ab" if binary_mode else "a") + except FileNotFoundError: + self.makedirs(os.path.dirname(filename)) # self._write(filename, file_content, "wb" if binary_mode else "w") def walk(self, dir): @@ -315,13 +321,23 @@ class BosFileSystem(object): content_length=len(init_data)) content_length = len(file_content) - offset = self.get_meta(bucket_name, object_key).metadata.content_length - self.bos_client.append_object(bucket_name=bucket_name, - key=object_key, - data=file_content, - content_md5=content_md5(file_content), - content_length=content_length, - offset=offset) + try: + offset = self.get_meta(bucket_name, + object_key).metadata.content_length + self.bos_client.append_object(bucket_name=bucket_name, + key=object_key, + data=file_content, + content_md5=content_md5(file_content), + content_length=content_length, + offset=offset) + except (exception.BceServerError, exception.BceHttpClientError) as e: + init_data = b'' + self.bos_client.append_object(bucket_name=bucket_name, + key=object_key, + data=init_data, + content_md5=content_md5(init_data), + content_length=len(init_data)) + self._file_contents_to_add = b'' self._file_contents_count = 0 self._start_append_time = time.time() -- GitLab