Angular Firebase存储安全规则400错误问题“;拒绝许可。无法访问存储桶…”;(在部署时,在本地主机上工作时)
我的应用程序中有一个上传文件功能,这在Angular Firebase存储安全规则400错误问题“;拒绝许可。无法访问存储桶…”;(在部署时,在本地主机上工作时),angular,firebase,google-cloud-storage,firebase-storage,firebase-security,Angular,Firebase,Google Cloud Storage,Firebase Storage,Firebase Security,我的应用程序中有一个上传文件功能,这在localhost上运行得非常好。但是,当运行firebase deploy并部署应用程序时,由于缺乏权限,我出现了以下错误 编辑:我可以部署,CLI没有问题,只是在部署时它不像在localhost上那样工作 尝试: 这个答案是: 启用Blaze计划,从而也可以计费 在两个域上进行测试:xxx.web.app和xxx.firebaseapp.com 检查Google云平台存储,它从localhost上传的文件与Firebase云存储完全相同 HTML代码
localhost
上运行得非常好。但是,当运行firebase deploy
并部署应用程序时,由于缺乏权限,我出现了以下错误
编辑:我可以部署,CLI没有问题,只是在部署时它不像在localhost
上那样工作
尝试:
- 这个答案是:
- 启用Blaze计划,从而也可以计费
- 在两个域上进行测试:xxx.web.app和xxx.firebaseapp.com
- 检查Google云平台存储,它从localhost上传的文件与Firebase云存储完全相同
<mat-form-field>
<!-- Accept only files in the following format: .doc, .docx, .jpg, .jpeg, .pdf, .png, .xls, .xlsx. However, this is easy to bypass, Cloud Storage rules has been set up on the back-end side. -->
<ngx-mat-file-input
[accept]="[
'.doc',
'.docx',
'.jpg',
'.jpeg',
'.pdf',
'.png',
'.xls',
'.xlsx'
]"
(change)="uploadFile($event)"
formControlName="fileUploader"
multiple
aria-label="Here you can add additional files about your project, which can be helpeful for us."
placeholder="Additional files"
title="Additional files"
type="file"
>
</ngx-mat-file-input>
<mat-icon matSuffix>folder</mat-icon>
<mat-hint
>Accepted formats: DOC, DOCX, JPG, JPEG, PDF, PNG, XLS and XLSX,
maximum files upload size: 20 MB.
</mat-hint>
<mat-error
*ngIf="contactForm.get('fileUploader')!.hasError('maxContentSize')"
>
This size is too large,
<strong
>maximum acceptable upload size is
{{
contactForm.get('fileUploader')?.getError('maxContentSize')
.maxSize | byteFormat
}}</strong
>
(uploaded size:
{{
contactForm.get('fileUploader')?.getError('maxContentSize')
.actualSize | byteFormat
}}).
</mat-error>
</mat-form-field>
文件夹
接受的格式:DOC、DOCX、JPG、JPEG、PDF、PNG、XLS和XLSX,
最大文件上载大小:20 MB。
这个尺寸太大了,
可接受的最大上载大小为
{{
contactForm.get('fileUploader')?.getError('maxContentSize'))
.maxSize | byteFormat
}}
(上传尺寸:
{{
contactForm.get('fileUploader')?.getError('maxContentSize'))
.按TEFORMAT实际化
}}).
打字脚本(角度)代码
/**
*@description将其他文件上载到Cloud Firestore并获取文件的URL。
*@param{event}-已发送文件的对象。
*@returns{void}
*/
公共上载文件(事件:任意):无效{
//遍历所有上传的文件。
for(设i=0;i{
fileRef.getDownloadURL().subscribe(downloadURL=>{
这是angularFirestore
.collection('文件')
.add({downloadURL:downloadURL});
this.downloadURL.push(downloadURL);
});
}),
catchError((错误:任意)=>{
返回投掷器(错误);
})
)
.subscribe();
}
}
存储规则
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read; // Required in order to send this as attachment.
// Allow write files Firebase Storage, only if:
// 1) File is no more than 20MB
// 2) Content type is in one of the following formats: .doc, .docx, .jpg, .jpeg, .pdf, .png, .xls, .xlsx.
allow write: if request.resource.size <= 20 * 1024 * 1024
&& (request.resource.contentType.matches('application/msword')
|| request.resource.contentType.matches('application/vnd.openxmlformats-officedocument.wordprocessingml.document')
|| request.resource.contentType.matches('image/jpg')
|| request.resource.contentType.matches('image/jpeg')
|| request.resource.contentType.matches('application/pdf')
|| request.resource.contentType.matches('image/png')
|| request.resource.contentType.matches('application/vnd.ms-excel')
|| request.resource.contentType.matches('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'))
}
}
}
rules_version='2';
服务firebase.storage{
匹配/b/{bucket}/o{
匹配/{allpath=**}{
allow read;//必须将其作为附件发送。
//仅在以下情况下允许写入文件Firebase存储:
//1)文件大小不超过20MB
//2)内容类型采用以下格式之一:.doc、.docx、.jpg、.jpeg、.pdf、.png、.xls、.xlsx。
允许写入:如果由于您收到的消息而导致request.resource.size,则您的问题与相关
这是其中的一个
检查您收到的错误消息,确保您正在部署的实例或您正在使用的服务帐户具有所需的角色,使它们能够执行您想要执行的操作。问题已解决。问题是我使用了GitLab CI管道,并将错误的环境变量注入了错误的版本中。如果您使用stag出现类似问题的ing和生产环境请查看您是否错误地没有将staging firebase config注入到生产环境中,反之亦然。部署不受安全规则的影响。如果部署失败,则说明存在其他问题。如果来自CLI的错误消息没有帮助,则应联系Firebase支持寻求帮助。@DougStevenson我可以部署它,但在部署时它不起作用。我认为我的第二句话很混乱,编辑了它。
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read; // Required in order to send this as attachment.
// Allow write files Firebase Storage, only if:
// 1) File is no more than 20MB
// 2) Content type is in one of the following formats: .doc, .docx, .jpg, .jpeg, .pdf, .png, .xls, .xlsx.
allow write: if request.resource.size <= 20 * 1024 * 1024
&& (request.resource.contentType.matches('application/msword')
|| request.resource.contentType.matches('application/vnd.openxmlformats-officedocument.wordprocessingml.document')
|| request.resource.contentType.matches('image/jpg')
|| request.resource.contentType.matches('image/jpeg')
|| request.resource.contentType.matches('application/pdf')
|| request.resource.contentType.matches('image/png')
|| request.resource.contentType.matches('application/vnd.ms-excel')
|| request.resource.contentType.matches('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'))
}
}
}