简体   繁体   English

如何使用 Python SDK 为我在 Azure 数据湖中创建的文件设置到期日期?

[英]How do I set an expiration date on a file I create in the Azure Data lake using the Python SDK?

I'm using Python 3.8 and Azure data lake Gen 2. I want to set an expiration time for a file I save on the data lake.我正在使用 Python 3.8 和 Azure 数据湖 Gen 2。我想为保存在数据湖中的文件设置过期时间。 Following this -- azure.datalake.store.core.AzureDLFileSystem class |继此 -- azure.datalake.store.core.AzureDLFileSystem 类 | Microsoft Docs, I tried the below Microsoft Docs,我尝试了以下

            file_client = directory_client.create_file(filename)
            file_client.upload_data(
                data,
                overwrite=True
            )
            ts = time.time() + 100
            file_client.set_expiry(path=path, expire_time=ts)

but am getting the error但我收到错误

AttributeError: 'DataLakeFileClient' object has no attribute 'set_expiry'

What's the proper way to set an expiration time when creating a file on the data lake?在数据湖上创建文件时设置过期时间的正确方法是什么?

The reason for your error, is that you appear to be attempting to call a method belonging to azure.datalake.store.core.AzureDLFileSystem on an object of type DataLakeFileClient .之所以你的错误,就是你似乎试图调用属于方法azure.datalake.store.core.AzureDLFileSystem类型的对象上DataLakeFileClient This is why you get the error!这就是您收到错误的原因! The method does not exist for objects of type DataLakeFileClient .对于DataLakeFileClient类型的对象,该方法不存在。

If you wish to call the method for set_expiry, you must first create the correct kind of object.如果您希望调用 set_expiry 的方法,您必须首先创建正确类型的对象。

For example in Gen1, create the object first as described here:例如,在 Gen1 中,首先按照此处所述创建对象:

https://docs.microsoft.com/en-us/azure/data-lake-store/data-lake-store-data-operations-python https://docs.microsoft.com/en-us/azure/data-lake-store/data-lake-store-data-operations-python

## Declare variables
subscriptionId = 'FILL-IN-HERE'
adlsAccountName = 'FILL-IN-HERE'

## Create a filesystem client object
adlsFileSystemClient = core.AzureDLFileSystem(adlCreds, store_name=adlsAccountName)

Using this object, you can call使用这个对象,你可以调用

adlsFileSystemClient exactly like how you have in your code example. adlsFileSystemClient 与您在代码示例中的完全一样。

set_expiry(path, expiry_option, expire_time=None)

Just make sure you're trying to call methods on the correct type of object.只要确保您尝试对正确类型的对象调用方法即可。

For Gen 2:对于第 2 代:

from azure.storage.filedatalake import DataLakeServiceClient
datalake_service_client = DataLakeServiceClient.from_connection_string(self.connection_string)

# Instantiate a FileSystemClient
file_system_client = datalake_service_client.get_file_system_client("mynewfilesystem")

For Gen2, you need to set a blob to expire as follows: https://docs.microsoft.com/en-us/azure/storage/blobs/storage-lifecycle-management-concepts?tabs=azure-portal#expire-data-based-on-age对于 Gen2,您需要将 blob 设置为过期,如下所示: https : //docs.microsoft.com/en-us/azure/storage/blobs/storage-lifecycle-management-concepts?tabs= azure-portal# expire-基于年龄的数据

Expire data based on age根据年龄使数据过期

Some data is expected to expire days or months after creation.某些数据预计会在创建后数天或数月到期。 You can configure a lifecycle management policy to expire data by deletion based on data age.您可以配置生命周期管理策略以根据数据年龄通过删除使数据过期。 The following example shows a policy that deletes all block blobs older than 365 days.以下示例显示了删除所有早于 365 天的块 blob 的策略。

{
  "rules": [
    {
      "name": "expirationRule",
      "enabled": true,
      "type": "Lifecycle",
      "definition": {
        "filters": {
          "blobTypes": [ "blockBlob" ]
        },
        "actions": {
          "baseBlob": {
            "delete": { "daysAfterModificationGreaterThan": 365 }
          }
        }
      }
    }
  ]
}

暂无
暂无

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

相关问题 如何使用 Python SDK 从 Azure 数据湖中删除文件? - How do you delete a file from an Azure Data Lake using the Python SDK? 对于 Python 3.8 Azure 数据湖 Gen 2,如何检查文件系统上是否存在文件? - For Python 3.8 Azure data lake Gen 2, how do I check if a file exists on a filesystem? 如何使用 python 从 Azure Data Lake Gen 2 读取文件 - How can i read a file from Azure Data Lake Gen 2 using python 如何创建/连接到 Docker Azure 服务总线(使用 Python SDK)? - How do I create/connect to a Docker Azure service bus (using Python SDK)? 如何在 python 上使用 presto 连接到 Azure 数据湖存储? - How to connect to Azure data lake storage using presto on python? 如何创建可在Python中将数据流式传输到的文件? - How do I create a file I can stream data to in Python? 如何使用来自Azure文件共享的多个线程将数据复制到Azure Data Lake存储? - How to copy data to Azure Data Lake store using multiple threads from azure file share? Azure Data Lake Storage Gen2 创建目录(如果 python 中不存在) - Azure Data Lake Storage Gen2 create directory if not exists in python 当您知道文件类型但不知道名称时,如何从 Azure Data Lake 下载文件? - How do you download a file from Azure Data Lake when you know the type of the file but not the name? 从存储帐户(Azure Data lake)读取 pdf 文件,无需使用 python 下载 - Read pdf file from storage account (Azure Data lake) without downloading it using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM