python - 从 VS Code 启动的 Azure Functions 出现 ssl 错误

标签 python ssl visual-studio-code azure-functions

我在 VS Code 中创建了一个 Azure 函数 - HTTP 触发器,但我遇到了与 SSL 相关的错误。
总而言之,我希望该函数从一个 blob 存储容器(works)中读取一个 csv 文件,然后生成另一个 .txt 文件并将其上传到另一个 blob 存储容器(这部分似乎会产生 ssl 错误)。
似乎无法找到问题的根源,有人知道如何解决它吗?
python 代码:

import pandas as pd
from datetime import datetime, timedelta
import logging
import azure.functions as func
from azure.storage.blob import BlobServiceClient, generate_container_sas, ContainerSasPermissions, generate_blob_sas, BlobSasPermissions, ContainerClient
from azure.core.exceptions import HttpResponseError, ResourceExistsError
import time
import os
import uuid

# using generate_container_sas
def get_img_url_with_container_sas_token(account_name,container_name,account_key):
    container_sas_token = generate_container_sas(
        account_name=account_name,
        container_name=container_name,
        account_key=account_key,
        permission=ContainerSasPermissions(read=True),
        expiry=datetime.utcnow() + timedelta(hours=1)
    )
    return container_sas_token

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = "xxxxx"
    key =  "xxxxx"
    raw_container = "raw"
    blob_name = 'employee.csv'

    gen_token = get_img_url_with_container_sas_token(name,raw_container,key)
    INPUTSTORAGEACCOUNTURLSAS = f"https://{name}.blob.core.windows.net/{raw_container}/{blob_name}?{gen_token}"

    #df = pd.read_csv(INPUTSTORAGEACCOUNTURLSAS,delimiter=',')

    connect_str ='xxxxx'
    # Create a file in local data directory to upload and download 
    local_file_name = "quickstart" + str(uuid.uuid4()) + ".txt"
    upload_file_path = "xxxxx" + local_file_name

    # Write text to the file
    file = open(upload_file_path, 'w')
    file.write("Hello, World!")
    file.close()

    container_name = 'proccessed'
   # Create a blob client using the local file name as the name for the blob
    blob_service_client = BlobServiceClient.from_connection_string(connect_str)
    blob_client = blob_service_client.get_blob_client(container=container_name, blob=local_file_name)

    print("\nUploading to Azure Storage as blob:\n\t" + local_file_name)

    # Upload the created file
    with open(upload_file_path, "rb") as data: 
        #print(data)
        blob_client.upload_blob(data) 

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )
错误导致:
Executed 'Functions.ecr_xml_to_dl' (Failed, Id=7190b60c-4f2c-4c14-ad3c-b76735405953, Duration=96214ms)
[2020-12-07T12:27:42.443Z] System.Private.CoreLib: Exception while executing function: Functions.ecr_xml_to_dl. System.Private.CoreLib: Result: Failure
Exception: ServiceRequestError: [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:852)
Stack:   File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.6/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 349, in _handle__invocation_request
    self.__run_sync_func, invocation_id, fi.func, args)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\concurrent\futures\thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.6/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 511, in __run_sync_func
    return func(**params)
  File "C:\Users\USER\Documents\Work\Coding\Azure_Test_Function\ecr_xml_to_dl\__init__.py", line 58, in main
    blob_client.upload_blob(data)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\core\tracing\decorator.py", line 83, in wrapper_use_tracer
    return func(*args, **kwargs)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\storage\blob\_blob_client.py", line 539, in upload_blob
    return upload_block_blob(**options)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\storage\blob\_upload_helpers.py", line 106, in upload_block_blob
    **kwargs)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\storage\blob\_generated\operations\_block_blob_operations.py", line 217, in upload
    pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\core\pipeline\_base.py", line 211, in run
    return first_node.send(pipeline_request)  # type: ignore
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\core\pipeline\_base.py", line 71, in send
    response = self.next.send(request)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\core\pipeline\_base.py", line 71, in send
    response = self.next.send(request)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\core\pipeline\_base.py", line 71, in send
    response = self.next.send(request)
  [Previous line repeated 2 more times]
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\core\pipeline\policies\_redirect.py", line 157, in send
    response = self.next.send(request)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\core\pipeline\_base.py", line 71, in send
    response = self.next.send(request)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\storage\blob\_shared\policies.py", line 515, in send
    raise err
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\storage\blob\_shared\policies.py", line 489, in send
    response = self.next.send(request)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\core\pipeline\_base.py", line 71, in send
    response = self.next.send(request)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\core\pipeline\_base.py", line 71, in send
    response = self.next.send(request)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\core\pipeline\_base.py", line 71, in send
    response = self.next.send(request)
  [Previous line repeated 1 more time]
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\storage\blob\_shared\policies.py", line 290, in send
    response = self.next.send(request)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\core\pipeline\_base.py", line 71, in send
    response = self.next.send(request)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\core\pipeline\_base.py", line 71, in send
    response = self.next.send(request)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\core\pipeline\_base.py", line 103, in send
    self._sender.send(request.http_request, **request.context.options),
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\storage\blob\_shared\base_client.py", line 319, in send
    return self._transport.send(request, **kwargs)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python36\lib\site-packages\azure\core\pipeline\transport\_requests_basic.py", line 285, in send
    raise error

最佳答案

根据一些测试,您的代码似乎没有问题,代码在我这边工作正常。
对于您提供的错误消息,它似乎是由您的互联网连接引起的。如果您的计算机使用代理连接到互联网,它可能会显示类似的问题。因此,请检查您的网络是否使用任何代理或您的计算机中是否打开了 fiddler。请更换另一台具有公共(public)互联网的计算机以使用相同的代码进行测试。

关于python - 从 VS Code 启动的 Azure Functions 出现 ssl 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65183165/

相关文章:

python - 如何运行协程但不等待它?

python - 为什么 Python 用于 Web 开发?

curl: (60) Peer 的证书颁发者已被用户标记为不信任

ssl - elixir websocket 库中的高级 SSL 选项

c# - 具有使用 SSL 的上下文的 UserPrincipal

typescript - VS Code在Vue + TypeScript项目中找不到模块,WebStorm没有问题

F# System.TypeLoadException

python - 无法安装任何库[SSL:TLSV1_ALERT_PROTOCOL_VERSION]

python - Django : python manage. py runserver(多个错误)

powershell - 在 VSCode 的 PowerShell 命令提示符中隐藏完整文件路径