gpt4 book ai didi

python - 当我调用以下 django api 代码将文件上传到 Azure Blob 存储时,出现 "encoding with ' idna ' codec failed (UnicodeError: label too long)"

转载 作者:行者123 更新时间:2023-12-03 05:00:24 24 4
gpt4 key购买 nike

import os

from azure.storage.blob import BlockBlobService, baseblobservice
from django.http import JsonResponse
from rest_framework.decorators import api_view


@api_view(['GET', 'POST'])
def upload_to_blob(request):
try:
container_name = 'xyzxyzxyz'
connection_string = "DefaultEndpointsProtocol=https;AccountName=dapblobstorage;AccountKey=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefgh==;"
local_file_path = "D:\Work\\uploadToBLOB API\\test\\a4.csv"
upload_location = "kuldeep/api"

block_blob_service = BlockBlobService(connection_string)

if os.path.exists(local_file_path):
block_blob_service.create_blob_from_path(container_name=container_name
, blob_name=(upload_location.strip('/') + '/' + os.path.basename(local_file_path))
, file_path=local_file_path)

return JsonResponse({'message': f'{os.path.basename(local_file_path)} uploaded successfully!'}, status=200, safe=False)
except Exception as err:
loggers.error(err, exc_info=True)
return JsonResponse({'message': f'File upload failed with error - {err}'}, status=500, safe=False)

响应:{“message”:“文件上传失败,出现错误 - 使用“idna”编解码器进行编码失败(UnicodeError:标签太长)”}

请帮忙!

最佳答案

您的问题是由错误的代码 block_blob_service = BlockBlobService(connection_string) 引起的。

根据azure-storage-python/azure-storage-blob/azure/storage/blob/blockblobservice.py源码中的构造函数BlockBlobService ,如下图,

enter image description here

如果您想使用 Azure 存储的连接字符串创建 BlockBlobService 对象,则应将 connection_string 变量的值传递给其参数 connection_string 如下面的代码。

block_blob_service = BlockBlobService(connection_string=connection_string)

如果不是,connection_string 变量的值将作为 account_name 参数的值并导致问题 https://bugs.python.org/issue32958打开套接字时将其用于 Azure 存储的 url。

同时,请在local_file_pathD:\处添加反斜杠以使其正确,避免被用作W的转义字符Work,或者在路径中使用斜杠 / 而不是双反斜杠 \\D:/Work/uploadToBLOB API/test/a4.csv 与 Windows 上的 D:\\Work\\uploadToBLOB API\\test\\a4.csv 相同。

关于python - 当我调用以下 django api 代码将文件上传到 Azure Blob 存储时,出现 "encoding with ' idna ' codec failed (UnicodeError: label too long)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59307892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com