ウェブ上で Cloud Storage でファイル メタデータを使用する

ファイルを Cloud Storage 参照にアップロードした後で、ファイル メタデータを取得または更新して、コンテンツ タイプの更新などを行うことができます。ファイルには、追加のファイル メタデータを使用してカスタム Key-Value ペアを格納することもできます。

ファイル メタデータを取得する

ファイル メタデータには、namesizecontentType(多くの場合 MIME タイプと呼ばれます)などの一般的なプロパティに加え、contentDispositiontimeCreated などのあまり一般的でないプロパティも含まれます。このメタデータは、getMetadata() メソッドを使用して Cloud Storage 参照から取得できます。getMetadata() は、完全なメタデータを含む Promise を返すか、Promise が拒否された場合はエラーを返します。

ウェブ向けのモジュラー API

import { getStorage, ref, getMetadata } from "firebase/storage";

// Create a reference to the file whose metadata we want to retrieve
const storage = getStorage();
const forestRef = ref(storage, 'images/forest.jpg');

// Get metadata properties
getMetadata(forestRef)
  .then((metadata) => {
    // Metadata now contains the metadata for 'images/forest.jpg'
  })
  .catch((error) => {
    // Uh-oh, an error occurred!
  });

ウェブ向けの名前空間付き API

// Create a reference to the file whose metadata we want to retrieve
var forestRef = storageRef.child('images/forest.jpg');

// Get metadata properties
forestRef.getMetadata()
  .then((metadata) => {
    // Metadata now contains the metadata for 'images/forest.jpg'
  })
  .catch((error) => {
    // Uh-oh, an error occurred!
  });

ファイル メタデータを更新する

ファイルのアップロードが完了したら、updateMetadata() メソッドを使用して、いつでもファイル メタデータを更新できます。更新できるプロパティについて詳しくは、全一覧をご覧ください。メタデータに指定されているプロパティのみ更新され、それ以外のプロパティはすべて変更されずに残ります。updateMetadata() は、完全なメタデータを含む Promise を返すか、Promise が拒否された場合はエラーを返します。

ウェブ向けのモジュラー API

import { getStorage, ref, updateMetadata } from "firebase/storage";

// Create a reference to the file whose metadata we want to change
const storage = getStorage();
const forestRef = ref(storage, 'images/forest.jpg');

// Create file metadata to update
const newMetadata = {
  cacheControl: 'public,max-age=300',
  contentType: 'image/jpeg'
};

// Update metadata properties
updateMetadata(forestRef, newMetadata)
  .then((metadata) => {
    // Updated metadata for 'images/forest.jpg' is returned in the Promise
  }).catch((error) => {
    // Uh-oh, an error occurred!
  });

ウェブ向けの名前空間付き API

// Create a reference to the file whose metadata we want to change
var forestRef = storageRef.child('images/forest.jpg');

// Create file metadata to update
var newMetadata = {
  cacheControl: 'public,max-age=300',
  contentType: 'image/jpeg'
};

// Update metadata properties
forestRef.updateMetadata(newMetadata)
  .then((metadata) => {
    // Updated metadata for 'images/forest.jpg' is returned in the Promise
  }).catch((error) => {
    // Uh-oh, an error occurred!
  });

null に設定すると、メタデータ プロパティを削除できます。

ウェブ向けのモジュラー API

import { getStorage, ref, updateMetadata } from "firebase/storage";

const storage = getStorage();
const forestRef = ref(storage, 'images/forest.jpg');

// Create file metadata with property to delete
const deleteMetadata = {
  contentType: null
};

// Delete the metadata property
updateMetadata(forestRef, deleteMetadata)
  .then((metadata) => {
    // metadata.contentType should be null
  }).catch((error) => {
    // Uh-oh, an error occurred!
  });

ウェブ向けの名前空間付き API

// Create file metadata with property to delete
var deleteMetadata = {
  contentType: null
};

// Delete the metadata property
forestRef.updateMetadata(deleteMetadata)
  .then((metadata) => {
    // metadata.contentType should be null
  }).catch((error) => {
    // Uh-oh, an error occurred!
  });

エラーを処理する

メタデータの取得または更新時にエラーが発生する理由として、ファイルが存在しない、目的のファイルのアクセス権がユーザーにないなど、多くの理由が考えられます。エラーについて詳しくは、このドキュメントのエラーの処理のセクションをご覧ください。

カスタム メタデータ

カスタム メタデータは、String プロパティを含むオブジェクトとして指定できます。

ウェブ向けのモジュラー API

const metadata = {
  customMetadata: {
    'location': 'Yosemite, CA, USA',
    'activity': 'Hiking'
  }
};

ウェブ向けの名前空間付き API

var metadata = {
  customMetadata: {
    'location': 'Yosemite, CA, USA',
    'activity': 'Hiking'
  }
};

カスタム メタデータを使用して、各ファイルに、アプリ固有のデータをさらに格納することは可能ですが、このタイプのデータを格納して同期するには、データベース(Firebase Realtime Database など)を使用することを強くおすすめします。

ファイル メタデータのプロパティ

ファイルに関するメタデータ プロパティの一覧は次のとおりです。

プロパティ タイプ 書き込み可能
bucket 文字列 ×
generation 文字列 ×
metageneration 文字列 ×
fullPath 文字列 ×
name 文字列 ×
size 数値 ×
timeCreated 文字列 ×
updated 文字列 ×
md5Hash 文字列 ○(アップロード時)、×(メタデータの更新時)
cacheControl 文字列
contentDisposition 文字列
contentEncoding 文字列
contentLanguage 文字列
contentType 文字列
customMetadata 文字列から文字列へのマッピングを含むオブジェクト

ファイルのアップロード、ダウンロード、更新は重要ですが、ファイルを削除できることも重要です。Cloud Storage からファイルを削除する方法を学習しましょう。