• 【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

  • 相关阅读:
    thymeleaf 模板引擎
    thymeleaf 内联语法
    thymeleaf 局部变量、属性优先级、注释
    PYNQ上手笔记 | ① 启动Pynq
    MSP430F5529时钟系统深究
    C#上位机开发(四)—— SerialAssistant功能完善
    C#上位机开发(三)—— 构建SerialAssistant雏形
    C#上位机开发(二)—— Hello,World
    C#上位机开发(一)—— 了解上位机
    FPGA学习笔记(八)—— 状态机设计实例之独立按键消抖
  • 原文地址:https://www.cnblogs.com/xjf125/p/12359098.html
Copyright © 2020-2023  润新知