|
@@ -13,9 +13,12 @@ let kAliyunOSSAccessKeyId: String = "LTAIG2eCY5UdheM1"
|
|
|
let kAliyunOSSAccessKeySecret: String = "4RJTcJkZsOJIby2oewUFHqUJxUR1vm"
|
|
|
let kAliyunOSSBucketPublic: String = "uptoyo"
|
|
|
let kAliyunOSSBucketPrivate: String = "uptoyo"
|
|
|
-let kAliyunOSSEndpoint: String = "http://oss-cn-zhangjiakou.aliyuncs.com"
|
|
|
+let kAliyunOSSEndpoint: String = "https://oss-cn-zhangjiakou.aliyuncs.com"
|
|
|
+let kAliyunOSSImageEndpoint: String = "http://oss.uptoyo.com"
|
|
|
let kAliyunOSSSecurityToken: String = "SecurityToken"
|
|
|
|
|
|
+
|
|
|
+
|
|
|
typealias AliyunOSSManagerProgressBlock = (Float) -> Void
|
|
|
typealias AliyunOSSManagerResultBlockUrl = (Bool, String?) -> Void
|
|
|
typealias AliyunOSSManagerResultBlockUrls = (Bool, Array<String>?) -> Void
|
|
@@ -33,23 +36,27 @@ class AliyunOSSManager: NSObject {
|
|
|
var configOSSStsModel : ConfigOSSStsModel?
|
|
|
|
|
|
/// 初始化AliyunOSS
|
|
|
- func initAliyunOSSManager() {
|
|
|
+ func initAliyunOSSManager(completion: @escaping successCallBack) {
|
|
|
|
|
|
SwiftMoyaNetWorkServiceConfig.shared().configOSSStsApi(completion: {
|
|
|
[weak self] (configOSSStsModel) -> (Void) in
|
|
|
self?.configOSSStsModel = configOSSStsModel as? ConfigOSSStsModel
|
|
|
// 1. credential
|
|
|
- let credential = OSSStsTokenCredentialProvider(accessKeyId: self?.configOSSStsModel?.accessKeyId ?? kAliyunOSSAccessKeyId, secretKeyId: self?.configOSSStsModel?.accessKeySecret ?? kAliyunOSSAccessKeySecret, securityToken: self?.configOSSStsModel?.stsToken ?? kAliyunOSSSecurityToken)
|
|
|
+ let credential = OSSStsTokenCredentialProvider(accessKeyId: self?.configOSSStsModel?.accessKeyId ?? "", secretKeyId: self?.configOSSStsModel?.accessKeySecret ?? "", securityToken: self?.configOSSStsModel?.stsToken ?? "")
|
|
|
+
|
|
|
+
|
|
|
// 2. OSSClient配置参数
|
|
|
let configuration = OSSClientConfiguration()
|
|
|
configuration.maxRetryCount = 3
|
|
|
configuration.timeoutIntervalForRequest = 20
|
|
|
- configuration.isHttpdnsEnable = false
|
|
|
- configuration.crc64Verifiable = true
|
|
|
+// configuration.isHttpdnsEnable = false
|
|
|
+// configuration.crc64Verifiable = false
|
|
|
// 3. 初始OSS化客户端
|
|
|
- self?.client = OSSClient(endpoint: self?.configOSSStsModel?.region ?? kAliyunOSSEndpoint, credentialProvider: credential, clientConfiguration: configuration)
|
|
|
+ self?.client = OSSClient(endpoint: kAliyunOSSEndpoint, credentialProvider: credential, clientConfiguration: configuration)
|
|
|
+ completion(nil)
|
|
|
|
|
|
}) { (loadingStatus) in
|
|
|
+ SwiftProgressHUD.shared().showText("获取token失败")
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -66,8 +73,9 @@ class AliyunOSSManager: NSObject {
|
|
|
progressBlock:@escaping AliyunOSSManagerProgressBlock)-> OSSPutObjectRequest {
|
|
|
|
|
|
// 0.初始化客户端
|
|
|
- initAliyunOSSManager()
|
|
|
-
|
|
|
+ initAliyunOSSManager { (_) -> (Void) in
|
|
|
+
|
|
|
+ }
|
|
|
// 1.上传请求
|
|
|
let put = OSSPutObjectRequest()
|
|
|
|
|
@@ -75,7 +83,7 @@ class AliyunOSSManager: NSObject {
|
|
|
put.bucketName = configOSSStsModel?.bucket ?? ""
|
|
|
|
|
|
// 1.2.文件名路径
|
|
|
- put.objectKey = PathManager.randomString() ?? ""
|
|
|
+ put.objectKey = AliyunOSSManager.objectKey()
|
|
|
|
|
|
// 1.3.上传对象(图片data)
|
|
|
put.uploadingData = Data.imageCompressForSize(sourceImage: image, targetPx: 750.0) ?? image.pngData()!
|
|
@@ -116,10 +124,7 @@ class AliyunOSSManager: NSObject {
|
|
|
NXLLog("上传成功")
|
|
|
DispatchQueue.main.async {
|
|
|
// 上传成功后的图片UrlStr
|
|
|
- let urlSting = ""
|
|
|
-// http:\/\/vod.uptoyo.com\/image\/default\/7D840D1A922B4721B57C64868F6A4156-6-2.jpg
|
|
|
-// NSString *str=[[ENDPOINTIMAGE componentsSeparatedByString:@"//"] lastObject];
|
|
|
-// NSString *urlString=[NSString stringWithFormat:@"https://%@.%@/%@",PUBLIC_BUCKET,str,put.objectKey];
|
|
|
+ let urlSting = kAliyunOSSImageEndpoint + "/" + put.objectKey
|
|
|
resultBlockUrl(true,urlSting)
|
|
|
}
|
|
|
}
|
|
@@ -149,10 +154,8 @@ class AliyunOSSManager: NSObject {
|
|
|
NXLLog("上传成功")
|
|
|
DispatchQueue.main.async {
|
|
|
// 上传成功后的图片UrlStr
|
|
|
- let urlSting = ""
|
|
|
- // http:\/\/vod.uptoyo.com\/image\/default\/7D840D1A922B4721B57C64868F6A4156-6-2.jpg
|
|
|
- // NSString *str=[[ENDPOINTIMAGE componentsSeparatedByString:@"//"] lastObject];
|
|
|
- // NSString *urlString=[NSString stringWithFormat:@"https://%@.%@/%@",PUBLIC_BUCKET,str,put.objectKey];
|
|
|
+ // 上传成功后的图片UrlStr
|
|
|
+ let urlSting = kAliyunOSSImageEndpoint + "/" + put.objectKey
|
|
|
resultBlockUrl(true,urlSting)
|
|
|
}
|
|
|
}else {
|
|
@@ -178,74 +181,101 @@ class AliyunOSSManager: NSObject {
|
|
|
resultBlockUrls:@escaping AliyunOSSManagerResultBlockUrls,
|
|
|
isAsync:Bool) {
|
|
|
// 0.初始化客户端
|
|
|
- initAliyunOSSManager()
|
|
|
- // 1.线程队列
|
|
|
- let queue = OperationQueue()
|
|
|
- // 1.1最大线程数
|
|
|
- queue.maxConcurrentOperationCount = images.count
|
|
|
- var imageUrls = Array<String>()
|
|
|
-
|
|
|
- for image in images {
|
|
|
- //创建operation
|
|
|
- let operation = BlockOperation { [weak self] () -> Void in
|
|
|
- // 1.上传请求
|
|
|
- let put = OSSPutObjectRequest()
|
|
|
-
|
|
|
- // 1.1.设置bucketName (文件夹名,与OSS对应)
|
|
|
- put.bucketName = self?.configOSSStsModel?.bucket ?? ""
|
|
|
-
|
|
|
- // 1.2.文件名路径
|
|
|
- put.objectKey = PathManager.randomString() ?? ""
|
|
|
-
|
|
|
- // 图片url
|
|
|
-// NSString *encodingString = [[NSString stringWithFormat:@"http://kachamao.oss-cn-shanghai.aliyuncs.com/%@",objectName]stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
|
|
|
-// [callBackNames addObject: encodingString];
|
|
|
- imageUrls.append("图片url")
|
|
|
-
|
|
|
- // 1.3.上传对象(图片data)
|
|
|
- put.uploadingData = Data.imageCompressForSize(sourceImage: image, targetPx: 1280.0) ?? image.pngData()!
|
|
|
-
|
|
|
- // 1.4.上传进度
|
|
|
-// put.uploadProgress = { (bytesSent , totalByteSent , totalBytesExpectedToSend) in
|
|
|
-// let progress = totalByteSent/totalBytesExpectedToSend
|
|
|
-// progressBlock(Float(progress))
|
|
|
-// }
|
|
|
-
|
|
|
- // 1.5.上传
|
|
|
- let putTast = self?.client?.putObject(put)
|
|
|
-
|
|
|
- putTast?.continue({ (task) -> Any? in
|
|
|
- if (task.error != nil) {
|
|
|
- NXLLog("上传失败:\(String(describing: task.error))")
|
|
|
- }else {
|
|
|
- let result = task.result as? OSSPutObjectResult
|
|
|
- NXLLog("上传成功")
|
|
|
- NXLLog("Result - requestId: \(result?.requestId ?? ""),headerFields:\(String(describing: result?.httpResponseHeaderFields))")
|
|
|
-
|
|
|
- }
|
|
|
- return nil
|
|
|
- }) // // 阻塞直到上传完成
|
|
|
-
|
|
|
- // 异步
|
|
|
- if isAsync {
|
|
|
- if image == images.last {
|
|
|
- NXLLog("上传完成")
|
|
|
- resultBlockUrls(true,imageUrls)
|
|
|
+ initAliyunOSSManager { (_) -> (Void) in
|
|
|
+ // 1.线程队列
|
|
|
+ let queue = OperationQueue()
|
|
|
+ // 1.1最大线程数
|
|
|
+ queue.maxConcurrentOperationCount = images.count
|
|
|
+ var imageUrls = Array<String>()
|
|
|
+
|
|
|
+ for image in images {
|
|
|
+ //创建operation
|
|
|
+ let operation = BlockOperation { [weak self] () -> Void in
|
|
|
+ // 1.上传请求
|
|
|
+ let put = OSSPutObjectRequest()
|
|
|
+
|
|
|
+ // 1.1.设置bucketName (文件夹名,与OSS对应)
|
|
|
+ put.bucketName = self?.configOSSStsModel?.bucket ?? ""
|
|
|
+
|
|
|
+ // 1.2.文件名路径
|
|
|
+ put.objectKey = AliyunOSSManager.objectKey()
|
|
|
+
|
|
|
+ // 1.3.上传对象(图片data)
|
|
|
+ put.uploadingData = Data.imageCompressForSize(sourceImage: image, targetPx: 1280.0) ?? image.pngData()!
|
|
|
+
|
|
|
+ // 图片url
|
|
|
+ // 上传成功后的图片UrlStr
|
|
|
+ let urlSting = kAliyunOSSImageEndpoint + "/" + put.objectKey
|
|
|
+ imageUrls.append(urlSting)
|
|
|
+
|
|
|
+ // 1.4.上传进度
|
|
|
+ // put.uploadProgress = { (bytesSent , totalByteSent , totalBytesExpectedToSend) in
|
|
|
+ // let progress = totalByteSent/totalBytesExpectedToSend
|
|
|
+ // progressBlock(Float(progress))
|
|
|
+ // }
|
|
|
+
|
|
|
+ // 1.5.上传
|
|
|
+ let putTast = self?.client?.putObject(put)
|
|
|
+
|
|
|
+ putTast?.continue({ (task) -> Any? in
|
|
|
+ if (task.error != nil) {
|
|
|
+ NXLLog("上传失败:\(String(describing: task.error))")
|
|
|
+
|
|
|
+ }else {
|
|
|
+ let result = task.result as? OSSPutObjectResult
|
|
|
+ NXLLog("上传成功")
|
|
|
+ NXLLog("Result - requestId: \(result?.requestId ?? ""),headerFields:\(String(describing: result?.httpResponseHeaderFields))")
|
|
|
+
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ }).waitUntilFinished()
|
|
|
+
|
|
|
+ // 异步
|
|
|
+ if isAsync {
|
|
|
+ if image == images.last {
|
|
|
+ NXLLog("上传完成")
|
|
|
+ resultBlockUrls(true,imageUrls)
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+ }
|
|
|
+ if queue.operations.count != 0 {
|
|
|
+ operation.addDependency(queue.operations.last!)
|
|
|
}
|
|
|
|
|
|
- }
|
|
|
- if queue.operations.count != 0 {
|
|
|
- operation.addDependency(queue.operations.last!)
|
|
|
+ queue.addOperation(operation)
|
|
|
}
|
|
|
|
|
|
- queue.addOperation(operation)
|
|
|
+ //同步
|
|
|
+ if !isAsync {
|
|
|
+ queue.waitUntilAllOperationsAreFinished()
|
|
|
+ resultBlockUrls(true,imageUrls)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- //同步
|
|
|
- if !isAsync {
|
|
|
- queue.waitUntilAllOperationsAreFinished()
|
|
|
- resultBlockUrls(true,imageUrls)
|
|
|
- }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+extension AliyunOSSManager {
|
|
|
+
|
|
|
+
|
|
|
+ /// 图片路径
|
|
|
+ static func objectKey() -> String {
|
|
|
+
|
|
|
+ /// 时间文件夹
|
|
|
+ let dateStr = Date.dateToString(Date(), dateFormat: "yyyyMM")
|
|
|
+
|
|
|
+ /// 时间戳
|
|
|
+ let dateTimeStamp = Date().timeStamp()
|
|
|
+
|
|
|
+ /// 随机字符串
|
|
|
+ let randomString = PathManager.randomString()!
|
|
|
+
|
|
|
+ /// 图片名
|
|
|
+ let imgName = "\(Int(dateTimeStamp))" + "_" + randomString + ".png"
|
|
|
+
|
|
|
+ /// 路径
|
|
|
+ let objectKey = dateStr + "/" + imgName
|
|
|
+ return objectKey
|
|
|
}
|
|
|
}
|