从 Azure ML 实验中访问 Azure Blob 存储

2024-05-23

Azure ML 实验提供了通过以下方式读取 CSV 文件并将其写入 Azure Blob 存储的方法:Reader and Writer模块。但是,我需要将 JSON 文件写入 blob 存储。由于没有模块可以执行此操作,因此我尝试在Execute Python Script module.

# 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-storageAzure ML 上未安装 Python 包。

如何从 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 目录中。然后,我按照下面 Dan 的注释,压缩了 site-packages 文件夹并作为 Zip 文件上传。然后,我包含了对 site-packages 目录的引用并成功导入了所需的项目。这导致写入博客存储时出现超时错误。

这是我的代码:

# 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

我目前的探索的领先之处在于,存在对requestsPython 包在azure-storage. requestsPython 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 在下面的评论中建议我尝试使用 Jupyter 笔记本托管在 Azure ML 上。我一直在本地 Jupyter 笔记本上运行它(请参阅上面的更新 2)。However,从 Azure ML Notebook 运行时失败,并且错误指向requires再次打包。我需要找到该包的已知问题,但根据我的阅读,已知问题与 urllib3 相关,并且仅影响 Python 2.7,而不影响任何 Python 3.x 版本。这是在 Python 3.x 笔记本中运行的。咕噜。

更新 4:正如 Dan 在下面指出的那样,这可能是 Azure ML 网络的问题,因为Execute Python Script相对较新,刚刚获得网络支持。不过,我还在 Azure 应用服务 Web 作业上对此进行了测试,该作业位于完全不同的 Azure 平台上。 (它也位于完全不同的 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

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

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

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

历史及信用:

我向 @AzureHelps 发布了有关此主题的查询,他们在 MSDN 论坛上开了一张票:

苏达山·拉古纳坦以魔法回应。以下是让每个人都可以轻松复制我的修复的步骤:

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

一些示例代码可以在这里找到:https://gist.github.com/drdarshan/92fff2a12ad9946892df 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 将数百个第 3 方库导入到 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,感谢他们帮助解决了这个问题。我非常感激!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 Azure ML 实验中访问 Azure Blob 存储 的相关文章

  • Python - 定义常量列表或字典的最佳/最简洁的方法

    第一次使用堆栈溢出 我很高兴来到这里 简介 我最近开始了 Python 编程世界的神奇冒险 我喜欢它 现在 在我从 C 语言的尴尬过渡中 一切都进展顺利 但我在创建与标头文件 h 同义的内容时遇到了麻烦 问题 我有中等大小的字典和列表 大约
  • 如何使用 python 从嵌套表结构中识别最终父级?

    我有下表 我的问题是 我如何以编程方式识别最终父级 以下是通过示例解释的规则 the id 5 0的父母是51 0 身份证号51 0没有父母 因此 id5 0的最终父级是51 0 the id 6 0的父母是1 0 身份证号1 0的父母是1
  • 为什么具有复杂无穷大的 NumPy 运算会导致有趣的结果?

    我注意到复杂的无穷大的有趣结果 In 1 import numpy as np In 2 np isinf 1j np inf Out 2 True In 3 np isinf 1 1j np inf Out 3 True In 4 np
  • 使用 pygtk3 将 GUI 窗口添加到 python opencv2 程序

    我已经使用Python和Opencv2完成了一个程序 现在 我想向我的程序添加一个 GUI 窗口 我对 PyGtk3 有一些经验 因此 我修改了代码以采用 PyGtk3 但是 我遇到了错误 因此 我尝试了一个简单的程序来找出实际的错误 我的
  • 使用opencv+picamera流IO用树莓派捕获视频

    我使用 Raspberry 来简单地显示一个视频 目前仅此 为此 我必须使用 opencv cv2 我尝试了很多解决方案 但现在我想使用 Picamera 库捕获视频 我将向您展示我的代码 import io import time imp
  • 熊猫 style.background_gradient 忽略 NaN

    我有以下代码来转储数据帧results到 HTML 表格中 这样的列TIME FRAMES根据seaborn 的颜色图进行着色 import seaborn as sns TIME FRAMES 24h 7d 30d 1y Set CSS
  • 不使用 graphviz/web 可视化决策树

    由于某些限制 我无法使用 graphviz webgraphviz com 可视化决策树 工作网络与另一个世界是封闭的 问题 是否有一些替代实用程序或一些 Python 代码用于至少非常简单的可视化可能只是决策树的 ASCII 可视化 py
  • 自定义信号的声明

    在 Qt 中 我们可以通过将自定义信号设为静态变量来创建它们 然后我们使用self signame反而classname signame 这样就在类中创建了一个实例变量 我想了解这种模式之外的理论 这是我尝试过的一些伪代码 这些伪代码已记录
  • Pythoncom - 将相同的 COM 对象传递给多个线程

    你好 对于 COM 对象 我是一个完全的初学者 非常感谢任何帮助 我正在开发一个Python程序 该程序应该以客户端 服务器的方式读取传入的MS Word文档 即客户端发送一个请求 一个或多个MS Word文档 服务器使用pythoncom
  • 使用 Twisted Python 的 UDP 客户端和服务器

    我想创建一个服务器和客户端 使用 Twisted 从网络发送和接收 UDP 数据包 我已经用 Python 中的套接字编写了此代码 但想利用 Twisted 的回调和线程功能 然而 我需要 Twisted 设计方面的帮助 我想接收多种类型的
  • Tensorflow 到 ONNX 的转换

    我目前正在尝试转换我使用本教程创建的已保存 且正在工作 的 pb 文件 https github com thtrieu darkflow https github com thtrieu darkflow 到 onnx 文件中 我目前正在
  • 使用 shell=True 将 PATH 设置为 bitbake 的“source”在 Python 中没有效果

    下面是shell脚本中的代码 source proj common tools repo etc profile d repo sh repo project init branch repo project sync source pok
  • python 和 android 中通过 AES 算法加密和解密

    我有用于 AES 加密的 python 和 android 代码 当我在android中加密文本时 它在python上成功解密 但无法在android端解密 有人有想法吗 Python代码 import base64 import hash
  • 如何使用 Python Pandas 制作 DataFrame 切片并在特定切片中“fillna”?

    问题 让我们从 Kaggle 获取泰坦尼克号数据集 我有包含 Pclass 性别 和 年龄 列的数据框 我需要用特定组的中位数填充 年龄 列中的 NaN 如果是来自一等的女性 我想用一等女性的中位数填写她的年龄 而不是整个年龄列的中位数 问
  • python 中的异步编程

    python 中有异步编程的通用概念吗 我可以为一个函数分配一个回调 执行它并立即返回主程序流 无论该函数的执行需要多长时间吗 您所描述的 主程序流程在另一个函数执行时立即恢复 不是通常所说的 异步 又名 事件驱动 编程 而是 多任务 又名
  • Python:如何使用生成器来避免 sql 内存问题

    我有以下方法来访问 mysql 数据库 并且查询在服务器中执行 我无权更改有关增加内存的任何内容 我对生成器很陌生 并开始阅读更多有关它的内容 并认为我可以将其转换为使用生成器 def getUNames self globalUserQu
  • 如何在 Flask 中获取 POSTed JSON?

    我正在尝试使用 Flask 构建一个简单的 API 现在我想在其中读取一些 POSTed JSON 我使用 Postman Chrome 扩展进行 POST 我 POST 的 JSON 很简单 text lalala 我尝试使用以下方法读取
  • Azure 不会覆盖应用程序设置

    在我的 Azure 应用服务中 我想更新应用程序设置 但从 VS 发布后 密钥不会覆盖本地 Web config 中的值 In Azure it looks like 在结果中 我在 azure 上的 web config 包含来自本地设置
  • 如何避免在附加字符串连接器逻辑应用程序中添加重复字符串

    我有以下 json 数组输入 results tableName ABC id 11 tableName ZX id 11 tableName ABC id 11 In logic app i have used in For each I
  • 为什么 Python exec 中的模块级变量无法访问?

    我正在尝试使用Pythonexec in a project https github com arjungmenon pypage执行嵌入的Python代码 我遇到的问题是在模块级 in an exec声明是难以接近的来自同一模块中定义的

随机推荐