如何在 blob 容器中查找热文件或冷文件

问题描述

是否有某种方法或脚本可以在您的 blob 容器中搜索哪些文件是热的或冷的以将其更改为存档?

我有数以千计的文件夹和文件,手动完成这项工作简直是一场噩梦

解决方法

如果您想将 blob 层(热或冷)更改为存档层,则有一个名为 lifecycle management 的内置功能。

您可以为您的存储帐户设置规则(该规则可以根据需要应用于容器级别或帐户级别或子文件夹级别),然后blob服务可以自动更改层(热和冷)以存档.

以下是容器级别的示例:

1.导航到azure门户->您的存储帐户->生命周期管理,然后点击“添加规则”:

enter image description here

  1. 在详细信息面板 -> 指定“规则名称”,选择“规则范围”(此处,容器级别选择“使用过滤器限制 Blob”)、“Blob 类型”和“Blob 子类型”:

enter image description here

3.在“Base blob”中,指定如下设置:

enter image description here

4.在“过滤器集”中,只需输入您的 container name 进行前缀匹配:

enter image description here

5.单击“添加”按钮保存规则。 请注意 the rule will be executed after 24 hours

,

您可以使用 Powershell 更改访问级别。

#Initialize the following with your resource group,storage account,container,and blob names
$rgName = ""
$accountName = ""
$containerName = ""

#Select the storage account and get the context
$storageAccount = Get-AzStorageAccount -ResourceGroupName $rgName -Name $accountName
$ctx = $storageAccount.Context

#list the blobs in a container
$blobs = Get-AzStorageBlob -Container $containerName  -Context $ctx  
foreach($blob in $blobs)  
{  
    #if tier not equal "Archive"
    if($blob.AccessTier -ne "Archive"){
        #Change the blob’s access tier to archive
        $blob.ICloudBlob.SetStandardBlobTier("Archive")
    }
}  

另一种方法使用 .Net 中的 BlobBatch.SetBlobAccessTier Method SDK。

// Get a connection string to our Azure Storage account.
string connectionString = "<connection_string>";
string containerName = "sample-container";

// Get a reference to a container named "sample-container" and then create it
BlobServiceClient service = new BlobServiceClient(connectionString);
BlobContainerClient container = service.GetBlobContainerClient(containerName);
container.Create();
// Create three blobs named "foo","bar",and "baz"
BlobClient foo = container.GetBlobClient("foo");
BlobClient bar = container.GetBlobClient("bar");
BlobClient baz = container.GetBlobClient("baz");
foo.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Foo!")));
bar.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Bar!")));
baz.Upload(new MemoryStream(Encoding.UTF8.GetBytes("Baz!")));

// Set the access tier for all three blobs at once
BlobBatchClient batch = service.GetBlobBatchClient();
batch.SetBlobsAccessTier(new Uri[] { foo.Uri,bar.Uri,baz.Uri },AccessTier.Archive);