0

我有一个 REST API,我正在尝试将数据上传到,基本上是这样的:https ://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/update

现在,由于我唯一的选择是 PATCH,我有哪些选项可以优化数据加载。我已经能够通过使用 data 参数和使用 read() 函数上传文件,但我认为这不是最佳的,因为我猜整个文件都被读入内存。我尝试使用 files 参数(multipaprt 表单编码)并且还查看了 toolbelt 包,但这似乎不适用于 PATCH

这是有效但不是最佳的示例代码

files={'file':('Sample',open('D:/FilePath/Demo.txt','rb'))}
length=os.stat('D:/FilePath/Demo.txt')
filesize=str(length.st_size)

with open('D:/File|Path/Demo.txt','rb') as f:
    file_data = f.read()

leng=len(file_data)

header = {
'Authorization': "Bearer " + auth_t
}

header_append = {
'Content-Length': filesize,
'Authorization': "Bearer " + auth_t
#'If-None-Match': "*" #Conditional HTTP Header
}

header_flush = {
'Content-Length': '0',
'Authorization': "Bearer " + auth_t
}


header_read = {
'Authorization': "Bearer " + auth_t
}

try:
    init_put=requests.put('https://adlstorageacc.dfs.core.windows.net/adobe/2019/02/DemoStreamFile4.txt?resource=file&recursive=True', headers=header_flush, proxies=proxies,verify=False)
    init_write=requests.patch('https://adlstorageacc.dfs.core.windows.net/adobe/2019/02/DemoStreamFile4.txt?action=append&position=0', headers=header_append, proxies=proxies,verify=False,data=file_data)

    flush_url='https://adlstorageacc.dfs.core.windows.net/adobe/2019/02/DemoStreamFile4.txt?action=flush&position=' + str(leng)
    init_flush=requests.patch(flush_url, headers=header_flush, proxies=proxies,verify=False)

问题是线路

init_write=requests.patch('https://adlstorageacc.dfs.core.windows.net/adobe/2019/02/DemoStreamFile4.txt?action=append&position=0', headers=header_append, proxies=proxies,verify=False,data=file_data)

它似乎只接受数据参数。如果我将其更改为

init_write=requests.patch('https://adlstorageacc.dfs.core.windows.net/adobe/2019/02/DemoStreamFile4.txt?action=append&position=0', headers=header_append, proxies=proxies,verify=False,file=files)

我得到一个空文件。

当我使用 requestToolbelt 包时也是如此。

补丁不能识别文件参数吗?请求文件上没有任何说明。

此外,如果数据参数是唯一的出路,那么在不执行 f.read() 或使用 f.read(n) 迭代指定要读取的字符数的情况下加载文件的最佳方法是什么。没有更好的方法吗?

4

1 回答 1

0

也通过Postman查看后,能够找到问题。这是解决方案。问题是 with open 语句,尤其是flush部分的位置参数,因为内容长度被自动覆盖,所以必须从响应的请求中获取内容长度。

files={'file':('Sample',open('D:/FilePath/Demo.txt','rb'))}
length=os.stat('D:/FilePath/Demo.txt')
filesize=str(length.st_size)
header = {
# 'Content-Type': 'text/plain',
'Authorization': "Bearer " + auth_t
#'If-None-Match': "*" #Conditional HTTP Header
}

header_append = {
'Content-Length': filesize,
'Authorization': "Bearer " + auth_t
#'If-None-Match': "*" #Conditional HTTP Header
}

header_flush = {
'Content-Type': "application/x-www-form-urlencoded",
'Content-Length': '0',
'Authorization': "Bearer " + auth_t,
#'If-None-Match': "*" #Conditional HTTP Header
}


header_read = {
# 'Content-Type': 'text/plain',
'Authorization': "Bearer " + auth_t,
#'Range': 'bytes=300000-302591'
#'If-None-Match': "*" #Conditional HTTP Header
}

try:
   init_put=requests.put('https://adlstorageacc.dfs.core.windows.net/adobe/2019/02/DemoStreamFile4.txt?resource=file&recursive=True', headers=header_flush, proxies=proxies,verify=False)
   init_write=requests.patch('https://adlstorageacc.dfs.core.windows.net/adobe/2019/02/DemoStreamFile4.txt?action=append&position=0', headers=header_append, proxies=proxies,verify=False,files=files)
   flush_length=init_write.request.headers['Content-Length']
   flush_url='https://adlstorageacc.dfs.core.windows.net/adobe/2019/02/DemoStreamFile4.txt?action=flush&position=' + str(flush_length)
   init_flush=requests.patch(flush_url, headers=header_flush, proxies=proxies,verify=False) 
except Exception as e:
    print("In Error")
    print(e)
于 2019-03-07T16:44:48.870 回答