def __init__(self, file_system, bucket_name, filename): self._file_system = file_system self._bucket_name = bucket_name self._key_name = filename self._closed = False self._file_name = path.basename(filename) self._name = self._key_name self._uploader = StreamUploader(self._file_system.client, self._bucket_name, self._key_name)
class MockCosWriteFile(object): def __init__(self, file_system, bucket_name, filename): self._file_system = file_system self._bucket_name = bucket_name self._key_name = filename self._closed = False self._file_name = path.basename(filename) self._name = self._key_name self._isEmptyFile = True self._uploader = StreamUploader(self._file_system.client, self._bucket_name, self._key_name) @property def name(self): return self._name def write(self, data): self._uploader.write(data) self._isEmptyFile = False return len(data) def close(self): logger.info("Closing file: {0}".format(self._key_name)) try: if self._isEmptyFile: self._file_system.client.put_object(Bucket=self._bucket_name, Body="", Key=self._key_name) self._uploader.close() except Exception as e: logger.exception( "close the uploader occurs an exception. File: {0}".format( str(self._key_name).encode("utf-8"))) raise FilesystemError("Upload failed. File:{0}".format( self._key_name)) finally: logger.debug("Upload finish. File:{0}".format(self._key_name)) self._closed = True @property def closed(self): return self._closed
class MockCosWriteFile(object): def __init__(self, file_system, bucket_name, filename): self._file_system = file_system self._bucket_name = bucket_name self._key_name = filename self._closed = False self._file_name = path.basename(filename) self._name = self._key_name self._uploader = StreamUploader(self._file_system.client, self._bucket_name, self._key_name) @property def name(self): return self._name def write(self, data): self._uploader.write(data) print "Recv data_len: %d, file: %s" % (len(data), self._key_name) return len(data) def close(self): logger.info("Closing file: {0}".format(self._key_name)) try: self._uploader.close() except Exception as e: logger.exception(e) raise FilesystemError("Upload failed. File:{0}".format( self._key_name)) finally: logger.debug("Upload finish. File:{0}".format(self._key_name)) self._closed = True @property def closed(self): return self._closed