Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9356

Google Cloud Storageで画像操作したメモ

$
0
0

画像uploadするにはどうすればいいんだろう
いろいろな記事を参考にしてみた
うまくいかない?
ちょっと!よくよく見るとこの記事、リファレンスに書かれてるものとオプション名違ってるじゃん!そりゃ期待通り動かないよ!
ってことがあったので、メモっておきます。

前提

upload

const {Storage} = require('@google-cloud/storage');
const storage = new Storage({
    projectId: 'hogehoge-project',
    keyFilename: './serviceAccountKey.json', // 前提で取得済みのキーファイル
});

const BUCKET_NAME = 'hogehoge-project.appspot.com';

storage
.bucket(BUCKET_NAME)
.upload('./image.png' /* uploadしたいファイル */, { destination: 'uploaded_image.png' /* storageにこのファイル名でuploadされる */ })
.then(() => { /* success */ })
.catch((err) => { /* error */ });

delete

storage
.bucket(BUCKET_NAME)
.file('uploaded_image.png')
.delete()
.then(() => { /* success */ })
.catch((err) => { /* error */ });

リファレンス

バケットの操作周りをみれば良い
https://googleapis.dev/nodejs/storage/latest/Bucket.html


Viewing all articles
Browse latest Browse all 9356

Trending Articles