Skip to content

Instantly share code, notes, and snippets.

@thanakijwanavit
Created September 2, 2020 13:11
Show Gist options
  • Save thanakijwanavit/0950c6703cc180202f2aab6f3b52975e to your computer and use it in GitHub Desktop.
Save thanakijwanavit/0950c6703cc180202f2aab6f3b52975e to your computer and use it in GitHub Desktop.
upload to s3 with accelerated endpoint swift
// initiation in appdelegate
let east1Configuration = AWSServiceConfiguration(
region: AWSRegionType.USEast1,
credentialsProvider: credentialsProvider
)!
let largeImageConfiguration = AWSS3TransferUtilityConfiguration()
largeImageConfiguration.isAccelerateModeEnabled = true
AWSS3TransferUtility.register(with: east1Configuration,
transferUtilityConfiguration: inputImageConfiguration,
forKey: S3ConfigurationKey.smallImage.rawValue)
// function definition
static func uploadData(data:Data, uploadPath:String, bucket:String, endpoint:S3ConfigurationKey){
let expression = AWSS3TransferUtilityUploadExpression()
expression.progressBlock = {(task, progress) in
debugPrint(task, progress)
}
var completionHandler: AWSS3TransferUtilityUploadCompletionHandlerBlock?
completionHandler = {(task, error) -> Void in
guard error == nil else {debugPrint(error!); return}
debugPrint("task upload completed")
}
let transferUtility = AWSS3TransferUtility.s3TransferUtility(forKey: endpoint.rawValue)!
transferUtility.uploadData(data,bucket: bucket, key: uploadPath, contentType: "text/plain", expression: expression, completionHandler: completionHandler).continueWith {
(task) -> AnyObject? in
guard task.error == nil else {debugPrint(task.error!); return nil}
if let result = task.result {
debugPrint(result)
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment