阿里云
1024开发者盛宴之Java专家问答专场
发表主题 回复主题
  • 540阅读
  • 0回复

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

级别: 新人
发帖
1
云币
2
T% /xti5$!  
oTplxF1  
func ossUpload(_ image: UIImage, bucket: String, callback: @escaping (_ result: Any?, _ error: Any?) -> Void) { )OW(T^>_'I  
    var originData: Data? 4yJ*85e]  
    var signType: String? b0~H>cnA  
    if let data: Data = UIImagePNGRepresentation(image) { .IW_DM-  
        originData = data 0)6i~MglY  
        signType = "image/png" wW6mYgPN%  
    } else if let data: Data = UIImageJPEGRepresentation(image, 1.0) { Rye ~w6  
        originData = data uQ)JC 7b\  
        signType = "image/jpeg" pJ 2:` f<;  
    } else { j#Ky0+@V  
        originData = nil &;~2sEo,  
        signType = nil p]1yd;Jt  
    } ei+9G,  
     v -|P_O&z  
    if originData == nil || signType == nil { TQOg~lH  
        return E1U4v&P  
    } B"?+5A7  
U- *8%>Qp  
^LEmi1L  
    let dateFormatter = DateFormatter() (T%F!2i([U  
    dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss" s{1Deek=  
    dateFormatter.timeZone = NSTimeZone(name: "UTC")! as TimeZone @aqd'O  
    dateFormatter.locale = Locale(identifier: "en-US") |%2/I>o  
    let date = dateFormatter.string(from: Date()) P*0nT  
     'o)ve(  
    let digest = OSSUtil.dataMD5String(originData!) m! '1$G  
    let sign = "PUT\n\(digest!)\n\(signType!)\n\(date) GMT" c5wkzY h  
Bah.\ZsYQP  
%0~wtZH_!  
    HWNetwork.request(target: .autograph(md5File: digest!, name: "123.jpg", mime: sign, type: "800"), success: { (result) in tz0Ttu=xH  
        let json = JSON(result) |$SvD2^  
        print("json===\(json)") //ios-upload-image ulPrb>i  
        if let path = json["data"]["path"].string{ 8~[C'+r  
            callback(path, nil) QNa}M{5>h  
            return qm'C^ X?  
        } ]\D6;E8P-~  
        if let bucket = json["data"]["bucket"].string{ CSX$Pk*  
            if let graph = json["data"]["graph"].string{ [b k&Nd[  
                 8SCW.;0  
                let endpoint = "https://oss-cn-shenzhen.aliyuncs.com" FbCZV3Y  
Fzld0p9=  
Lx:N!RDw  
                let provider = OSSCustomSignerCredentialProvider.init(implementedSigner: { (result, error) -> String? in aWsKJo>j[#  
                    return graph @aD~YtL"n  
                }) ;~@PYIp  
{!g.255+  
<]e;tF)+  
                let client = OSSClient(endpoint: endpoint, credentialProvider: provider!) ge {4;,0=  
                let put = OSSPutObjectRequest() dzv,)X  
                put.bucketName = bucket  hL{B9?  
                 S7~F*CGBh  
                if let url = json["data"]["url"].string{ o&F.mYnqX  
     j_WF38o  
                    put.objectKey = String(url[url.index(url.startIndex, offsetBy: 1)..<url.endIndex]) CG>2 ,pP,  
                    put.uploadingData = originData! ^R'!\m|FR  
                    put.objectMeta = ["Date": "\(date) GMT"] L{0OMyUA  
                     -0r "#48(%  
                    uploadQueue.async { 4b(irDT3F  
                         KW6" +,Th  
                        let task = client.putObject(put) 3N%Ev o  
                        task.continue({ (task:OSSTask) -> Any? in XK/@!ud"`  
                 .x$T a l  
                            if task.error == nil { KP7RrgOan&  
                                callback(url, nil) *4^]?Y\*  
                                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 _>m*`:Wb  
                                    // pass pVa|o&,  
                                    print("success") 3cFLU^  
                                } c^WBB$v  
                            } else { xAz4ZXj=q  
                                print("task error-----\(task.error!)") r~2@#gTbl  
                                callback(nil, "error") %y\7  
                            } \fR:+rbQ&|  
                            return nil [k=9 +0p  
                        }).waitUntilFinished() nN\XVGP,t  
                    } r= | |sZs  
                } p33GKg0i+(  
                 >~>[}d;glw  
            } x,c68Q)g  
        } gO%i5  
    }) { (error) in /aa;M*Qp  
        callback(nil, error) 5XUI7Q%  
    } ;k%sKVP  
} ZWW8Hr  
GXK?7S0H  
}o:sx/=u_  
YY<?w  
']Xx#U N  
这是我的json打印信息 =<h=">}5'  
64>[pZF8  
json==={ 4H? Ma|,  
  "code" : 0, HbSx}bM_9  
  "data" : { _rJ SkZO  
    "graph" : "OSS LTAIBlPDFHyjlxfD:PsbFOVd86djrovoyS3eHpc54bu0=", g uWqHVSs  
    "url" : "\/icon\/3569686715190237_1531302723861.jpg", ^JDV4>S\  
    "bucket" : "huiwan-test" ~ A|*]0,  
  }, 6i9I 4*'  
  "message" : "success" (3r,PS@Qq@  
} jt r=8OiL  
4:PP[2?  
'|?r&-5 h  
这是报错,不知道签名哪不匹配了。似乎动被截取了一部分? #}yFHM?i  
,VsCRp  
X*"O'XCA  
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 XJ?z{gXJ  
6yM dl~.  
1H 6Wrik  
image/jpeg 8HA=O ?Cg  
Wed, 11 Jul 2018 09:52:03 GMT B :.;:AEbT  
.Awq(  
/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.} |?=1tS{iT  
PW4Wn`u  
发表主题 回复主题
« 返回列表上一主题下一主题

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