Firebase 存储模拟器规则不起作用

Ste*_*nch 5 rules emulation ios firebase-tools firebase-storage

我的应用程序工作正常,但当我使用 Firebase 模拟器时,我遇到了不遵守存储规则的问题。我已经下载了该storage.rules文件,它与该文件位于同一目录中firebase-json。模拟器套件启动得很好,我可以看到存储模拟器正在工作。但是,当我尝试上传图像(就像我在实时应用程序中所做的那样)时,出现错误。

Error while uploading file: Error Domain=FIRStorageErrorDomain Code=-13021 "User does not have permission to access gs://my-stuff-7796d.appspot.com/Profiles/0ye7psTQA4xR6DfjZRXjjtCWKyPw.jpg." UserInfo={object=Profiles/0ye7psTQA4xR6DfjZRXjjtCWKyPw.jpg, ResponseBody={"error":{"code":403,"message":"Permission denied. No WRITE permission."}}, bucket=my-stuff-7796d.appspot.com, data={length = 74, bytes = 0x7b226572 726f7222 3a7b2263 6f646522 ... 73696f6e 2e227d7d }, data_content_type=application/json; charset=utf-8, NSLocalizedDescription=User does not have permission to access gs://my-stuff-7796d.appspot.com/Profiles/0ye7psTQA4xR6DfjZRXjjtCWKyPw.jpg., ResponseErrorDomain=com.google.HTTPStatus, ResponseErrorCode=403}

存储规则是:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

再次强调,在实时 Firebase 上运行效果很好并且遵守规则。这是我的firebase.json文件

  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "storage": {
    "rules": "storage.rules"
  },
  "emulators": {
    "auth": {
      "port": 9099
    },
    "firestore": {
      "port": 8080
    },
    "storage": {
      "port": 9199
    },
    "ui": {
      "enabled": true
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

当我启动我的应用程序时,这是我在调用 FirebaseApp.configure 后初始化的代码

Auth.auth().useEmulator(withHost:"localhost", port:9099)
Storage.storage().useEmulator(withHost:"localhost", port:9199)
let settings = Firestore.firestore().settings
settings.host = "localhost:8080"
settings.isPersistenceEnabled = false
settings.isSSLEnabled = false
Firestore.firestore().settings = settings
Run Code Online (Sandbox Code Playgroud)

我错过了什么,或者这是一个错误?

小智 0

我也有这个问题。似乎在 11.8.0 版本中已经为我解决了。作为临时解决方法,我采取允许所有读/写的方式,这样我就不必使用我的生产环境并支付使用费用。这不是一个理想的解决方案,但它解除了我的障碍。

但对于那些可能遇到类似问题的人,请尝试更新到最新的 firebase-tools:

npm install -g firebase-tools
Run Code Online (Sandbox Code Playgroud)

请务必解决以下问题:

npm audit fix
Run Code Online (Sandbox Code Playgroud)

或者对 storage.rules 文件进行以下更改:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)