【问题标题】:Trying to move data from one Azure Blob Storage to another using a Python script尝试使用 Python 脚本将数据从一个 Azure Blob 存储移动到另一个
【发布时间】:2020-07-24 05:48:46
【问题描述】:

我在容器 A 中有以压缩格式存在的数据,我需要使用 Python 脚本对其进行转换,并尝试将其安排在 Azure 中进行,但是在将输出写入新的存储容器(容器 B)时,它只是输出一个带有文件名而不是数据的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 输出,其中单元格 A1 中的内容为“iris_setosa.csv”,而不是它读取的实际数据。

【问题讨论】:

    标签: python azure azure-data-factory


    【解决方案1】:

    更新:

    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_blobdelete_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)
    

    【讨论】:

    • 感谢 Pamela - 这看起来可以解决移动 blob 的问题,但不允许我在移动之间转换数据。对吗?
    • @Phishypenguin 它移动整个文件,包括文件中的数据。
    • @Phishypenguin 请试试我的更新。它工作正常。
    猜你喜欢
    • 2020-09-24
    • 1970-01-01
    • 2021-06-27
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    • 2021-08-25
    相关资源
    最近更新 更多