将指定数据存储到文件服务器的指定路径下 :params data: 需要上传的数据 :type data: string :params to_file_url_or_path: 可以指定上传到指定路径,如已存在,则更新该文件,为空则由系统创建随机命名文件 :type to_file_url_or_path: string :params type: 仅在to_file_url_or_path为路径时有效 :type type: FileServer.TypeEnum :return
(self, data, to_file_url_or_path=None, type=TypeEnum.TEMPORARY)
| 81 | |
| 82 | @RetryDecor(interval=3) |
| 83 | def put_file(self, data, to_file_url_or_path=None, type=TypeEnum.TEMPORARY): |
| 84 | """将指定数据存储到文件服务器的指定路径下 |
| 85 | :params data: 需要上传的数据 |
| 86 | :type data: string |
| 87 | :params to_file_url_or_path: 可以指定上传到指定路径,如已存在,则更新该文件,为空则由系统创建随机命名文件 |
| 88 | :type to_file_url_or_path: string |
| 89 | :params type: 仅在to_file_url_or_path为路径时有效 |
| 90 | :type type: FileServer.TypeEnum |
| 91 | :return: file_url:文件服务器指定的路径 |
| 92 | """ |
| 93 | if type not in self.TypeEnum.values(): |
| 94 | raise ValueError("type参数错误。支持的类型:%s" % self.TypeEnum.values()) |
| 95 | if to_file_url_or_path: |
| 96 | file_url = to_file_url_or_path if to_file_url_or_path.startswith(self._server_url) \ |
| 97 | else urllib.parse.urljoin(self._server_url, "%s_%s/%s" % (self._type_prefix, type, to_file_url_or_path)) |
| 98 | else: |
| 99 | # 无命名文件默认上传到临时文件夹,数据保留7天 |
| 100 | file_url = urllib.parse.urljoin(self._server_url, "%s_%s/unnamed/%s.file" % ( |
| 101 | self._type_prefix, self.TypeEnum.TEMPORARY, uuid.uuid1().hex)) |
| 102 | headers = copy.copy(self.get_auth_headers()) |
| 103 | headers.update({"Content-SHA256": self.get_data_sha256(data), "Content-MD5": self.get_data_md5(data)}) |
| 104 | rsp = self._http_client.put(file_url, data=data, headers=headers) |
| 105 | if rsp.status == self.OK_STATUS: |
| 106 | return file_url |
| 107 | else: |
| 108 | logger.error('return code %d when put file to %s, content: %s' % ( |
| 109 | rsp.status, file_url, rsp.data.decode("utf-8"))) |
| 110 | raise ServerError(errcode.E_SERVER_FILE_SERVICE_ERROR, |
| 111 | 'return code %d when put file to %s' % (rsp.status, file_url)) |
| 112 | |
| 113 | @RetryDecor() |
| 114 | def get_file(self, file_url): |
no test coverage detected