Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
4
Help us understand the problem. What are the problem?
@imanao89

Node-RedからGoogle Driveに画像ファイルをアップロードする

1. Google Developer Consoleで APIの有効化・サービスアカウント作成し、JSONファイルをダウンロード

※参考記事

1-1. Google Developer Consoleにアクセスする

1-2. 上部メニューのプロジェクトを選択を選ぶ
image.png

1-3. 新しいプロジェクトをクリック

image.png

1-4. プロジェクト名を入力して作成ボタンを押す
image.png

1-5. 作成したプロジェクトを選択し、Google Drive APIを有効にする
image.png

1-6. サービスアカウントを作成
image.png

1-7. サービスアカウントの編集画面から鍵を作成しJSONファイルをダウンロードする
image.png

2. Google Driveでフォルダを作成し、フォルダを先ほど作成したサービスアカウントに共有する

image.png

3.作成したフォルダのURLからフォルダIDを取得

image.png

4. Node-Redのパレットから「node-red-contrib-google」ノードをインストール

image.png

5. フローを読み込み、各ノードブロックのプロパティを設定する

[{"id":"37fe8973.9b3846","type":"change","z":"d7300f51.c03af","name":"set parameters","rules":[{"t":"set","p":"folderid","pt":"msg","to":"","tot":"str"},{"t":"set","p":"filename","pt":"msg","to":"test.jpg","tot":"str"},{"t":"set","p":"mimeType","pt":"msg","to":"image/jpg","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"{\t    \"resource\": {\t        \"name\": filename,\t        \"parents\": [\t            folderid\t        ]\t    },\t    \"media\": {\t        \"mimeType\": mimeType,\t        \"body\": payload\t    },\t    \"fields\": \"id\"\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":140,"wires":[["d862bfac.a32a7"]]},{"id":"39a1f760.f13da8","type":"debug","z":"d7300f51.c03af","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":140,"wires":[]},{"id":"d862bfac.a32a7","type":"google","z":"d7300f51.c03af","name":"","google":"be4a5517.9547d8","api":"drive:v3","operation":"files.create","x":470,"y":140,"wires":[["39a1f760.f13da8"]]},{"id":"caf7c056.9ef8d","type":"file in","z":"d7300f51.c03af","name":"","filename":"","format":"","chunk":false,"sendError":false,"encoding":"none","x":470,"y":80,"wires":[["37fe8973.9b3846"]]},{"id":"7d1550df.83376","type":"change","z":"d7300f51.c03af","name":"","rules":[{"t":"set","p":"filename","pt":"msg","to":"/home/pi/Pictures/test/test.jpg","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":80,"wires":[["caf7c056.9ef8d"]]},{"id":"e8c966be.6e9248","type":"inject","z":"d7300f51.c03af","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":80,"wires":[["7d1550df.83376"]]},{"id":"be4a5517.9547d8","type":"google-conn","z":"","name":"Google","key":"{}\n","scopes":"https://www.googleapis.com/auth/drive\nhttps://www.googleapis.com/auth/drive.appdata\nhttps://www.googleapis.com/auth/drive.file\nhttps://www.googleapis.com/auth/drive.metadata\nhttps://www.googleapis.com/auth/drive.metadata.readonly\nhttps://www.googleapis.com/auth/drive.photos.readonly\nhttps://www.googleapis.com/auth/drive.readonly\nhttps://www.googleapis.com/auth/drive.scripts"}]

googleノードブロック:JSON Keyを手順1-7で取得したJSONファイルの内容で設定
image.png

「set parameters」ブロック:「folderid」に手順3で取得したフォルダIDを設定
image.png

「set msg.filename」ブロック:送信対象の画像ファイルのパスを設定
image.png

6. injectノードを実行してGoogle Driveにファイルがアップロードされていることを確認

image.png

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
4
Help us understand the problem. What are the problem?