[英]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.