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

How to find hot or cool files in blob container

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

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

您可以使用 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方法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);

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

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

这是容器级别的示例:

1.Nav到Azure门户->你的存储账户->生命周期管理,然后点击“添加规则”:

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

3.In“Base blobs”,指定如下设置:

4.In“过滤器集”,只需键入前缀匹配的 container name

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