Reputation: 222
I'm trying to upload a file to Azure File Service using the REST API. After creating the file, the 'put range' request fails with 'InvalidHeaderValue'
So far I've successfully created the file using the documentation found here. https://learn.microsoft.com/en-us/rest/api/storageservices/create-file
After that, I try to put content into the newly created file following the documentation found here. https://learn.microsoft.com/en-us/rest/api/storageservices/put-range
here is an example of the URI I'm using. It look correct next to the example in the doc.
uri = 'https://<account>.file.core.windows.net/<share>/<directory>/<file>?comp=range&<sas token>'
I get the content of the file with...
with open(file.txt, mode='rb') as fh:
data = fh.read()
My headers look like this...
headers = {
'x-ms-write':'update',
'x-ms-date':'Wed, 10 Apr 2019 22:14:17 GMT',
'x-ms-version':'2018-03-28',
'x-ms-range':'bytes=0-' + str(len(data)),
'Content-Length':str(len(data) + 1)
}
Request...
requests.put(url, headers=headers, data=data)
The response code I get back is a 400 and the return header is this...
{
'Content-Length': '322',
'Content-Type': 'application/xml',
'Server': 'Windows-Azure-File/1.0 Microsoft-HTTPAPI/2.0',
'x-ms-request-id': '23642a26-e01a-0049-35da-ef0109000000',
'x-ms-version': '2018-03-28',
'x-ms-error-code': 'InvalidHeaderValue',
'Date': 'Wed, 10 Apr 2019 20:14:46 GMT'
}
Thanks for help in advance.
Upvotes: 2
Views: 1534
Reputation: 30035
The error is due to the incorrect value of x-ms-range
and Content-Length
.
The correct one should like below:
headers={
#other headers
'x-ms-range':'bytes=0-' + str(len(data)-1),
'Content-Length':str(len(data))
}
My sample code as below, and works fine:
import requests
uri = r'https://xxx.file.core.windows.net/test/good222.txt?comp=range&sv=2018-03-28&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-04-12T10:59:42Z&st=2019-04-12T02:59:42Z&spr=https&sig=xxxxxxxxxx'
with open("D:\\hello.txt",mode='rb') as fh:
data = fh.read()
print(len(data))
headers={
'x-ms-write':'update',
'x-ms-date':'Fri, 12 Apr 2019 06:40:14 GMT',
'x-ms-version':'2018-03-28',
'x-ms-range':'bytes=0-' + str(len(data)-1),
'Content-Length':str(len(data))
}
r= requests.put(uri,data=data,headers=headers)
print(r.status_code)
print(r.headers)
The result(the file also updated on the azure portal):
Please also note that the file(locally) length should not be longer than the one you want to update on azure file share.
Upvotes: 3