[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"blog-使用-gas-建立檔案上傳後端":3},{"id":4,"slug":5,"title":6,"description":7,"content":8,"date":9,"image":10,"imageAlt":6,"author":11,"categories":14,"path":18,"minRead":19},3634,"使用-gas-建立檔案上傳後端","使用 GAS 建立檔案上傳後端","使用 GAS 建立檔案上傳後端 之前建立了一組 LIFF + GAS API 的問卷流程，後續出現了需要上傳圖 […]","\u003Ch1>使用 GAS 建立檔案上傳後端\u003C\u002Fh1>\n\u003Cp>之前建立了一組 LIFF + GAS API 的問卷流程，後續出現了需要上傳圖片的需求，結果發現一樣可以透過 GAS 去對 Google Drive 存取，就稍微改造了一下XD\u003C\u002Fp>\n\u003Cp>這篇僅記錄 GAS 端接收圖片資料後的處理。\u003C\u002Fp>\n\u003Ch2>圖像 Blob 化\u003C\u002Fh2>\n\u003Cp>需要先拆解接收的 base64 字串，原本長得像「data:image\u002Fpng;base64, xxxxxxxxx」要把 data:image\u002Fpng;base64 拆掉，剩下後面的 base64 字串。\u003C\u002Fp>\n\u003Cp>拆解完後再重新建立 blob 物件(Binary Large Object)\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-jsx\">const base64String = imageBlobstring.split(&#039;;base64,&#039;).pop();\nconst imageBlob = Utilities.newBlob(Utilities.base64Decode(base64String), &#039;image\u002Fpng&#039;,&#039;test.png&#039;);\u002F\u002F需要指定檔名\nconst image = imageBlob;\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch2>直接建立檔案\u003C\u002Fh2>\n\u003Cp>記得要先開放 Google Drive 的存取權限，開放給所有人編輯\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-jsx\">file = Drive.Files.create(file, image, {&#039;fields&#039;: &#039;id,size&#039;});\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch2>指定資料夾\u003C\u002Fh2>\n\u003Cp>如果不調整資料夾，則會在 Google Drive 的根目錄，當然不太可能這樣做，因此要調整資料夾位置\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-jsx\">\u002F\u002F 取得資料夾\n\u002F\u002Fhttps:\u002F\u002Fdrive.google.com\u002Fdrive\u002Fu\u002F0\u002Ffolders\u002F&lt;FolderId&gt; 記得要開權限\nvar folder = DriveApp.getFolderById(&#039;&lt;FolderId&gt;&#039;); \n\n\u002F\u002F 在資料夾中創建文件\nvar createdFile = folder.createFile(image);\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch2>全部程式碼\u003C\u002Fh2>\n\u003Cpre>\u003Ccode class=\"language-jsx\">function uploadFile(imageBlobstring) {\n  try {\n    \u002F\u002F Makes a request to fetch a URL.\n    \u002F\u002Fconst image = UrlFetchApp.fetch(&#039;http:\u002F\u002Fgoo.gl\u002Fnd7zjB&#039;).getBlob();\n    const base64String = imageBlobstring.split(&#039;;base64,&#039;).pop();\n\n    const imageBlob = Utilities.newBlob(Utilities.base64Decode(base64String), &#039;image\u002Fpng&#039;,&#039;test.png&#039;);\u002F\u002F需要指定檔名\n    Logger.log(imageBlob);\n    const image = imageBlob;\n    let file = {\n      name: &#039;google_logo.png&#039;,\n      mimeType: &#039;image\u002Fpng&#039;\n    };\n    \u002F\u002F \u002F\u002F Create a file in the user&#039;s Drive.\n    \u002F\u002F file = Drive.Files.create(file, image, {&#039;fields&#039;: &#039;id,size&#039;});\n    \u002F\u002F 獲取資料夾\n    var folder = DriveApp.getFolderById(&#039;1JOGpsxxAD8ijbT3hIjFATE2daRvaKVN4&#039;);\n\n    \u002F\u002F 在資料夾中創建文件\n    var createdFile = folder.createFile(image);\n\n    Logger.log(&#039;ID: %s, File size (bytes): %s&#039;, createdFile.getId(), createdFile.getSize());\n  } catch (err) {\n    \u002F\u002F TODO (developer) - Handle exception\n    Logger.log(&#039;Failed to upload file with error %s&#039;, err.message);\n  }\n}\u003C\u002Fcode>\u003C\u002Fpre>\n","2024-01-24T10:48:42","\u002Fapi\u002Fproxy-image?url=https%3A%2F%2Fpost.billxu.net%2Fwp-content%2Fuploads%2F2024%2F01%2Ffe8a42d7-145a-408d-bd5f-e5f17d21cdad.jpeg",{"name":12,"link":13},"Chih Fan","http:\u002F\u002Fpost.billxu.net",[15],{"name":16,"slug":17},"GAS","gas","\u002Fblog\u002F使用-gas-建立檔案上傳後端",1]