问题描述
我正在使用下面的python代码将文件保存到本地文件夹中.我想将此文件直接保存到Azure Blob.我不希望将文件存储在本地然后上传到Blob.
I am using below python code to save the file into local folder. I want to save this file into Azure Blob directly. I do not want file to be stored locally and then upload into blob.
我尝试在文件夹变量中提供blob位置,但是它不起作用.我有一个要从Web浏览器读取的excel文件,并使用python保存到Azure blob.
I tried giving blob location in folder variable but it did not work. I have excel file that I want to read from Web browser and saved into Azure blobs using python.
folder = 'Desktop/files/ab'
r = requests.get(api_end_point, headers=api_headers, stream=True)
with open(folder, 'wb') as f:
f.write(r.content)
推荐答案
首先,您应该像流一样获取文件.
First you should get the files as something like stream.
import os
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
connect_str = os.getenv('str')
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_name = "test"
container_client = blob_service_client.get_container_client(container_name)
blob_client = blob_service_client.get_blob_client(container_name, "MyFirstBlob.txt")
blob_client.upload_blob(req.get_body(), blob_type="BlockBlob")
在我这边,我将数据放入请求的正文中,然后将其上传到蔚蓝的Blob中.它是流.您还可以在其中放入流.
On my side, I put the data in the body of request, and I upload that to azure blob. It is stream. You can also put a stream in it.
这些是官方文档:
这篇关于将文件保存到Azure Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!