我正在尝试实施Firebase存储规则,以便只有帖子作者才能创建,删除和更新它。问题是在模拟器中它可以完美运行,但在实际情况下却不能。对我来说,我使用的元数据似乎未显示在规则中或未包含在放置请求中。
我按照文档中的说明进行所有操作:
这些是我的规则:
service firebase.storage {
match /b/{bucket}/o {
//Posts
match /{allImages=**} {
allow read: if authenticated();
allow create: if authenticated() && metadata().userId == request.auth.uid;
allow update, delete: if authenticated() && metadata().userId == request.auth.uid;
}
}
function metadata() { return request.resource.metadata; }
function authenticated() { return request.auth.uid != null; }
}
这是我用来上传文件的功能:
uploadFile({ commit, rootState }, payload) {
const storageRef = firebase
.storage()
.ref(`posts/${payload.id}/${payload.index}/${payload.index}`)
commit('app/setError', null, { root: true })
commit('app/setLoading', true, { root: true })
return new Promise((resolve, reject) => {
storageRef
.put(payload.file, {
userId: rootState.authentication.user.id
}) //HERE IS THE METADATA AS THE SECOND PARAMETER (userId)
.then(snapshot => {
snapshot.ref.getDownloadURL().then(downloadURL => {
resolve(downloadURL)
commit('app/setLoading', false, { root: true })
})
})
.catch(error => {
reject(error)
commit('app/setError', error, { root: true })
commit('app/setLoading', false, { root: true })
})
})
},
Edit 1:这是我在模拟器内部测试的对象元数据:
{"metadata":{"userId":"9PuxRiKI17Y8hbwW9aVISpdrpZa2"},"name":"b/myprojectid-develop.appspot.com/o/posts/{postId}","bucket":"myprojectid-develop.appspot.com"}
这是我在真实案例中以元数据形式发布的最小对象:
{"userId":"9PuxRiKI17Y8hbwW9aVISpdrpZa2"}
我做错了什么?任何帮助表示赞赏!
storageRef
.put(payload.file, {
customMetadata: { //This is the required key
userId: <your user id data>
}
})
NB!这似乎记录在here