【发布时间】:2018-02-09 13:01:13
【问题描述】:
这里是 上下文:我的 iOS swift 应用程序
- 录制声音,
- 创建一个 firebase 对象,
- 用对象的键重命名文件
- 在 Firebase 云上上传 wav 文件。
- 触发了 Firebase 云功能,将音频文件发送到 Google Speech .recognize
我的问题: 当我手动将声音文件上传到云存储时,它可以正常工作,但是当应用程序自动上传文件时,我收到以下错误消息作为语音 API 的返回:
{ Error: The caller does not have permission at /user_code/node_modules/@google-cloud/speech/node_modules/grpc/src/node/src/client.js:554:15代码:7,元数据:元数据{_internal_repr:{}},注意: '重试方法发生异常,未归类为 瞬态'}
这里是快速部分:
func uploadFile(fileName:String){
// File located on disk
let localFileURL = FileManager.default.urls(for: .documentDirectory, in:.userDomainMask)[0]
let fileURL = localFileURL.appendingPathComponent(fileName)
if FileManager.default.fileExists(atPath: fileURL.path) {
print("FilePath", fileURL.path)
// Create a reference to the file you want to upload
let newMnemoRef = MnemoDatabase.shared.createNew()
let newMnemoId = newMnemoRef.key
let filename=newMnemoId+".wav"
//let filename=fileName
let audioStorageRef = storage.reference().child(filename)
let storagePath = "gs://\(audioStorageRef.bucket)/\(audioStorageRef.fullPath)"
print(storagePath)
// Upload the file to the path "audio"
let uploadTask = audioStorageRef.putFile(from: fileURL, metadata: nil) { metadata, error in
if let error = error {
print("Upload error : ", error.localizedDescription)
} else {
// Metadata contains file metadata such as size, content-type, and download URL.
print ("OK")
}
}
// Add a progress observer to an upload task
let observer = uploadTask.observe(.success) { snapshot in
print("uploaded!")
newMnemoRef.child("audio").setValue([
"encoding_converted":"LINEAR16",
"sampleRate_converted":"44100",
"path_converted":storagePath])
}
} else {
print ("Non existent file", fileURL.path)
}
}
调用语音 API 的云函数适用于手动上传文件。 这是摘录
const request = {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: language,
speechContexts: context
};
speech.recognize(uri, request)
云存储桶和云函数都共享相同的项目凭据。 我从存储桶中删除了所有身份验证
// Anyone can read or write to the bucket, even non-users of your app.
// Because it is shared with Google App Engine, this will also make
// files uploaded via GAE public.
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}
我什至尝试将路径硬编码到云函数中,但无济于事。
如果有任何帮助,我将不胜感激
【问题讨论】:
标签: swift google-app-engine firebase firebase-realtime-database firebase-storage