简体   繁体   English

Google Cloud Storage - 如何直接从浏览器上传内容 (JavaScript)

[英]Google Cloud Storage - How to Upload content directly from browser (JavaScript)

How to upload content directly from browser (JavaScript) to GC Storage?如何将内容直接从浏览器(JavaScript)上传到 GC 存储? Is there some simple way to do it with token (in URL/cookies) or alike?是否有一些简单的方法可以使用令牌(在 URL/cookies 中)或类似方法?

Note: no intermediary systems (like URL signers) are considered.注意:不考虑中间系统(如 URL 签名者)。 I am looking for a way to send request from JS and and process it at GC Storage.我正在寻找一种从 JS 发送请求并在 GC 存储中处理它的方法。

If no, what is the simplest solution do you know about to not introduce too many dependencies and complexity?如果不是,您知道不引入太多依赖项和复杂性的最简单解决方案是什么?

It's very easy.这很容易。 You can do it from the web with the Firebase JavaScript SDK.您可以从 web 和 Firebase JavaScript ZF20E3C5E54C3AB3D375D666B.

https://firebase.google.com/docs/storage/web/upload-files https://firebase.google.com/docs/storage/web/upload-files

Code from the docs来自文档的代码

var uploadTask = storageRef.child('images/rivers.jpg').put(file);

// Register three observers:
// 1. 'state_changed' observer, called any time the state changes
// 2. Error observer, called on failure
// 3. Completion observer, called on successful completion
uploadTask.on('state_changed', function(snapshot){
  // Observe state change events such as progress, pause, and resume
  // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
  var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
  console.log('Upload is ' + progress + '% done');
  switch (snapshot.state) {
    case firebase.storage.TaskState.PAUSED: // or 'paused'
      console.log('Upload is paused');
      break;
    case firebase.storage.TaskState.RUNNING: // or 'running'
      console.log('Upload is running');
      break;
  }
}, function(error) {
  // Handle unsuccessful uploads
}, function() {
  // Handle successful uploads on complete
  // For instance, get the download URL: https://firebasestorage.googleapis.com/...
  uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
    console.log('File available at', downloadURL);
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JavaScript从客户端浏览器上传到Google云端存储 - Upload from Client Browser to Google Cloud Storage Using JavaScript 将缓冲区直接上传到 Google Cloud Storage - Upload Buffer Directly to Google Cloud Storage 如何使用服务器上的 Node 从客户端 JavaScript 上传到 Google Cloud Storage? - How do I upload to Google Cloud Storage from client-side JavaScript, with Node on the server? 通过javascript将文件上传到Google云存储 - Upload file to google cloud storage by javascript 使用 javascript 上传到谷歌云存储签名 url - upload to google cloud storage signed url with javascript 用户可以不经身份验证直接将文件上传到Google Cloud Storage吗? - Can users upload files directly to Google Cloud Storage without authentication? 如何从 Node 中的图像 url 将图像上传到 Google Cloud Storage? - How to upload an image to Google Cloud Storage from an image url in Node? 使用node.js为Google云端存储创建已签名的网址,以便从浏览器直接上传 - Create signed URLs for Google Cloud Storage with node.js for direct upload from browser 如何使用 Node.js 将 base64 编码的图像(字符串)直接上传到 Google Cloud Storage 存储桶? - How do I upload a base64 encoded image (string) directly to a Google Cloud Storage bucket using Node.js? 将图像从Google Cloud功能上传到云端存储 - Upload Image from Google Cloud Function to Cloud Storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM