阿里云
发表主题 回复主题
  • 113阅读
  • 0回复

[求助]iOS阿里云上传图片不成功

级别: 新人
发帖
1
云币
2
;%(sbA  
l&qyLL2 w  
func ossUpload(_ image: UIImage, bucket: String, callback: @escaping (_ result: Any?, _ error: Any?) -> Void) { (*=>YE'V{  
    var originData: Data? /W-ges  
    var signType: String? =nc;~u|]  
    if let data: Data = UIImagePNGRepresentation(image) { X%znNx  
        originData = data O(2c_!d  
        signType = "image/png" )=X g  
    } else if let data: Data = UIImageJPEGRepresentation(image, 1.0) { P0(LdZH6u  
        originData = data 9}a$0H h  
        signType = "image/jpeg" vnVT0)Lel  
    } else { 4X7J~  
        originData = nil J$5 G8<d>  
        signType = nil MBw;+'93qf  
    } zh0T3U0D  
     1 I.P7_/  
    if originData == nil || signType == nil { hcRe,}wJ  
        return ti6X=@ P:  
    } oqysfLJ  
r-xP 6  
(~{Y}n]s  
    let dateFormatter = DateFormatter() X$1YvYsID  
    dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss" =[!(s/+>L  
    dateFormatter.timeZone = NSTimeZone(name: "UTC")! as TimeZone 2gkN\w6zQ  
    dateFormatter.locale = Locale(identifier: "en-US") ~/)]`w  
    let date = dateFormatter.string(from: Date()) 3h>L0  
     H lM7^3(&  
    let digest = OSSUtil.dataMD5String(originData!) <;W-!R759  
    let sign = "PUT\n\(digest!)\n\(signType!)\n\(date) GMT" %C qp88]  
1W\wIj.  
dR< d7  
    HWNetwork.request(target: .autograph(md5File: digest!, name: "123.jpg", mime: sign, type: "800"), success: { (result) in kRwUR34yc  
        let json = JSON(result) ^,^MW  
        print("json===\(json)") //ios-upload-image chUYLX}45  
        if let path = json["data"]["path"].string{ ENr#3+m$;  
            callback(path, nil) XMkRYI1~  
            return czu9a"M>X  
        } /FC HF#yK  
        if let bucket = json["data"]["bucket"].string{ ru9@|FgAE  
            if let graph = json["data"]["graph"].string{ ZYY2pY 1  
                  ^,ISz-4  
                let endpoint = "https://oss-cn-shenzhen.aliyuncs.com" |?{V-L  
o8<~zeI  
~+t@7A=  
                let provider = OSSCustomSignerCredentialProvider.init(implementedSigner: { (result, error) -> String? in F@q9UlfB-  
                    return graph bQq/~  
                }) N*$GP3]  
||wi4T P  
{'wvb "b  
                let client = OSSClient(endpoint: endpoint, credentialProvider: provider!) g~.#.S ds  
                let put = OSSPutObjectRequest() <&) hg:  
                put.bucketName = bucket uHZ4 @ w:  
                 ;UpJ_y)n8\  
                if let url = json["data"]["url"].string{ 1Hr1Ir<KR  
     1 /{~t[*.  
                    put.objectKey = String(url[url.index(url.startIndex, offsetBy: 1)..<url.endIndex]) Q Kr/  
                    put.uploadingData = originData! 'fb\t,  
                    put.objectMeta = ["Date": "\(date) GMT"] ~BuBma_   
                     V-go?b`  
                    uploadQueue.async { 4XER 7c  
                         EZI#CLT[  
                        let task = client.putObject(put) F$ G)vskd  
                        task.continue({ (task:OSSTask) -> Any? in E%D.a=UX,  
                 O<L=N-  
                            if task.error == nil { .|go$}Fk  
                                callback(url, nil) |36%B7H  
                                App.apollo.perform(mutation: CreateAttachmentMutation(path: url, md5: digest!, size: Long(originData!.count), width: Long(image.size.width), height: Long(image.size.height), isImage: true)) { (_, _) in gU|:Y&lFZg  
                                    // pass /"k[T  
                                    print("success") g3[-[G^5  
                                } >]ux3F3\  
                            } else { rYdNn0mh k  
                                print("task error-----\(task.error!)") f9>pMfi:@  
                                callback(nil, "error") WLj]EsA.  
                            } F5cN F 5  
                            return nil uk$MQ v*D  
                        }).waitUntilFinished() l\V1c90m  
                    } ,qp8Rg|3j  
                } zo "L9&Hzo  
                 K*DH_\SPK  
            } qQDe'f~  
        } svEe@Kt`  
    }) { (error) in 7"aN7Q+EbI  
        callback(nil, error) |?^qs nB  
    } PH8 88O  
} ?K2EK'-q  
Pm2LB<qS  
T>(X`(  
b~tu;:  
P#!g P3  
这是我的json打印信息 JSju4TQ4  
mUP!jTF  
json==={ ?b(wZ-/  
  "code" : 0, u5rvrn ]  
  "data" : { yZp/P%y  
    "graph" : "OSS LTAIBlPDFHyjlxfD:PsbFOVd86djrovoyS3eHpc54bu0=", %Ul,9qG+  
    "url" : "\/icon\/3569686715190237_1531302723861.jpg", = )3\B  
    "bucket" : "huiwan-test" Gq[5H(0/c  
  }, s92ol0`  
  "message" : "success" =d"5k DK-m  
} <Bn0wr8)\  
d ]|K%<+(  
! a\v)R  
这是报错,不知道签名哪不匹配了。似乎动被截取了一部分? (c}!gjm  
kzCJs  
%4M,f.[e  
task error-----Error Domain=com.aliyun.oss.serverError Code=-403 "(null)" UserInfo={__name=Error, HostId=huiwan-test.oss-cn-shenzhen.aliyuncs.com, Code=SignatureDoesNotMatch, OSSAccessKeyId=LTAIBlPDFHyjlxfD, RequestId=5B45D3448EE2472B572FAEDA, StringToSign=PUT Ue;Z)}  
;(}~m&p  
T<Y*();Zo  
image/jpeg > JA-G@3i  
Wed, 11 Jul 2018 09:52:03 GMT YGQ/zB^Pj  
IOxtuR  
/huiwan-test/icon/3569686715190237_1531302723861.jpg, StringToSignBytes=50 55 54 0A 0A 69 6D 61 67 65 2F 6A 70 65 67 0A 57 65 64 2C 20 31 31 20 4A 75 6C 20 32 30 31 38 20 30 39 3A 35 32 3A 30 33 20 47 4D 54 0A 2F 68 75 69 77 61 6E 2D 74 65 73 74 2F 69 63 6F 6E 2F 33 35 36 39 36 38 36 37 31 35 31 39 30 32 33 37 5F 31 35 33 31 33 30 32 37 32 33 38 36 31 2E 6A 70 67, SignatureProvided=PsbFOVd86djrovoyS3eHpc54bu0=, Message=The request signature we calculated does not match the signature you provided. Check your key and signing method.} kUGFg{"  
-"H$ &p~  
发表主题 回复主题
« 返回列表上一主题下一主题

限100 字节
批量上传需要先选择文件,再选择上传
 
验证问题: 64 + 16 = ?
上一个 下一个