Python应用访问国内Azure站点

清疚 2022-09-20 09:16 247阅读 0赞

Windows Azure为6种语言提供了native的访问接口:.NET/Java/Python/Ruby/Nodejs. 其中存储的API更是有十几种语言。在使用各种语言开发基于WindowsAzure的应用时,我们都会遇到一个问题,就是国内和国外的Azure URL不一样,会造成无法顺利调用。Windows Azure中国门户里面有一个文档,解释了这些差异:http://windowsazure.cn/zh-cn/develop/other/developerdifferences/

不过,对于Python来说,这个文档并没有给出详细的说明。下面我就给出几个例子,看看如何利用Python连接国内的Windows Azure平台。

  1. 调用Azure存储服务

在Windows Azure的文档中心中(http://www.windowsazure.com/en-us/develop/python/tutorials/web-app-with-blob-storage/?fb=zh-cn),我们可以看到常规的调用方法如下:

  1. from azure.storage import TableService
  2. table_service = TableService(account_name='myaccount', account_key='mykey')
  3. blob_service = BlobService(account_name='myaccount', account_key='mykey')
  4. queue_service = QueueService(account_name='myaccount', account_key='mykey')

显然,如果直接运行,这个API会连接国外的Azure存储账户。我们可以修改一下,让它连接国内的Azure

  1. from azure.storage import TableService
  2. table_service = TableService(account_name='myaccount', account_key='mykey',host_base='.table.core.chinacloudapi.cn')
  3. blob_service = BlobService(account_name='myaccount', account_key='mykey',host_base='.blob.core.chinacloudapi.cn')
  4. queue_service = QueueService(account_name='myaccount', account_key='mykey', host_base='.queue.core.chinacloudapi.cn')

我们增加了一个参数,即国内存储服务的URL。这样就可以顺利连接国内站点

  1. 调用Azure管理服务

同样的,如果希望调用Azure管理服务,比如创建虚拟机,我们可以将缺省的代码:

  1. from azure import *
  2. from azure.servicemanagement import *
  3. sms = ServiceManagementService(subscription_id, certificate_path)

修改为

  1. from azure import *
  2. from azure.servicemanagement import *
  3. sms = ServiceManagementService(subscription_id, certificate_path, 'management.core.chinacloudapi.cn')

对于其他的API怎么办呢?目前Windows Azure Python库的API文档并不是很完善,但是这个API库是开源的,我们可以直接查看其代码,很方便的

这个地址是Python API的文档:https://github.com/WindowsAzure/azure-sdk-for-python

如果我们要看具体的API,可以直接浏览代码目录:https://github.com/WindowsAzure/azure-sdk-for-python/tree/master/azure

比如我们可以在service bus里面,找到其初始化函数,然后我们就知道它可以接受一个host_base参数作为Azure端点

https://github.com/WindowsAzure/azure-sdk-for-python/blob/master/azure/servicebus/servicebusservice.py

SouthEast

发表评论

表情:
评论列表 (有 0 条评论,247人围观)

还没有评论,来说两句吧...

相关阅读

    相关 国内Azure购买指南

    本购买指南的目的是为正在考虑购买在中国由世纪互联运营的 Microsoft Azure 服务(“Azure 中国服务”)的本地和海外用户提供端到端的购买流程及上线指导说明。笔者