简体   繁体   English

Azure Blob 使用 Python

[英]Azure Blob Using Python

I am accessing a website that allows me to download CSV file.我正在访问一个允许我下载 CSV 文件的网站。 I would like to store the CSV file directly to the blob container.我想将 CSV 文件直接存储到 blob 容器中。 I know that one way is to download the file locally and then upload the file, but I would like to skip the step of downloading the file locally.我知道一种方法是在本地下载文件然后上传文件,但我想跳过在本地下载文件的步骤。 Is there a way in which I could achieve this.有没有办法可以实现这一目标。

i tried the following:我尝试了以下方法:

block_blob_service.create_blob_from_path('containername','blobname','https://*****.blob.core.windows.net/containername/FlightStats',content_settings=ContentSettings(content_type='application/CSV'))

but I keep getting errors stating path is not found.但我不断收到错误提示找不到路径。 Any help is appreciated.任何帮助表示赞赏。 Thanks!谢谢!

The file_path in create_blob_from_path is the path of your local file, looks like "C:\xxx\xxx". create_blob_from_path中的 file_path 是本地文件的路径,看起来像“C:\xxx\xxx”。 This path( 'https://*****.blob.core.windows.net/containername/FlightStats' ) is Blob URL.此路径( 'https://*****.blob.core.windows.net/containername/FlightStats' )是 Blob URL。

You could download your file to byte array or stream, then use create_blob_from_bytes or create_blob_from_stream method.您可以将文件下载到字节数组或 stream,然后使用create_blob_from_bytescreate_blob_from_stream方法。

Other answer uses the so called "Azure SDK for Python legacy ".其他答案使用所谓的“Azure SDK for Python legacy ”。


I recommend that if it's fresh implementation then use Gen2 Storage Account (instead of Gen1 or Blob storage).我建议如果它是新的实现,那么使用 Gen2 存储帐户(而不是 Gen1 或 Blob 存储)。

For Gen2 storage account, see example here :对于 Gen2 存储帐户,请参见此处的示例

from azure.storage.filedatalake import DataLakeFileClient

data = b"abc"
file = DataLakeFileClient.from_connection_string("my_connection_string",
                                                 file_system_name="myfilesystem", file_path="myfile")

file.append_data(data, offset=0, length=len(data))
file.flush_data(len(data))

It's painful, if you're appending multiple times then you'll have to keep track of offset on client side.这很痛苦,如果您要多次附加,那么您将不得不跟踪客户端的offset

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM