Problem


Azure Storage Account offers three tiers of blob storage. Hot, cool and archive. We usually use hot tier for data that needs to be accessed frequently, move blobs to cool tier if they won't be used very often, and archive blobs when they are not used at all. This is a good practice to reduce costs when using Azure.

In my case, I am building a system that stores attachments to Azure Storage Account. These attachments will only be frequently accessed within a month. So hot tier is only when attachments are uploaded, after 30 days, they are moved to cool tier, and after 365 days, they will be archived.

However, manually, or programmatically moving blobs still require an amount of work to do, which can make you work 996. Let's see how to do it without any programming.

Solution


It's a built-in feature in Azure Storage Account. Go to Lifecycle management blade of your Storage Account in Azure portal. 

Click "+ Add a rule". Enter a name, for example "move attachments to cool tier". In my case I am targeting blobs that are only in the "attachments" container. So, I will select "Limit blobs with filters".

In the next step. I set a rule for blobs that are created more then 30 days, move them to cool storage.

In the last step, add blob prefix of "attachments". 

This is for targeting only a specific container, which in my case is the "attachments" container.

You can create similar rules for archive tier. Make sure all your rules are Enabled.

However, when the rule is created, it won't be executed immediately, just wait for a day, then you will find your blobs being correctly moved to cool or archive tier.

All rules can also be created in code, refer to: https://learn.microsoft.com/en-us/azure/storage/blobs/lifecycle-management-overview

e.g.

{
  "enabled": true,
  "name": "move attachments to cool tier",
  "type": "Lifecycle",
  "definition": {
    "actions": {
      "baseBlob": {
        "tierToCool": {
          "daysAfterCreationGreaterThan": 30
        }
      }
    },
    "filters": {
      "blobTypes": [
        "blockBlob"
      ],
      "prefixMatch": [
        "attachments"
      ]
    }
  }
}