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-storage
Azure 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
我目前的探索的领先之处在于,存在对requests
Python 包在azure-storage
. 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 在下面的评论中建议我尝试使用 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