Skip to content

Instantly share code, notes, and snippets.

@roma86
Created April 14, 2016 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roma86/d31557ca66e53e7151d88a6342af365e to your computer and use it in GitHub Desktop.
Save roma86/d31557ca66e53e7151d88a6342af365e to your computer and use it in GitHub Desktop.
AWS S3 iOS SDK upload file helper
/*
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