【问题标题】:Access Azure blob storage from within an Azure ML experiment从 Azure ML 实验中访问 Azure Blob 存储
【发布时间】:2016-05-16 18:27:00
【问题描述】:

Azure ML 实验提供了通过 ReaderWriter 模块将 CSV 文件读取和写入 Azure Blob 存储的方法。但是,我需要将 JSON 文件写入 blob 存储。由于没有模块可以这样做,我试图在 Execute Python Script 模块中这样做。

# Import the necessary items
from azure.storage.blob import BlobService

def azureml_main(dataframe1 = None, dataframe2 = None):
    account_name = 'mystorageaccount'
    account_key='mykeyhere=='
    json_string='{jsonstring here}'

    blob_service = BlobService(account_name, account_key)

    blob_service.put_block_blob_from_text("upload","out.json",json_string)

    # Return value must be of a sequence of pandas.DataFrame
    return dataframe1,

但是,这会导致错误:ImportError: No module named azure.storage.blob

这意味着 azure-storage Python 包未安装在 Azure ML 上。

如何从 Azure ML 实验中写入 Azure blob 存储?

这是填充错误消息:

Error 0085: The following error occurred during script evaluation, please view the output log for more information:
---------- Start of error message from Python interpreter ----------
data:text/plain,Caught exception while executing function: Traceback (most recent call last):
  File "C:\server\invokepy.py", line 162, in batch
    mod = import_module(moduleName)
  File "C:\pyhome\lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "C:\temp\azuremod.py", line 19, in <module>
    from azure.storage.blob import BlobService
ImportError: No module named azure.storage.blob

---------- End of error message from Python  interpreter  ----------
Start time: UTC 02/06/2016 17:59:47
End time: UTC 02/06/2016 18:00:00`

谢谢大家!

更新:感谢 Dan 和 Peter 提出以下想法。这是我使用这些建议所取得的进展。我创建了一个干净的 Python 2.7 虚拟环境(在 VS 2005 中),并使用pip install azure-storage 将依赖项放入我的站点包目录中。然后我压缩了 site-packages 文件夹并作为 Zip 文件上传,按照下面 Dan 的说明。然后我包含了对站点包目录的引用并成功导入了所需的项目。这导致写入博客存储时出现超时错误。

这是我的代码:

# Get access to the uploaded Python packages    
import sys
packages = ".\Script Bundle\site-packages"
sys.path.append(packages)

# Import the necessary items from packages referenced above
from azure.storage.blob import BlobService
from azure.storage.queue import QueueService

def azureml_main(dataframe1 = None, dataframe2 = None):
    account_name = 'mystorageaccount'
    account_key='p8kSy3F...elided...3plQ=='

    blob_service = BlobService(account_name, account_key)
    blob_service.put_block_blob_from_text("upload","out.txt","Test to write")

    # All of the following also fail
    #blob_service.create_container('images')
    #blob_service.put_blob("upload","testme.txt","foo","BlockBlob")

    #queue_service = QueueService(account_name, account_key)
    #queue_service.create_queue('taskqueue')

    # Return value must be of a sequence of pandas.DataFrame
    return dataframe1,

这是新的错误日志:

Error 0085: The following error occurred during script evaluation, please view the output log for more information:
---------- Start of error message from Python interpreter ----------
data:text/plain,C:\pyhome\lib\site-packages\requests\packages\urllib3\util\ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
Caught exception while executing function: Traceback (most recent call last):   
  File "C:\server\invokepy.py", line 169, in batch
    odfs = mod.azureml_main(*idfs)
  File "C:\temp\azuremod.py", line 44, in azureml_main
    blob_service.put_blob("upload","testme.txt","foo","BlockBlob")
  File ".\Script Bundle\site-packages\azure\storage\blob\blobservice.py", line 883, in put_blob
    self._perform_request(request)
  File ".\Script Bundle\site-packages\azure\storage\storageclient.py", line 171, in _perform_request
    resp = self._filter(request)
  File ".\Script Bundle\site-packages\azure\storage\storageclient.py", line 160, in _perform_request_worker
    return self._httpclient.perform_request(request)
  File ".\Script Bundle\site-packages\azure\storage\_http\httpclient.py", line 181, in perform_request
    self.send_request_body(connection, request.body)
  File ".\Script Bundle\site-packages\azure\storage\_http\httpclient.py", line 143, in send_request_body
    connection.send(request_body)
  File ".\Script Bundle\site-packages\azure\storage\_http\requestsclient.py", line 81, in send
    self.response = self.session.request(self.method, self.uri, data=request_body, headers=self.headers, timeout=self.timeout)
  File "C:\pyhome\lib\site-packages\requests\sessions.py", line 464, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\pyhome\lib\site-packages\requests\sessions.py", line 576, in send
    r = adapter.send(request, **kwargs)
  File "C:\pyhome\lib\site-packages\requests\adapters.py", line 431, in send
    raise SSLError(e, request=request)
SSLError: The write operation timed out

---------- End of error message from Python  interpreter  ----------
Start time: UTC 02/10/2016 15:33:00
End time: UTC 02/10/2016 15:34:18

我目前的探索主要是依赖于azure-storage 中的requests Python 包。 requests 在 Python 2.7 中有一个已知错误,用于调用更新的 SSL 协议。不确定,但我现在正在那个地区挖掘。

更新 2:此代码在 Python 3 Jupyter 笔记本中运行良好。此外,如果我让 Blob 容器对公共访问开放,我可以通过 URL 直接从容器中读取。例如:df = pd.read_csv("https://mystorageaccount.blob.core.windows.net/upload/test.csv") 可以轻松地从 blob 存储中加载文件。但是,我不能使用azure.storage.blob.BlobService 从同一个文件中读取。

更新 3:Dan 在下面的评论中建议我尝试使用托管在 Azure ML 上的 Jupyter 笔记本。我一直在本地 Jupyter 笔记本上运行它(参见上面的更新 2)。 但是,从 Azure ML Notebook 运行时会失败,并且错误再次指向 requires 包。我需要找到该包的已知问题,但根据我的阅读,已知问题是 urllib3 并且只影响 Python 2.7 而不是任何 Python 3.x 版本。这是在 Python 3.x 笔记本中运行的。咕噜。

更新 4:正如 Dan 在下面指出的,这可能是 Azure ML 网络的一个问题,因为 Execute Python Script 相对较新并且刚刚获得网络支持。但是,我还在一个完全不同的 Azure 平台上的 Azure App Service webjob 上对此进行了测试。 (它也在一个完全不同的 Python 发行版上,支持 Python 2.7 和 3.4/5,但仅支持 32 位 - 即使在 64 位机器上也是如此。)那里的代码也失败了,并带有 InsecurePlatformWarning 消息。

[02/08/2016 15:53:54 > b40783: SYS INFO] Run script 'ListenToQueue.py' with script host - 'PythonScriptHost'
[02/08/2016 15:53:54 > b40783: SYS INFO] Status changed to Running
[02/08/2016 15:54:09 > b40783: INFO] test.csv
[02/08/2016 15:54:09 > b40783: ERR ] D:\home\site\wwwroot\env\Lib\site-packages\requests\packages\urllib3\util\ssl_.py:315: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning.
[02/08/2016 15:54:09 > b40783: ERR ]   SNIMissingWarning
[02/08/2016 15:54:09 > b40783: ERR ] D:\home\site\wwwroot\env\Lib\site-packages\requests\packages\urllib3\util\ssl_.py:120: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
[02/08/2016 15:54:09 > b40783: ERR ]   InsecurePlatformWarning
[02/08/2016 15:54:09 > b40783: ERR ] D:\home\site\wwwroot\env\Lib\site-packages\requests\packages\urllib3\util\ssl_.py:120: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
[02/08/2016 15:54:09 > b40783: ERR ]   InsecurePlatformWarning

【问题讨论】:

    标签: python azure cortana-intelligence azure-machine-learning-studio


    【解决方案1】:

    前面的底线:使用 HTTP 而不是 HTTPS 访问 Azure 存储。

    在声明 BlobService 时传入 protocol='http' 以强制服务通过 HTTP 进行通信。请注意,您必须将容器配置为允许通过 HTTP 进行请求(默认情况下会这样做)。

    client = BlobService(STORAGE_ACCOUNT, STORAGE_KEY, protocol="http")

    历史和荣誉:

    我在@AzureHelps 上发布了一个关于这个主题的查询,他们在 MSDN 论坛上开了一张票:https://social.msdn.microsoft.com/Forums/azure/en-US/46166b22-47ae-4808-ab87-402388dd7a5c/trouble-writing-blob-storage-file-in-azure-ml-experiment?forum=MachineLearning&prof=required

    Sudarshan Raghunathan 用魔法回应。以下是让每个人都可以轻松复制我的修复的步骤:

    1. 下载提供所需库的 azure.zip:https://azuremlpackagesupport.blob.core.windows.net/python/azure.zip
    2. 将它们作为数据集上传到 Azure ML Studio
    3. 将它们连接到 Execute Python Script 模块上的 Zip 输入
    4. 像往常一样编写脚本,确保使用protocol='http' 创建BlobService 对象
    5. 运行实验 - 您现在应该能够写入 blob 存储。

    一些示例代码可以在这里找到:https://gist.github.com/drdarshan/92fff2a12ad9946892df

    我使用的代码如下,它不会先将 CSV 写入文件系统,而是作为文本流发送。

    from azure.storage.blob import BlobService
    
    def azureml_main(dataframe1 = None, dataframe2 = None):
        account_name = 'mystorageaccount'
        account_key='p8kSy3FACx...redacted...ebz3plQ=='
        container_name = "upload"
        json_output_file_name = 'testfromml.json'
        json_orient = 'records' # Can be index, records, split, columns, values
        json_force_ascii=False;
    
        blob_service = BlobService(account_name, account_key, protocol='http')
    
        blob_service.put_block_blob_from_text(container_name,json_output_file_name,dataframe1.to_json(orient=json_orient, force_ascii=json_force_ascii))
    
        # Return value must be of a sequence of pandas.DataFrame
        return dataframe1,
    

    一些想法:

    1. 如果默认导入 azure Python 库,我更愿意。作为 Anaconda 发行版的一部分,Microsoft 将数百个 3rd 方库导入 Azure ML。它们还应包括使用 Azure 所必需的那些。我们在 Azure 中,我们致力于 Azure。拥抱它。
    2. 我不喜欢我必须使用 HTTP 而不是 HTTPS。当然,这是 Azure 内部通信,因此可能没什么大不了的。但是,大多数文档都建议在使用 blob 存储时使用 SSL / HTTPS,所以我更愿意这样做。
    3. 我仍然在实验中遇到随机超时错误。有时 Python 代码会在几毫秒内执行,有时它会运行 60 或几秒然后超时。这使得在实验中运行它有时非常令人沮丧。但是,当作为 Web 服务发布时,我似乎没有这个问题。
    4. 我希望本地代码的体验更接近 Azure ML。在本地,我可以使用 HTTPS 并且永远不会超时。它速度极快,而且易于编写。但迁移到 Azure ML 实验意味着几乎每次都需要进行一些调试。

    非常感谢来自 Microsoft 的 Dan、Peter 和 Sudarshan,感谢他们帮助解决了这个问题。非常感谢!

    【讨论】:

      【解决方案2】:

      你走的是正确的道路。 Execution Python Script 模块旨在满足这样的自定义需求。您真正的问题是如何导入现有的 Python 脚本模块。完整的方向可以在这里找到,但我会总结一下。

      您需要使用 Azure Python SDK 并将其压缩、上传,然后导入到您的模块中。我可以看看为什么默认情况下不存在...

      https://azure.microsoft.com/en-us/documentation/articles/machine-learning-execute-python-scripts/

      导入现有 Python 脚本模块

      许多数据科学家的一个常见用例是将现有 Python 脚本合并到 Azure 机器学习实验中。执行 Python 脚本模块不是将所有代码连接并粘贴到单个脚本框中,而是接受第三个输入端口,包含 Python 模块的 zip 文件可以连接到该输入端口。然后执行框架在运行时解压缩该文件,并将内容添加到 Python 解释器的库路径中。然后 azureml_main 入口点函数可以直接导入这些模块。

      例如,考虑包含简单“Hello, World”函数的文件 Hello.py。

      图 4. 用户定义的函数。

      接下来,我们可以创建一个包含 Hello.py 的文件 Hello.zip:

      图 5. 包含用户定义的 Python 代码的 Zip 文件。

      然后,将此作为数据集上传到 Azure 机器学习工作室。如果我们然后创建并运行一个简单的实验 a 使用该模块:

      图 6. 用户定义的 Python 代码作为 zip 文件上传的示例实验。

      模块输出显示 zip 文件已解包,并且函数 print_hello 确实已运行。 图 7. Execute Python Script 模块中使用的用户定义函数。

      【讨论】:

      • 丹,感谢您的回复。这是使用 zip 文件进行设置的一个很好的介绍,但仅提示如何解决无法有效导入 azure.storage.blob 的核心问题。我确实从 Github 获取了实际代码并将其引入,以便我可以参考它。这成功地允许我引用 azure.storage.blob,但是,这还不够,因为所有请求都超时。我将在对我的原始帖子的评论中进一步解决它。但我真的想说谢谢,丹。这非常有帮助,特别是对于那些稍后跟进的类似问题的人。
      • 我没有看到问题的更新...但是您尝试上传 blob 的容器是否存在?如果容器不存在,您的代码将不会创建容器。可能需要添加blob_service.create_container('mycontainer')?希望这可能会有所帮助。
      • 再次感谢,丹。我现在更新了问题,并更清楚地指出了这些问题。容器存在,我什至无法从 Azure ML 创建容器。我希望我的更新能让它更清楚。再次感谢您的工作。我喜欢 Azure,也喜欢 Python。我真的希望能够在 Azure PaaS 和 SaaS 服务上使用 Python!
      • 你能在你的工作空间中创建一个新的 python 3 笔记本,并在笔记本中运行你的代码吗?我正在检查这是否是Execute Python Script 的网络 I/O 问题,最好告诉与我交谈的人我们知道您的代码是在笔记本上工作的。谢谢
      • 丹,这是 HTTP 与 HTTPS。我发布了解决方案。谢谢你的帮助。我仍然喜欢 Azure 中对 SSL 的支持,但这实际上只是一个挑剔的问题,因为我们都在您非常安全的网络中。我无法告诉你我是多么感谢你在这方面的所有帮助。这让我很沮丧,而您的帮助确实让我朝着正确的方向前进,并激发了我继续排除故障的热情。
      【解决方案3】:

      据我所知,您可以通过提供给第三个输入的 zip 文件使用其他包。 Azure ML 中 Python 模板脚本中的 cmets 说:

      如果连接到第三个输入端口的 zip 文件,则在“.\Script Bundle”下解压缩。该目录被添加到 sys.path。因此,如果您的 zip 文件包含 Python 文件 mymodule.py,您可以使用以下命令导入它: 导入我的模块

      所以您可以通过点击New,点击Dataset,然后选择From local fileazure-storage-python打包为一个zip文件,然后Zip 文件 选项可将 ZIP 文件上传到您的工作区。

      作为参考,您可以在文档Execute Python ScriptHow to Use Execute Python Script 部分查看更多信息。

      【讨论】:

      • 谢谢,彼得。我更新了我的帖子以包含我在使用该功能导入任意 Python 代码时遇到的问题。希望这能让它更清楚。然而,我质疑的一件事是,为什么微软不简单地将 Azure Python 包作为默认分发的一部分包含在内?似乎很奇怪。有点讽刺的是,微软构建的 azureml Python 包并未包含在 Azure ML 实验中……:-D Azure ML 将 azureml 包视为 3rd 方包!
      • 来自 python 模块的网络访问是较新的,这就是为什么它以前不包括在内的原因......我试图把它放进去:-)
      • 丹,我可以很容易地通过网络联系。事实上,我可以使用requests 来访问其他网址。只是访问似乎被阻止的 Azure 存储帐户(可能还有其他 azure 资源)。但我同意它看起来很像一个网络问题。然而,作为一种实践,我认为azure-storage 和其他特定于 azure 的包应该由 Microsoft 默认加载。您加载了数百个其他 3rd 方包以方便使用,为什么不自己加载?
      • 彼得,感谢您的回答和帮助。最终不支持 HTTPS,这是默认设置。如果您认为答案还可以,您介意点个赞吗?
      • @StevenBorg 没关系。
      猜你喜欢
      • 2017-11-18
      • 2023-01-30
      • 2020-06-03
      • 2021-12-18
      • 1970-01-01
      • 2017-09-16
      • 2019-10-14
      • 2021-11-19
      • 2019-01-15
      相关资源
      最近更新 更多