dak ブログ

python、rubyなどのプログラミング、MySQL、サーバーの設定などの備忘録。レゴの写真も。

Typescript で Cloud Storage 上の json ファイルを読み込む方法

2023-07-11 20:04:39 | Node.js
Typescript で Cloud Storage 上の json ファイルを読み込む方法のメモ。
以下の json ファイルをダウンロードするプログラム例です。
gs://cloud-storage-test-bucket/data/test.json

■インストール
npm install @google-cloud/storage

■プログラム
import storage from '@google-cloud/storage';

(async () => {
  const bucketName = 'cloud-storage-test-bucket';
  const fileName = 'data/test.json';

  const storage = new Storage();
  const response = await storage.bucket(bucketName).file(fileName).download();
  
  const obj = JSON.parse(response.toString());
  console.log(JSON.stringify(obj));
})();