共用方式為


使用 TypeScript 管理 Blob 屬性和中繼資料

除了包含的資料之外,Blob 還支援系統屬性和使用者定義的中繼資料。 此文章說明如何使用適用於 JavaScript 的 Azure 儲存體用戶端程式庫,來管理系統屬性和使用者定義的中繼資料。

必要條件

關於屬性和中繼資料

  • 系統屬性:系統屬性存在於每個 Blob 儲存體資源上。 其中一些可以讀取或設定,另一些則是唯讀的。 實際上,某些系統屬性會對應至特定標準 HTTP 標頭。 適用於 JavaScript 的 Azure 儲存體用戶端程式庫會為您維護這些屬性。

  • 使用者定義的中繼資料:使用者定義的中繼資料是由您為 Blob 儲存體資源所指定一或多個成對的名稱和數值所組成。 您可以使用中繼資料來儲存資源的額外值。 中繼資料值僅供您自己使用,並不會影響資源的運作方式。

    中繼資料名稱/值組是有效的 HTTP 標頭,所以應該遵守控管 HTTP 標頭的所有限制。 如需有關中繼資料命名需求的詳細資訊,請參閱中繼資料名稱

注意

Blob 索引標記也提供將任意使用者定義索引鍵/值屬性與 Azure Blob 儲存體資源一起儲存的功能。 雖然與中繼資料類似,但是只有 Blob 索引標記會自動編製索引,並且可讓原生 Blob 服務進行搜尋。 除非您利用個別服務 (例如 Azure 搜尋服務),否則中繼資料無法進行編製索引和查詢。

若要深入了解此功能,請參閱使用 Blob 索引 (預覽) 來管理和尋找 Azure Blob 儲存體上的資料

設定 Blob HTTP 標頭

下列程式碼範例會在 Blob 上設定 Blob HTTP 系統屬性。

若要設定 Blob 的 HTTP 屬性,請建立 BlobClient,然後呼叫 BlobClient.setHTTPHeaders。 檢閱 BlobHTTPHeaders 屬性,以了解您想要設定哪些 HTTP 屬性。 未明確設定的 HTTP 屬性都會遭到清除。

async function setHTTPHeaders(blobClient: BlobClient, headers): Promise<void> {
  /*
  headers= {
      blobContentType: 'text/plain',
      blobContentLanguage: 'en-us',
      blobContentEncoding: 'utf-8',
      // all other http properties are cleared
    }
  */

  const headerResults = await blobClient.setHTTPHeaders(headers);

  if (!headerResults.errorCode) {
    console.log(`headers set successfully ${headerResults.date}`);
  }
}

設定中繼資料

您可以將中繼資料指定為 blob 或容器資源上的一個或多個成對的名稱和數值。 若要設定中繼資料,請建立 BlobClient,然後請使用下列方式傳送成對的名稱和數值之 JSON 物件:

下列程式碼範例會在 Blob 上設定中繼資料。

async function setBlobMetadata(
  blobClient: BlobClient,
  metadata: Metadata
): Promise<void> {
  /*
    metadata= {
        reviewedBy: 'Bob',
        releasedBy: 'Jill',
    }
*/
  const metadataResults = await blobClient.setMetadata(metadata);

  if (!metadataResults.errorCode) {
    console.log(`metadata set successfully ${metadataResults.date}`);
  }
}

若要讀取中繼資料,請取得 Blob 的屬性 (如下所示),特別是參考 metadata 屬性。

取得 Blob 屬性

下列程式碼範例會取得 Blob 的系統屬性,包括 HTTP 標頭和中繼資料,並顯示這些值。

async function getProperties(blobClient: BlobClient): Promise<void> {
  const propertiesResponse: BlobGetPropertiesResponse =
    await blobClient.getProperties();

  if (!propertiesResponse.errorCode) {
    console.log(blobClient.name + ' properties: ');

    for (const property in propertiesResponse) {
      switch (property) {
        // nested properties are stringified and returned as strings
        case 'metadata':
        case 'objectReplicationRules':
          console.log(
            `    ${property}: ${JSON.stringify(propertiesResponse[property])}`
          );
          break;
        default:
          console.log(`    ${property}: ${propertiesResponse[property]}`);
          break;
      }
    }
  }
}

Blob 屬性可以包括:

lastModified: Mon Mar 20 2023 11:04:17 GMT-0700 (Pacific Daylight Time)
createdOn: Mon Mar 20 2023 11:04:17 GMT-0700 (Pacific Daylight Time)
metadata: {"releasedby":"Jill","reviewedby":"Bob"}
objectReplicationPolicyId: undefined
objectReplicationRules: {}
blobType: BlockBlob
copyCompletedOn: undefined
copyStatusDescription: undefined
copyId: undefined
copyProgress: undefined
copySource: undefined
copyStatus: undefined
isIncrementalCopy: undefined
destinationSnapshot: undefined
leaseDuration: undefined
leaseState: available
leaseStatus: unlocked
contentLength: 19
contentType: text/plain
etag: "0x8DB296D85EED062"
contentMD5: undefined
isServerEncrypted: true
encryptionKeySha256: undefined
encryptionScope: undefined
accessTier: Hot
accessTierInferred: true
archiveStatus: undefined
accessTierChangedOn: undefined
versionId: undefined
isCurrentVersion: undefined
tagCount: undefined
expiresOn: undefined
isSealed: undefined
rehydratePriority: undefined
lastAccessed: undefined
immutabilityPolicyExpiresOn: undefined
immutabilityPolicyMode: undefined
legalHold: undefined
errorCode: undefined
body: true
_response: [object Object]
objectReplicationDestinationPolicyId: undefined
objectReplicationSourceProperties:

資源

若要深入了解如何使用適用於 JavaScript 的 Azure Blob 儲存體用戶端程式庫來管理系統屬性和使用者定義中繼資料,請參閱下列資源。

REST API 操作

適用於 JavaScript 的 Azure SDK 包含建置在 Azure REST API 之上的程式庫,可讓您透過熟悉的 JavaScript 範例與 REST API 作業進行互動。 用來管理系統屬性與使用者定義中繼資料的用戶端程式庫方法會使用下列 REST API 作業:

程式碼範例

用戶端程式庫資源