几个星期前,我花了一些时间来弄清楚,但我能够使用Akamai的iOS版Netstorage(Swift 2.0)上传文件。
这是我的代码。希望它可以帮助别人。
let kAKAMAI_CONFIGURATION_NETSTORAGE_USER_NAME = "netstorage_user_name"
let kAKAMAI_CONFIGURATION_NETSTORAGE_KEY = "---- YOU NETSTORAGE KEY ---"
let kAKAMAI_CONFIGURATION_NETSTORAGE_HOSTNAME = "xxxxxx.akamaihd.net"
let kAKAMAI_CONFIGURATION_NETSTORAGE_CP_CODE = "00000"
// MARK: File Manager
func getFileSize (filePath: String) -> UInt64 {
if let filePathAttributes:NSDictionary? = try? NSFileManager.defaultManager().attributesOfItemAtPath(filePath) {
return filePathAttributes!.fileSize();
}
return 0
}
func sendPictureToAkamaiNetStorageCDN(localImageFileLocation:String, onSuccessHandler:((fileUploadPath: String)->())? = nil, onErrorHandler:((NSError)->())? = nil) -> NSURLSessionDataTask? {
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session: NSURLSession? = NSURLSession(configuration: config, delegate: nil, delegateQueue: NSOperationQueue.mainQueue())
let request_time: NSTimeInterval = NSDate().timeIntervalSince1970
let uniqueID:String = NSUUID().UUIDString.lowercaseString
let auth_data:String = String(format: "5, 0.0.0.0, 0.0.0.0, %.0f, \(uniqueID), \(kAKAMAI_CONFIGURATION_NETSTORAGE_USER_NAME)", request_time)
let uniqueFileName:String = "\(uniqueID).jpg"
var akamaiNetStoragePartialPathURL:String = "/\(kAKAMAI_CONFIGURATION_NETSTORAGE_CP_CODE)/my_folder/%@"
let fileUploadURLPath: String = String(format:"https://%@\(akamaiNetStoragePartialPathURL)", kAKAMAI_CONFIGURATION_NETSTORAGE_HOSTNAME, uniqueFileName)
let action_data:String = String(format:"version=1&action=upload")
let headerFileName:String = String(format:"\(akamaiNetStoragePartialPathURL)", uniqueFileName)
let signature:String = String(format: "%@%@\nx-akamai-acs-action:%@\n", auth_data, headerFileName, action_data)
// For HMAC-SHA1 see https://gist.github.com/aldo-jlaurenstin/96db6f5c0e651732c842
let auth_sign = ALCommonHMAC.hmacSHA256(signature, withKey: kAKAMAI_CONFIGURATION_NETSTORAGE_KEY)
let request = NSMutableURLRequest(URL: NSURL(string: fileUploadURLPath)!)
request.HTTPMethod = "POST"
request.setValue(action_data, forHTTPHeaderField: "X-Akamai-ACS-Action")
request.setValue(auth_data, forHTTPHeaderField: "X-Akamai-ACS-Auth-Data")
request.setValue(auth_sign, forHTTPHeaderField: "X-Akamai-ACS-Auth-Sign")
request.setValue("image/jpeg", forHTTPHeaderField: "Content-Type")
request.setValue(String(format:"%i", self.getFileSize (localImageFileLocation)), forHTTPHeaderField: "Content-Length")
request.HTTPShouldHandleCookies = false
let url: NSURL = NSURL(fileURLWithPath: localImageFileLocation)
let task = session!.uploadTaskWithRequest(request, fromFile: url, completionHandler: {(data: NSData?, response: NSURLResponse?, error: NSError?) in
if data == nil {
// Handle error
}
else {
if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode == 200 {
// Upload successful
return
}
}
// If not code 200, there was an error
}
})
task.priority = NSURLSessionTaskPriorityHigh
task.resume()
return task
}
您可能需要更改一些内容(主要是您的上传路径),但此解决方案适用于我。
此致