【问题标题】:Automatically Delete/Expire Azure Blobs after a time period一段时间后自动删除/过期 Azure Blob
【发布时间】:2016-05-19 23:28:17
【问题描述】:

使用 Azure Blob 存储,是否可以让容器中的单个 Blob 或所有 Blob 在一段时间后自行删除,类似于 Amazon AWS S3's Object Expiration Feature?还是 Azure 存储不提供此类功能?

【问题讨论】:

标签: azure azure-blob-storage


【解决方案1】:

【讨论】:

    【解决方案2】:

    因为多年来我一直错过该功能,所以我编写了一个带有漂亮“部署到 Azure 按钮”的小项目。还不完美,但可以工作https://github.com/nulllogicone/ExpireBlobFunction

    现在我看到微软在 2019 年 3 月 27 日有released this as a feature

    摘自那篇文章:

    Azure Blob 存储生命周期管理提供了丰富的、基于规则的 GPv2 和 Blob 存储帐户的策略。使用该政策 将您的数据转换到适当的访问层或在 数据生命周期结束。

    生命周期管理策略允许您:

    • 将 blob 转换到较冷的存储层(热到冷、热到归档或冷到归档)以优化性能和成本
    • 在生命周期结束时删除 blob
    • 定义要在存储帐户级别每天运行一次的规则 将规则应用于容器或 Blob 子集(使用前缀作为过滤器)

    【讨论】:

    【解决方案3】:
    【解决方案4】:

    Azure 存储团队最近(2017 年 10 月 5 日)发布了有关即将过期的 blob 的更新。似乎现在可以使用 Azure Logic App 模板来实现这一点,并且他们将在今年晚些时候推出本机 Blob 存储解决方案。

    链接:Provide Time to live feature for Blobs

    我们很高兴地宣布,我们已经提供了一个 Azure 逻辑应用模板来使旧 blob 过期。要在您的环境中设置此自动化解决方案:创建一个新的逻辑应用实例,选择“删除旧 Azure blob”模板,自定义并运行。我们将在未来几周发布一篇博文,详细说明说明并提供更多模板。

    未来一年仍计划允许用户在本地从存储中定义 Blob 的过期策略。一旦我们有进展可以分享,我们就会这样做。我们将继续每季度至少提供一次更新。

    如有任何其他问题,或讨论您的具体方案,请发送电子邮件至 azurestoragefeedback@microsoft.com。

    【讨论】:

    • 我们已经实现了 Azure 逻辑应用程序,但这实际上意味着创建一个应用程序来列出 blob、过滤旧的 blob 并删除它们。您确实为逻辑应用程序付费,而且它似乎非常昂贵。我们的订阅费用在短短两天内就增加了超过 230 美元!这个逻辑应用程序还需要监控、维护……所以我不认为它是一个托管解决方案。在实施此之前要小心,并跟踪您的成本!!!
    【解决方案5】:

    Azure 存储没有过期功能;您必须通过您的应用删除 blob。你如何做到这一点取决于你;您需要将到期日期目标存储在某处(无论是在数据库中还是在 blob 属性中)。

    可以通过共享访问签名(通过在 SAS 上设置结束日期)有效地在 blob 访问上创建 TTL。这将使您能够在需要删除访问权限时有效地删除访问权限,然后让后续流程删除现已过期的 blob。

    【讨论】:

      【解决方案6】:

      是的,这是可能的。参考这两个,示例代码好难找。

      规则参考:https://docs.microsoft.com/en-us/azure/storage/blobs/lifecycle-management-overview?tabs=azure-portal

      Python 示例代码参考:https://github.com/Azure-Samples/azure-samples-python-management/blob/master/samples/storage/manage_management_policy.py

      我使用的代码 sn-p:

      def add_expiry_rule(self):
              token_credential = ClientSecretCredential(
                  tenant_id=tenant_id,
                  client_id=client_id,
                  client_secret=client_secret,
              )
              storage_client = StorageManagementClient(
                  credential=token_credential, subscription_id=subscription_id
              )
              rule = {
                  "id": "test",
                  "prefix": "test/",
                  "expiration": 91,
              }
              azure_rule = {
                  "enabled": True,
                  "name": rule.get("id"),
                  "type": "Lifecycle",
                  "definition": {
                      "filters": {"blob_types": ["blockBlob"], "prefix_match": [rule.get("prefix")]},
                      "actions": {
                          "base_blob": {
                              "delete": {
                                  "days_after_modification_greater_than": str(rule.get("expiration"))
                              }
                          }
                      },
                  },
              }
              try:
                  management_policy = storage_client.management_policies.get(
                      group_name, storage_account, "default"
                  )
                  existing_rules = management_policy.policy.as_dict()
                  existing_rules.get("rules").append(azure_rule)
                  management_policy_rules = existing_rules
              except Exception as e:
                  management_policy_rules = {"rules": [azure_rule]}
              try:
                  management_policy = storage_client.management_policies.create_or_update(
                      group_name,
                      storage_account,
                      "default",
                      {"policy": management_policy_rules},
                  )
                  print("Azure: Added rule {} successfully".format(rule.get("id")))
              except Exception as e:
                  if e.message.endswith("conflicting rule name."):
                      print("Azure: Rule ID: {} exists".format(rule.get("id")))
                  else:
                      raise Exception("Azure: Error adding rule. {}".format(e.message))
      

      【讨论】:

      • 欢迎提供解决方案的链接,但请确保您的答案在没有它的情况下有用:add context around the link 这样您的其他用户就会知道它是什么以及为什么存在,然后引用最多如果目标页面不可用,您链接到的页面的相关部分。
      猜你喜欢
      • 2020-03-02
      • 2021-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      相关资源
      最近更新 更多