Traceback (most recent call last):
File "C:\Users\AZ\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\storage\queue\__init__.py", line 8, in <module>
from ._queue_client import QueueClient
File "C:\Users\AZ\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\storage\queue\_queue_client.py", line 22, in <module>
from ._shared.base_client import StorageAccountHostsMixin, parse_connection_str, parse_query
File "C:\Users\AZ\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\storage\queue\_shared\base_client.py", line 26, in <module>
from azure.core.pipeline.transport import RequestsTransport, HttpTransport
ImportError: cannot import name 'RequestsTransport' from 'azure.core.pipeline.transport' (C:\Users\AZ\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\core\pipeline\transport\__init__.py)
Process finished with exit code 1
我有azure-core==1.31.0,azure-storage-blob==12.19.0 天蓝色存储队列==12.1.6
我已经在 base_client.py 文件中验证了它正在导入“from azure.core.pipeline.transport import RequestsTransport, HttpTransport”。还是失败了
确保项目中的文件名与您正在使用的模块名称不匹配。在这种情况下,重命名文件可以解决问题。
重新安装
Azure-Core
模块,其版本包含 RequestsTransport
以确保兼容性(或)创建新的虚拟环境并重新安装最新版本的所有必需模块。
我创建了一个 Python Azure 函数并安装了以下模块。
需求.txt:
azure-functions
azure-core
azure-storage-blob
azure-storage-queue
示例代码:
import azure.functions as func
import logging
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
from azure.storage.queue import QueueServiceClient, QueueClient
from azure.core.exceptions import AzureError
@app.route(route="http_trigger", auth_level=func.AuthLevel.ANONYMOUS)
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
try:
blob_data = req.get_body()
blob_name = "sample-blob.txt"
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
blob_client.upload_blob(blob_data, overwrite=True)
logging.info(f"Blob uploaded successfully: {blob_name}")
message = "New blob uploaded: " + blob_name
queue_client.send_message(message)
logging.info(f"Message sent to queue: {message}")
return func.HttpResponse(f"Blob uploaded and message sent: {message}", status_code=200)
except AzureError as e:
logging.error(f"Azure Error occurred: {str(e)}")
return func.HttpResponse(f"Azure error occurred: {str(e)}", status_code=500)
except Exception as e:
logging.error(f"An unexpected error occurred: {str(e)}")
return func.HttpResponse(f"An unexpected error occurred: {str(e)}", status_code=500)
输出:
[2024-11-05T08:32:35.531Z] Executing 'Functions.http_trigger' (Reason='This function was programmatically called via the host APIs.', Id=29a710d8-7306-4932-b97f-80aa0dfd01f1)
[2024-11-05T08:32:35.643Z] Request URL: 'https://storage.blob.core.windows.net/container1/sample-blob.txt'
Request method: 'PUT'
Request headers:
'x-ms-blob-type': 'REDACTED'
'Content-Length': '0'
'x-ms-version': 'REDACTED'
'Content-Type': 'application/octet-stream'
'Accept': 'application/xml'
'User-Agent': 'azsdk-python-storage-blob/12.19.0 Python/3.11.9 (Windows-10-10.0.22631-SP0)'
'x-ms-date': 'REDACTED'
'x-ms-client-request-id': '8239b614-9b50-11ef-b49d-98fa9b0d1c80'
'Authorization': 'REDACTED'
No body was attached to the request
[2024-11-05T08:32:37.190Z] Response status: 201
Response headers:
'Content-Length': '0'
'Content-MD5': 'REDACTED'
'Last-Modified': 'Tue, 05 Nov 2024 08:32:36 GMT'
'ETag': '"0x8DCFD7466B2CBD9"'
'Server': 'Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0'
'x-ms-request-id': '2bb7857f-201e-005b-215d-2f79f7000000'
'x-ms-client-request-id': '8239b614-9b50-11ef-b49d-98fa9b0d1c80'
'x-ms-version': 'REDACTED'
'x-ms-content-crc64': 'REDACTED'
'x-ms-request-server-encrypted': 'REDACTED'
'Date': 'Tue, 05 Nov 2024 08:32:35 GMT'
[2024-11-05T08:32:37.196Z] Blob uploaded successfully: sample-blob.txt
[2024-11-05T08:32:37.196Z] Request URL: 'https://storage.queue.core.windows.net/queue1/messages'
Request method: 'POST'
Request headers:
'x-ms-version': 'REDACTED'
'Content-Type': 'application/xml'
'Accept': 'application/xml'
'Content-Length': '129'
'User-Agent': 'azsdk-python-storage-queue/12.1.6 Python/3.11.9 (Windows-10-10.0.22631-SP0)'
'x-ms-date': 'REDACTED'
'x-ms-client-request-id': '832749a2-9b50-11ef-a0bc-98fa9b0d1c80'
'Authorization': 'REDACTED'
A body is sent with the request
[2024-11-05T08:32:39.035Z] Response status: 201
Response headers:
'Transfer-Encoding': 'chunked'
'Content-Type': 'application/xml'
'Server': 'Windows-Azure-Queue/1.0 Microsoft-HTTPAPI/2.0'
'x-ms-request-id': '4b284df6-5003-0076-655d-2ffa37000000'
'x-ms-version': 'REDACTED'
'Date': 'Tue, 05 Nov 2024 08:32:37 GMT'
[2024-11-05T08:32:39.046Z] Message sent to queue: New blob uploaded: sample-blob.txt
[2024-11-05T08:32:39.314Z] Executed 'Functions.http_trigger' (Succeeded, Id=29a710d8-7306-4932-b97f-80aa0dfd01f1, Duration=3796ms)
容器:
队列: