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

  • 相关阅读:
    IIS7.5 部署WCF项目问题集锦
    C#制作“安装和部署”时,实现软件开机启动
    Strsafe.h:更安全的C语言字符串处理函数
    FMOD音频引擎简单使用
    您也使用托管C++吗?
    《Programming in Lua中文版》 8.Compilation, Execution, and Errors
    恶心的C语言strtok函数
    Lua一些基本函数
    Lua tables 分析1
    如何让EditPlus支持LUA(转)
  • 原文地址:https://www.cnblogs.com/xjf125/p/12359098.html
Copyright © 2020-2023  润新知