phpazureazure-storageazure-sdkhttp-content-length

Azure file storage putRange operation fails with 416: InvalidHeaderValue (Content-Length)


I'm using the Azure SDK to perform basic operations on an Azure files storage. In order to support larger file uploads I support chunked uploads, so I'm calling:

$storage->createFileFromContent(...) on the first chunk and

$storage->putFileRange(...) on all the following.

The first request works fine and the file is filled with inside the first range specified. The second and following requests however fail with the error:

Code: 416
Value: The range specified is invalid for the current size of the resource.

Here are my specified headers for the first request:

{
  ["x-ms-range"]=>"bytes=0-2999999"
  ["content-length"]=>3000000
  ["x-ms-write"]=>"Update"
}

Actual size of the stream: 3000000

And for the second request:

{
  ["x-ms-range"]=>"bytes=3000000-5999999"
  ["content-length"]=>3000000
  ["x-ms-write"]=>"Update"
}

Actual size of the stream: 3000000

Can someone please tell me what is wrong?? I also tried to adjust the Range to something like 300000-6000000, but then I get this error:

"Code: 400 Value: The value for one of the HTTP headers is not in the correct format. >> Content-Length"

Could this be caused by a wrong configuration of the storage in Azure?


Solution

  • Most likely you are not uploading the ranges to a file that is actually 6000000 byte long. Put Range operation will not expand the size of the file for you implicitly. You need to explicitly call SetFileProperties to resize the file first.

    $storage->createFileFromContent(...) will set the file size as the same size to the content, so it is very likely not to be 6000000 byte.

    Also, if there is a mismatch in range and the content length, e.g. in your case where range specifies 3000001 bytes while content length is 3000000 bytes, a mentioned 400 will be returned by the server.