您的位置:首页 > 编程语言 > C语言/C++

关于Azure Storage 的一点研究

2015-08-25 15:30 435 查看


事情是这样的,老大让看一下Azure云存储,是不是适合我们的需求,于是花了点时间去看了下微软的这个东东。虽然最后并没有深入下去,但是还是想把遇到的一点小问题记录一下,说不定能帮到有同样疑问的童鞋。

关于Azure Storage 的介绍和用C++去使用blob Storage,可以参考下面这个官方资料:https://azure.microsoft.com/en-us/documentation/articles/storage-c-plus-plus-how-to-use-blobs/
运维给了一个URI https://{account_name}.blob.core.chinacloudapi.cn/,一个account_name和一个account_key.

在尝试往URI上传文件时,发现报错,提示找不到对应地址。

使用的C++源码是从官方资料上直接copy的,如下:


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

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

// Create the blob client.
azure::storage::cloud_blob_client blob_client = storage_account.create_cloud_blob_client();  



在仔细查阅官方资料和各种民间讨论中,最终推断出是URI的默认domain后缀的问题。具体来说,官方拼接的连接字符串中,并没有指定

core.windows.net  Or

core.chinacloudapi.cn



推测应该是前者是默认的域后缀,而我需要使用的是中国地区的域后缀。

问题知道了,下一步就是如何解决了。

翻了很多官方和非官方的资料,都没有说到该怎么去设置这个值。

好在机智的我,通过使用如下所示的一个可视化管理工具,找到了一点线索。

可视化工具 Azure Storage Explorer 6 Preview 3 (August 2014)(开源):
http://azurestorageexplorer.codeplex.com/




有了这个图,更加证明了之前我的想法,问题就出现在这个Storage endpoints domain。
遗憾的是,这里的的UI并不能直接告诉我这个值对应的变量名是什么。而试图去查看使用UI生成的连接字符串,发现跟官方demo里一致。
还在,这是个开源的工具。最终,经过对其C#源码的一番研究后,一切问题都得到了解决!
具体如下:
azure::storage::storage_credentials credential = azure::storage::storage_credentials(my_account_name,my_account_key);

azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account(
credential, endpoint_suffix, true
);
另外,通过分析源码的命名规则,推测出那个指定domain的变量值很可能应该长成这样:EndpointSuffix
于是,方案二也出现了:
// Define the connection-string with your values.
const utility::string_t storage_connection_string(
U("EndpointSuffix=core.chinacloudapi.cn;DefaultEndpointsProtocol=https;AccountName=your_storage_account;AccountKey=your_storage_account_key")
);
至此,问题得到完美解决~
PS:不知道是不是我的开发环境问题(VS2013 & C++ Azure SDK),官方demo的
concurrency::streams::file_stream<uint8_t>::open_istream


方法我的程序里提示没有定义,进而无法使用upload_from_stream这个方法,

后来没有正面去解决,而是使用了upload_from_file方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息