2
votes

I'm trying to use azure-storage-cpp's APIs to write to page blobs using the following code:

// The code is representative. I have removed some of the error handling code.
void page_blobs_async()
{

 // Initialize storage account
  azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string);

  // Create a blob container
  azure::storage::cloud_blob_client blob_client = storage_account.create_cloud_blob_client();
  azure::storage::cloud_blob_container container = blob_client.get_container_reference(_XPLATSTR("my-sample-page-async-container"));

  // Return value is true if the container did not exist and was successfully created.
  container.create_if_not_exists();

  // Make the blob container publicly accessible
  azure::storage::blob_container_permissions permissions;
  permissions.set_public_access(azure::storage::blob_container_public_access_type::blob);
  container.upload_permissions(permissions);
  filename = "DataFile1.txt" // This contains '1'
  string blob_name = "async_blob"
  concurrency::streams::istream input_stream = concurrency::streams::file_stream<uint8_t>::open_istream(_XPLATSTR(filename)).get();
  azure::storage::cloud_block_blob blob1 = container.get_block_blob_reference(_XPLATSTR(blob_name));
  azure::storage::cloud_page_blob blob1 = container.get_page_blob_reference(_XPLATSTR(blob_name));
  blob1.create(16 * 1024 * 1024);
  std::cout<<"Blob write started"<<std::endl;
  blob1.upload_from_stream(input_stream);

}

I get the following error:

Error: The value for one of the HTTP headers is not in the correct format. The value for one of the HTTP headers is not in the correct format. RequestId:e4fe5ee9-e01e-0045-6076-386b87000000 Time:2018-08-20T11:08:57.8352526Z

Header dump:

headers Date:Mon, 20 Aug 2018 11:08:57 GMT
headers ETag:"0x8D6068BAC4DC466"
headers Last-Modified:Mon, 20 Aug 2018 10:57:08 GMT
headers Server:Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
headers Transfer-Encoding:chunked
headers x-ms-blob-public-access:blob
headers x-ms-lease-state:available
headers x-ms-lease-status:unlocked
headers x-ms-request-id:e4fe5ebd-e01e-0045-3a76-386b87000000
headers x-ms-version:2017-04-17
headers Date:Mon, 20 Aug 2018 11:08:57 GMT
headers ETag:"0x8D6068D535B837D"
headers Last-Modified:Mon, 20 Aug 2018 11:08:57 GMT
headers Server:Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
headers Transfer-Encoding:chunked
headers x-ms-request-id:e4fe5ec5-e01e-0045-4076-386b87000000
headers x-ms-version:2017-04-17
headers Date:Mon, 20 Aug 2018 11:08:57 GMT
headers ETag:"0x8D6068D535DE4B5"
headers Last-Modified:Mon, 20 Aug 2018 11:08:57 GMT
headers Server:Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
headers Transfer-Encoding:chunked
headers x-ms-request-id:e4fe5ee4-e01e-0045-5b76-386b87000000
headers x-ms-request-server-encrypted:true
headers x-ms-version:2017-04-17
Blob write started
headers Content-Length:331
headers Content-Type:application/xml
headers Date:Mon, 20 Aug 2018 11:08:57 GMT
headers Server:Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
headers x-ms-request-id:e4fe5ee9-e01e-0045-6076-386b87000000
headers x-ms-version:2017-04-17
headers Content-Length:331
headers Content-Type:application/xml
headers Date:Mon, 20 Aug 2018 11:08:57 GMT
headers Server:Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
headers x-ms-request-id:e4fe5ee9-e01e-0045-6076-386b87000000
headers x-ms-version:2017-04-17

I'm using cpprestsdk-2.9.1 and Ubuntu 16.04.

1

1 Answers

2
votes

If you catch the request traffic with tools like Fiddler, you may see the error message point to header x-ms-blob-content-length.

See Storage page blob REST API:

The page blob size must be aligned to a 512-byte boundary.

It is by design and described in storage docs:

Page blobs are a collection of 512-byte pages optimized for random read and write operations.

So we should assure length of file to upload is multiple of 512-byte. Besides, upload_from_stream is to initialize a new page blob(overwrite existing one), create in advance seems unnecessary.