LoginSignup
17
11

More than 3 years have passed since last update.

【GCS】画像を一般公開する

Posted at

はじめに

Google Cloud Storage(以下GCS)で、エンドユーザーにコンテンツを配信することはよくあると思います。
本稿では、GCSにアップロードした画像をエンドユーザーに配信する方法を記載します。

期限付きで公開する

有効期限が最大7日間署名付きの画像URLを発行することができる

node.jsで発行する

import { Storage } from "@google-cloud/storage";

const storage = new Storage();
const file = storage.bucket(`バケット名`).file(`画像ファイル名`);
const url = await file.getSignedUrl({
  action: 'read',
  expires: Date.now() + 20 * 60 * 1000
});

バケットを公開する

無期限で画像を公開したい場合バケット自体をパブリックにする

NO1.png
デフォルトの設定ではPublic accessPer objectとなっている
NO2.png
バケットのPermissionsより次を設定する
New members: allUsers
Role: Storage Object Viewer

NO3.png
Public accessPublicになりバケットが一般公開された状態になる

NO4.png
画像は次のURLでアクセスすることができる
https://storage.googleapis.com/{バケット名}/{画像ファイル名}
例えばmako.pngであれば
https://storage.googleapis.com/mako0715_images/mako.png
でアクセスすることができる

参考

17
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
11