我正在运行一个在 Express 上运行的 react.js 应用程序。我添加了google-cloud npm module,但我还没有设法让它工作。当我需要该module时,我的控制台中出现以下错误:
看起来 npm module应该在服务器端运行,而 react.js 在客户端运行。有没有人尝试使用 react.js 应用程序将文件上传到 Google Cloud Storage?
我正在运行一个在 Express 上运行的 react.js 应用程序。我添加了google-cloud npm module,但我还没有设法让它工作。当我需要该module时,我的控制台中出现以下错误:
看起来 npm module应该在服务器端运行,而 react.js 在客户端运行。有没有人尝试使用 react.js 应用程序将文件上传到 Google Cloud Storage?
有一个适用于 Google API 的 JavaScript 客户端库,可用于 Google Cloud Platform。这是一个教程,向您展示如何将其与 Compute Engine API 结合使用。将其与 Google Cloud Storage 一起使用的原则应该是相似的。
您的OAuth API范围需要符合云存储API JSON,你需要加载一个storage客户端,而不是compute。从那里您可以根据需要使用Cloud Storage JSON API,其风格与教程使用Compute Engine API 的风格相同。
您可以使用 firebase 在任何 React 应用程序中轻松访问您的云存储
npm install firebase
然后像任何其他库一样导入和使用它
import firebase from "firebase/app";
import "firebase/storage"
var firebaseConfig = [a key you get from your firebase project]
firebase.initializeApp(firebaseConfig);
var storage = firebase.storage();
并随心所欲地使用它。例如。
var files = await storage.ref("/images").list();
console.log(files.items);
简单地列出名为的云存储文件夹中的文件 images
你可以在这里获得完整的文档开始在 web 上使用 cloudtorage,用于前端 web 使用,就像你打算在 React 中使用一样