[英]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.