firebase 存储无法上传任何文件
firebase storage can not upload any file
当我尝试将任何图像文件上传到 Firebase 存储时,收到一条错误消息。
Object { code_: "storage/unauthorized", message_: "Firebase Storage: User does not have permission to access 'img/product/1540281542536/58916.jpg'.", serverResponse_: "{\n \"error\": {\n \"code\": 403,\n \"message\": \"Permission denied. Could not perform this operation\"\n }\n}", name_: "FirebaseError" }
不知道为什么不能用
这是我的存储规则
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
match /img {
match /{allImages=**} {
allow read
}
match /product/{productID}/{imageId} {
allow create, write, delete: if request.auth != null
}
}
match /user/{userId} {
allow read, write: if request.auth.uid == userId;
}
}
}
}
这是我的上传代码
$formImgUpload.on('change', function (e) {
let file = this.files[0]
let rootRef = firebase.storage().ref()
let fileName = 'img/product/' + id + '/' + file.name
let fileRef = rootRef.child(fileName)
fileRef.put(file).then(function (result) {
console.log('"' + fileName + '" was uploaded successfully.')
result.ref.getDownloadURL().then(function (url) {
$img.attr('src', url)
$fileUrl.text(url)
})
}).catch(function (err) {
console.error(err)
})
})
谁能帮我写出正确的规则?
我也有这个问题。你是从你的客户端上传的吗?如果是这样,目前您没有为 request.auth 发送任何内容,因此它将始终为空。
要解决此问题,请发送自定义令牌 --> https://firebase.google.com/docs/auth/admin/create-custom-tokens
使用 Firebase Admin SDK 创建自定义令牌是一种非常简单的方法,使用您的 UID 从服务器发送令牌,然后在客户端对其进行身份验证。然后,当您将数据发送到存储时,request.auth 随之而来,数据库规则将能够处理这种情况。
希望对您有所帮助。
祝一切顺利。
当我尝试将任何图像文件上传到 Firebase 存储时,收到一条错误消息。
Object { code_: "storage/unauthorized", message_: "Firebase Storage: User does not have permission to access 'img/product/1540281542536/58916.jpg'.", serverResponse_: "{\n \"error\": {\n \"code\": 403,\n \"message\": \"Permission denied. Could not perform this operation\"\n }\n}", name_: "FirebaseError" }
不知道为什么不能用
这是我的存储规则
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
match /img {
match /{allImages=**} {
allow read
}
match /product/{productID}/{imageId} {
allow create, write, delete: if request.auth != null
}
}
match /user/{userId} {
allow read, write: if request.auth.uid == userId;
}
}
}
}
这是我的上传代码
$formImgUpload.on('change', function (e) {
let file = this.files[0]
let rootRef = firebase.storage().ref()
let fileName = 'img/product/' + id + '/' + file.name
let fileRef = rootRef.child(fileName)
fileRef.put(file).then(function (result) {
console.log('"' + fileName + '" was uploaded successfully.')
result.ref.getDownloadURL().then(function (url) {
$img.attr('src', url)
$fileUrl.text(url)
})
}).catch(function (err) {
console.error(err)
})
})
谁能帮我写出正确的规则?
我也有这个问题。你是从你的客户端上传的吗?如果是这样,目前您没有为 request.auth 发送任何内容,因此它将始终为空。
要解决此问题,请发送自定义令牌 --> https://firebase.google.com/docs/auth/admin/create-custom-tokens
使用 Firebase Admin SDK 创建自定义令牌是一种非常简单的方法,使用您的 UID 从服务器发送令牌,然后在客户端对其进行身份验证。然后,当您将数据发送到存储时,request.auth 随之而来,数据库规则将能够处理这种情况。
希望对您有所帮助。
祝一切顺利。