简体   繁体   中英

How to upload the image to amazon bucket in swift3?

I want to post the customer signature image to amazon s3 bucket_type server with an api...I am referring with different tutorials...I didn't get enough information from any of tutorials...I am trying with following code but I am getting nil result...

    funcUpload(service:String,imagedata:Data,completion:@escaping (_ result:NSDictionary?,_ error:AnyObject?) -> ()){           
 let boundaryConstant  = "----------V2y2HFg03eptjbaKO0j1"
     let urlpath:String = "http:myurl" + service        
    let url:NSURL = NSURL(string:urlpath)!
    let request = NSMutableURLRequest(url: url as URL)
    request.httpMethod = "P0ST"
    request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringLocalCacheData

    request.httpShouldHandleCookies = false
    let contentType = "multipart/form-data; boundary=\(boundaryConstant)"        
    request.setValue(contentType, forHTTPHeaderField: "Content-Type"
    let body = NSMutableData()
     if imagedata != nil{
        body.append("--\(boundaryConstant)\r\n".data(using: String.Encoding.utf8)!)           
        body.append("Content-Disposition: form-data;   name=\"\("universalapparealproduct")\" ; filename=\"image.jpg\"\r\n".data(using: String.Encoding.utf8)!)
        body.append("Content-Type: \("universalapparealproduct")\r\n\r\n".data(using: String.Encoding.utf8)!)
        body.append(imagedata)
        body.append("\r\n".data(using: String.Encoding.utf8)!)         
        }
        body.append("--\(boundaryConstant)--\r\n".data(using:      String.Encoding.utf8)!)
        request.httpBody  = body as Data       
        let config = URLSessionConfiguration.ephemeral
        self.session = URLSession(configuration: config)
        let task = session.dataTask(with: request as URLRequest) {     (data, response, error) -> Void in
        if let receivedData = data
       {                      
        do
       {let resultDic = try JSONSerialization.jsonObject(with:   receivedData, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
           completion(resultDic!,nil)
        }
        catch let error
        {
            NSLog("\(error)")
        }
    }
    else if let errorMessage = error
    {
        completion(nil,errorMessage as AnyObject?)
    }
            self.session.invalidateAndCancel()
    }           
    task.resume()
    }
}

Please can any one help me to resolve this issue. Thanks in advance.

  1. let urlpath:String = "http:myurl" + service looks like a typo.
  2. I assume that the service variable contain a url with pre-signed credentials that you can upload to. Make sure that is correct. If not you have to have a server serve you pre-signed credentials.
  3. use PUT not POST
  4. I would expect that the body of the request would just be the imageData, but you seem to be adding a lot of stuff to body. I don't know what kind of file you want to upload but you are making a weird file. If that is your intent is fine, but just we aware that is what you are doing. If indenting to upload a custom file type, I would move all of that code to a different function and then have another function that just uploads the data.
  5. the only HTTPHeader field that you have to set is Content-Length . It must be set to the size of the body that you send.
  6. The expected result of the request is data with a length of 0 and no error. So your JSONSerialization in the response block in wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM