【发布时间】:2020-04-06 00:57:37
【问题描述】:
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)
响应:{“消息”:“文件上传失败并出现错误 - 使用 'idna' 编解码器编码失败(UnicodeError:标签太长)”}
请帮忙!
【问题讨论】:
标签: python django azure azure-storage azure-blob-storage