gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 06:13:43 25 4
gpt4 key购买 nike

如何从浏览器(JavaScript)直接上传内容到GC Storage?有没有一些简单的方法可以使用 token (在 URL/cookies 中)或类似的东西来做到这一点?

注意:不考虑中间系统(如 URL 签名者)。我正在寻找一种从 JS 发送请求并在 GC 存储中处理它的方法。

如果不是,您知道在不引入太多依赖性和复杂性的情况下最简单的解决方案是什么?

最佳答案

这很容易。您可以使用 Firebase JavaScript SDK 从网络上执行此操作。

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

文档中的代码

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);
});
});

关于javascript - Google Cloud Storage - 如何直接从浏览器上传内容 (JavaScript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63183073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com