dak ブログ

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

TypeScript で Cloud Storage のファイルをダウンロード

2024-03-23 00:13:03 | SVG
TypeScript で Cloud Storage のファイルをダウンロードする方法のメモ。
■ライブラリのインストール
npm install @google-cloud/storage
以下のプログラムで cloud-storage-foo/bar/baz.html をダウンロード。
■プログラム
import { Bucket, Storage } from '@google-cloud/storage';

(async () => {
  const target_storage = 'cloud-storage-foo';
  const target_file = 'bar/baz.html';

  const storage = new Storage();
  const bucket = storage.bucket(target_storage);
  const file = bucket.file(target_file);

  const contents = await file.download();
  for (const content of contents) {
    console.log(content.toString('utf-8'));
  }
})();