MCPcopy Create free account
hub / github.com/Tencent/CodeAnalysis / put_file

Method put_file

server/projects/main/util/fileserver.py:77–104  ·  view source on GitHub ↗

将指定数据存储到文件服务器的指定路径下 :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)

Source from the content-addressed store, hash-verified

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

Callers 4

contextMethod · 0.45
resultMethod · 0.45
task_paramsMethod · 0.45
resultMethod · 0.45

Calls 8

get_auth_headersMethod · 0.95
get_data_sha256Method · 0.95
get_data_md5Method · 0.95
ServerErrorClass · 0.90
errorMethod · 0.80
valuesMethod · 0.45
updateMethod · 0.45
putMethod · 0.45

Tested by

no test coverage detected