[英]Python: How to move or copy Azure Blob from one container to another
[英]Trying to move data from one Azure Blob Storage to another using a Python script
I have data that exists in a zipped format in container A that I need to transform using a Python script and am trying to schedule this to occur within Azure, but when writing the output to a new storage container (container B), it simply outputs一個 csv 里面的文件名而不是數據。
我已經完全按照微軟網站上給出的教程進行操作,但我無法讓它工作——我錯過了什么?
https://docs.microsoft.com/en-us/azure/batch/tutorial-run-python-batch-azure-data-factory
file_n='iris.csv'
# Load iris dataset from the task node
df = pd.read_csv(file_n)
# Subset records
df = df[df['Species'] == "setosa"]
# Save the subset of the iris dataframe locally in task node
df.to_csv("iris_setosa.csv", index = False, encoding="utf-8")
# Upload iris dataset
blobService.create_blob_from_text(containerName, "iris_setosa.csv", "iris_setosa.csv")
具體來說,最后一行似乎只是給了我一個名為“iris_setosa.csv”的 csv 的 output,單元格 A1 中的內容為“iris_setosa.csv”,而不是它讀取的實際數據。
更新:
將create_blob_from_text
替換為create_blob_from_path
。
create_blob_from_text
從 str/unicode 創建一個新的 blob,或更新現有 blob 的內容。 因此,您會在新 blob 的內容中找到文本iris_setosa.csv 。
create_blob_from_path
從文件路徑創建新 blob,或更新現有 blob 的內容。 這是你想要的。
此解決方法使用copy_blob
和delete_blob
將Azure Blob 從一個容器移動到另一個容器。
from azure.storage.blob import BlobService
def copy_azure_files(self):
blob_service = BlobService(account_name='account_name', account_key='account_key')
blob_name = 'iris_setosa.csv'
copy_from_container = 'test-container'
copy_to_container = 'demo-container'
blob_url = blob_service.make_blob_url(copy_from_container, blob_name)
# blob_url:https://demostorage.blob.core.windows.net/test-container/iris_setosa.csv
blob_service.copy_blob(copy_to_container, blob_name, blob_url)
#for move the file use this line
blob_service.delete_blob(copy_from_container, blob_name)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.