【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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值