Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Swift 3 Upload file to Amazon S3 with pre-signed link
func upload(data: Data, urlString: String, mimeType: String, completion: @escaping (Bool, Error?) -> Void) {
let requestURL = URL(string: urlString)!
let client = AFHTTPSessionManager(baseURL: requestURL)
var request = URLRequest(url: requestURL)
request.httpMethod = "PUT"
request.httpBody = data
request.setValue(mimeType, forHTTPHeaderField: "Content-Type")
request.setValue("\(data.count)", forHTTPHeaderField: "Content-Length")
request.setValue("public-read", forHTTPHeaderField: "x-amz-acl")
let task = client?.dataTask(with: request, completionHandler: { (response, responseObject, error) in
print(response ?? "response nil")
print(error ?? "response nil")
completion(error == nil, error)
})
task?.resume()
}
func upload(image: UIImage, urlString: String, mimeType: String, completion: @escaping (Bool, Error?) -> Void) {
let data = UIImageJPEGRepresentation(image, 0.9)!
upload(data: data, urlString: urlString, mimeType: mimeType, completion: completion)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment