【Swift】上传图片到腾讯云(生成token,上传)
1、注册
//MARK:注册腾讯云存储 func registCosSave() { let config = QCloudServiceConfiguration.init() config.signatureProvider = self config.appID = "130*****18" let endpoint = QCloudCOSXMLEndPoint.init() endpoint.regionName = "ap-shanghai" endpoint.useHTTPS = true config.endpoint = endpoint QCloudCOSXMLService.registerDefaultCOSXML(with: config) QCloudCOSTransferMangerService.registerDefaultCOSTransferManger(with: config) }
2、生成签名
func signature(with fileds: QCloudSignatureFields!, request: QCloudBizHTTPRequest!, urlRequest urlRequst: NSMutableURLRequest!, compelete continueBlock: QCloudHTTPAuthentationContinueBlock!) { let cre = QCloudCredential.init() cre.secretID = "AKIDW**********************tkOlnnRp" cre.secretKey = "f5F9K*******************ARVKz" let auth = QCloudAuthentationV5Creator.init(credential: cre) let signature = auth?.signature(forData: urlRequst) continueBlock(signature,nil) }
3、使用
let uploadRequest = QCloudCOSXMLUploadObjectRequest<AnyObject>.init() if let dataBody = image.resized(toWidth: 828)!.pngData() { uploadRequest.body = dataBody as NSData uploadRequest.bucket = "oops-1********8" uploadRequest.object = "\(Int64(Date.init().timeIntervalSince1970 * 1000))" + self.targetId! uploadRequest.sendProcessBlock = {(bytesSent , totalBytesSent , totalBytesExpectedToSend) in print(bytesSent) print(totalBytesSent) print(totalBytesExpectedToSend) } uploadRequest.setFinish { (result, error) in if error == nil{ print("kiminotachi") print(result) print(imageName) }else{ //从 result 中获取请求的结果 print(error); }} QCloudCOSTransferMangerService.defaultCOSTransferManager().uploadObject(uploadRequest);
能用七牛云的话,不建议用腾讯云,掉图有点严重,笔者也是无奈,只好更换成七牛云,,现在写这个,是为了记录,都是即将被清理的代码了。
七牛云请参考另外一篇随笔:https://www.cnblogs.com/xjf125/p/12329167.html