简体   繁体   English

使用 python 条件删除 Azure blob 存储

[英]Conditional Delete Azure blob storage using python

Im trying to create a script where it delete all the file in the blob when the creation date of the file > 15 days.我正在尝试创建一个脚本,当文件的创建日期 > 15 天时,它会删除 blob 中的所有文件。 i'm able to delete the blob using the code below but i coudn't find a way to access creation date from azure and then delete the file我可以使用下面的代码删除 blob,但我找不到从 azure 访问创建日期然后删除文件的方法

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
storage_account_key = "KEY"
storage_account_name = "STORAGE NAME"
connection_string = "connection STRING "
container_name = "Connection String"
blob_name="blob storage
container_client = ContainerClient.from_connection_string(conn_str=connection_string, 
container_name=container_name)
container_client.delete_blob(blob=blob_name)

What you need to do is get the blob properties using BlobClient.get_blob_properties() method.您需要做的是使用BlobClient.get_blob_properties()方法获取 blob 属性。 Once you get the properties , you can find the blob's created date/time in creation_time property.获得properties后,您可以在creation_time属性中找到 blob 的创建日期/时间。

Your code would be something like:您的代码将类似于:

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
storage_account_key = "KEY"
storage_account_name = "STORAGE NAME"
connection_string = "connection STRING "
container_name = "Connection String"
blob_name="blob storage
container_client = ContainerClient.from_connection_string(conn_str=connection_string, 
container_name=container_name)
blob_client = container_client.get_blob_client(blob=blob_name)
blob_properties = blob_client.get_blob_properties()
#Check for creation time and then delete blob if needed
if blob_properties.creation_time > 'some date time value'
  blob_client.delete_blob()

Adding to @Gaurav Mantri, Below is the code where you can check if the creation date is greater than 15 days and delete if it is.添加到@Gaurav Mantri,下面是您可以检查创建日期是否大于 15 天并删除的代码。

blob_client = container_client.get_blob_client(blob=blob_name)

blob_properties = blob_client.get_blob_properties()

creation_time=str(blob_properties.creation_time)
d1 = creation_time[0:creation_time.index(" ")]
today = str(datetime.today())
d2 = creation_time[0:today.index(" ")]
d1 = datetime.strptime(d1, "%Y-%m-%d")
d2 = datetime.strptime(d2, "%Y-%m-%d")
Threshold=abs((d2 - d1).days)

if Threshold==0:
    blob_client.delete_blob()
    print(blob_name +" got deleted")

RESULTS:结果:

在此处输入图像描述

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

相关问题 Azure Python 下载存储 blob 返回“不满足使用 HTTP 条件标头指定的条件。” - Azure Python download storage blob returns 'The condition specified using HTTP conditional header(s) is not met.' 使用 python 将文件上传到 azure blob 存储 - Uploading file to azure blob storage using python 使用 Python 的 azure httptrigger blob 存储 - azure httptrigger blob storage using Python 使用 python 将图像上传到 azure blob 存储 - Upload image to azure blob storage using python 如何在 Python 中使用 Azure Functions 的 Azure Blob 存储绑定将 JSON 数据上传到 Azure 存储 blob - How to upload JSON data to Azure storage blob using Azure Blob storage bindings for Azure Functions in Python 使用 python azure 函数从 azure blob 存储读取文件 - Read files from azure blob storage using python azure functions 使用 Azure-Storage-Blob Python 读取 Blob 容器目录中每个 Blob 的文件大小 - Reading the File size for each blob inside a directory of a Blob Container using Azure-Storage-Blob Python 使用python上载csv文件以使Blob存储蔚蓝 - Uploading csv file using python to azure blob storage 使用 Python 从 azure blob 存储下载文件(csv、excel) - Download files (csv, excel) from azure blob storage using Python Stream 文件到 Azure Blob 存储中的 Zip 文件,使用 ZA7F5F35426B9627411FC3Z231 - Stream Files to Zip File in Azure Blob Storage using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM