补丁请求文件上传

时间:2019-03-05 16:30:37

标签: python rest optimization python-requests http-patch

我有一个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)

似乎只采用了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包时也是如此。

补丁程序无法识别file参数吗?要求文件上什么也没说。

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

1 个答案:

答案 0 :(得分:0)

在查看了邮递员之后,能够找到问题所在。这是解决方案。问题在于open语句,尤其是冲洗部分的位置参数,因为Content Length被自动覆盖,因此必须从响应请求中获取Content Length。

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)