Created
April 14, 2016 19:31
-
-
Save roma86/d31557ca66e53e7151d88a6342af365e to your computer and use it in GitHub Desktop.
AWS S3 iOS SDK upload file helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Created by Georg Romas on 14/04/16. | |
Convenient class method to upload local file to AWS S3 bucket | |
Example usage: | |
AWSS3TransferManager.uploadFile( | |
url, | |
bucketName: "storage", | |
destinationFileName: "images/client/reviews/\(NSUUID().UUIDString).jpg", | |
contentType: "image/jpeg", | |
completeCallback: { [unowned self] (task) -> AnyObject? in | |
if let error = task.error { | |
log.error("s3 upload error: \(error)") | |
// Notify user about error | |
return nil | |
} | |
if let result = task.result as? AWSS3TransferManagerUploadOutput { | |
let cloudFronUrl = "https://XXXXXXXX.cloudfront.net/\(fileName)" | |
log.info("s3 upload success: \(result) | cloudfront url should be \(cloudFronUrl)") | |
// Save cloudFronUrl as new file path | |
} | |
return nil | |
}) | |
*/ | |
import Foundation | |
import AWSS3 | |
extension AWSS3TransferManager { | |
class func uploadFile( | |
localFileUrl: NSURL, | |
bucketName: String, | |
destinationFileName: String, | |
contentType: String, | |
completeCallback: ((AWSTask) -> AnyObject?) | |
) { | |
let transferManager = AWSS3TransferManager.defaultS3TransferManager() | |
let uploadRequest = AWSS3TransferManagerUploadRequest() | |
uploadRequest.bucket = bucketName | |
uploadRequest.key = destinationFileName | |
uploadRequest.body = localFileUrl | |
uploadRequest.contentType = contentType | |
uploadRequest.contentEncoding = nil | |
transferManager | |
.upload(uploadRequest) | |
.continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: completeCallback) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment