共用方式為


透過 Java 使用 Blob 索引標籤來管理和尋找資料

本文說明如何利用適用於 Java 的 Azure 儲存體用戶端程式庫來使用 Blob 索引標籤管理和尋找資料。

必要條件

關於 Blob 索引標籤

Blob 索引標記會使用索引鍵/值標記屬性,將儲存體帳戶中的資料分類。 這些標記會自動編製索引,並公開為可搜尋的多維度索引,以便輕鬆地尋找資料。 本文說明如何使用 Blob 索引標記來設定、取得及尋找資料。

已啟用階層命名空間的儲存體帳戶不支援 Blob 索引標籤。 若要深入了解 Blob 索引標籤功能以及已知問題和限制,請參閱使用 Blob 索引標籤來管理及尋找 Azure Blob 資料

設定標記

如果您的程式碼透過以下其中一種機制獲得授權可以存取 Blob 資料,則您可以設定索引標籤:

如需詳細資訊,請參閱設定 Blob 索引標籤

您可以使用下列方法來設定標籤:

此方法中的指定標籤會取代現有的標籤。 如果必須保留舊值,則必須下載它們並將其包含在對此方法的呼叫中。 下列範例示範如何設定標籤:

public void setBlobTags(BlobClient blobClient) {
    // Get any existing tags for the blob if they need to be preserved
    Map<String, String> tags = blobClient.getTags();

    // Add or modify tags
    tags.put("Sealed", "false");
    tags.put("Content", "image");
    tags.put("Date", "2022-01-01");

    // setTags will replace existing tags with the map entries we pass in
    blobClient.setTags(tags);
}

您可以將空的 Map 物件傳遞至 setTags 方法來刪除所有標籤:

public void clearBlobTags(BlobClient blobClient) {
    Map<String, String> tags = new HashMap<String, String>();
    blobClient.setTags(tags);
}

取得標籤

如果您的程式碼透過以下其中一種機制獲得授權可以存取 Blob 資料,則您可以取得索引標籤:

如需詳細資訊,請參閱取得和列出 Blob 索引標籤

您可以使用下列方法來取得標籤:

下列範例示範如何擷取和逐一查看 Blob 的標籤:

public void getBlobTags(BlobClient blobClient) {
    Map<String, String> tags = blobClient.getTags();

    System.out.println("Blob tags:");
    for (Map.Entry<String, String> entry : tags.entrySet())
        System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}

使用 Blob 索引標記篩選和尋找資料

如果您的程式碼透過以下其中一種機制獲得授權可以存取 Blob 資料,則您可以使用索引標籤來尋找和篩選資料:

如需詳細資訊,請參閱使用 Blob 索引標籤來尋找資料

注意

您無法使用索引標籤來擷取先前的版本。 先前版本的標記不會傳遞至 Blob 索引引擎。 如需詳細資訊,請參閱條件和已知問題

您可以使用下列方法來尋找資料:

下列範例會尋找標記為影像的所有 Blob:

public void findBlobsByTag(BlobContainerClient blobContainerClient) {
    String query = "\"Content\"='image'";

    blobContainerClient.findBlobsByTags(query)
            .forEach(blob -> System.out.printf("Name: %s%n", blob.getName()));
}

資源

若要深入了解如何利用適用於 Java 的 Azure Blob 儲存體用戶端程式庫來使用索引標籤管理及尋找資料,請參閱下列資源。

REST API 操作

適用於 JavaScript 的 Azure SDK 包含建置在 Azure REST API 之上的程式庫,可讓您透過熟悉的 JavaScript 範例與 REST API 作業進行互動。 用來管理和使用 Blob 索引標籤的用戶端程式庫方法會使用下列 REST API 作業:

程式碼範例

用戶端程式庫資源

另請參閱