C ++ Azure文件上载:服务器无法验证请求

时间:2017-12-28 11:52:36

标签: python c++ azure

我正在尝试使用以下C ++代码(从官方文档here引用)将文件上传到我的azure文件服务中:

const utility::string_t storage_connection_string(U("DefaultEndpointsProtocol=https;AccountName=myaccount;"
    "AccountKey=mykey"));

// Retrieve storage account from connection string.
azure::storage::cloud_storage_account storage_account =
  azure::storage::cloud_storage_account::parse(storage_connection_string);

// Create the Azure Files client.
azure::storage::cloud_file_client file_client =
  storage_account.create_cloud_file_client();

// Get a reference to the share.
azure::storage::cloud_file_share share =
  file_client.get_share_reference(_XPLATSTR("myfileservice"));

//Get a reference to the root directory for the share.
azure::storage::cloud_file_directory root_dir = share.get_root_directory_reference();

// Upload a file from a file.
azure::storage::cloud_file file =
  root_dir.get_file_reference(_XPLATSTR("my-sample-file"));
file.upload_from_file(_XPLATSTR("test.pdf"));

这会发出以下错误:

terminate called after throwing an instance of 'azure::storage::storage_exception'

what():  Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

我搜索了互联网,发现它应该与使用SAS的身份验证相关。我在线尝试了很多示例代码,但仍然无法成功。

但是,我可以通过以下 Python 代码without SAS authentication进行类似的文件上传,并且没有任何问题:

import os
from azure.storage.file import FileService, ContentSettings

currentDir = os.path.dirname(os.path.abspath(__file__))

#credentials for azure account
file_service = FileService(account_name='myaccount', account_key='mykey')

#path for azure file
global pathStr
pathStr = 'https://myaccount.file.core.windows.net/myfileservice/'

#function for uploading file onto Azure container
def upload(myfile):
    file_service.create_file_from_path(
        'myfileservice',
        'files',
        test + '.pdf',
        myfile,
        content_settings=ContentSettings(content_type='text/pdf')       
        )
print('finished uploading file.')

这可能是什么解决方案?

0 个答案:

没有答案