Alamofire无法将文件上传到Google Cloud Storage

我是蝙蝠侠

我正在尝试通过Alamofire将媒体文件上传到GCS(Google云存储)。下面是代码。

   let soundUrl = URL(fileURLWithPath: dirPaths.appending("/audio-file.wav"))
    
    let headers: HTTPHeaders = [
        "Content-Type": "audio/wav",
        "Authorization": "Bearer <MYTOKEN>"
    ]


    Alamofire.upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append(soundUrl, withName: "name")
    },
        to: "https://www.googleapis.com/upload/storage/v1/b/<folder-name>/o?uploadType=media?&name=audio-file.wav",method: .post, headers: headers,
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseString(completionHandler: { (response) in
                    print(response)
                })

            case .failure(let encodingError):
            print(encodingError)
           
            }

         }
    )

但这并没有取得成功。

我尝试过的同样的东西Postman.它工作正常。我检查了来自https://requestb.in的发帖请求,两者看起来都很相似。

不知道问题出在哪里。

更新-

我需要获取iOS的授权令牌吗?因为我从终端获得了令牌。我把它放在标题中 我想可以吗?

我是蝙蝠侠

由于OAuth会像每小时一样过期,因此我们需要从设备获取它。一旦有了令牌,就可以从以下代码上传文件。

let soundUrl = URL(fileURLWithPath: dirPaths.appending("/audio-file.wav"))

let headers: HTTPHeaders = [
    "Content-Type": "audio/wave",
    "Authorization": "Bearer \(self.authorizationKey)"
]        
    Alamofire.upload(soundUrl, to: "https://www.googleapis.com/upload/storage/v1/b/<FOLDER>/o?uploadType=media&name=audio-file.wav",method: HTTPMethod.post, headers: headers).responseJSON{ response in
    print(response)
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将 xml 文件上传到 Google Cloud Storage 时出错

使用Flask将文件上传到Google Cloud Storage

将文件上传到Google Cloud Storage:500后端错误

将.raw文件上传到Google Cloud Storage

使用 Codeigniter 将文件上传到 Google Cloud Storage

使用 For 循环将多个文件上传到 Google Cloud Storage

无法从Django将文件上传到Google Cloud Storage(本地Windows ENV

将文件从Firebase Cloud Functions上传到Cloud Storage

将图像从Google Cloud Function上传到Cloud Storage

将文件上传到Google Cloud Storage而不在本地下载文件?

使用Google App Engine(Python)将文件上传到Google Cloud Storage

使用Google App Engine将大文件上传到Google Cloud Storage

无法将图像上传到Firebase Cloud Storage

如何通过Cloud Functions将文件上传到Cloud Storage并使用Firestore控制对Cloud Storage的访问?

自动将数据上传到 Google Cloud Storage 和 BigQuery

将 Excel 上传到 Google Cloud Storage 时出错

无法将XCTest软件包上传到Google Cloud Storage for Firebase测试实验室

使用JSON API将文件上传到Google Cloud Storage,错误401未经授权

将文件直接上传到Google Cloud Storage for GAE应用

使用PHP和Ajax将文件上传到Google Cloud Storage Bucket

将Python文件上传到Google Cloud Storage Bucket返回管道中断错误

如何使用JSON API和CloseableHttpClient将文件上传到Google Cloud Storage存储桶?

使用 Node.js 将文件上传到 Google Cloud Storage(已声明“bucketName”)

使用Python将文件上传到Google Cloud Storage签名的URL

将python对象上传到Google Cloud Storage,而不保存到文件中

使用Python将文件上传到Google Cloud Storage Bucket子目录

从Localhost或外部服务器将文件上传到Google Cloud Storage

使用C#中的HMAC密钥将文件上传到Google Cloud Storage

如何将文件上传到 Google Cloud Storage Bucket 子目录