1.通过pod方式加载依赖库
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
def pods
pod 'AWSCore'
pod 'AWSS3'
end
target 'AWS_UploadImage' do
pods
end
2.在Brigde-Header.h文件中加入
#import <AWSCore/AWSCore.h>
#import <AWSS3/AWSS3.h>
3.初始化
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.usEast1, identityPoolId:"")
let configuration = AWSServiceConfiguration(region: AWSRegionType.usEast1, credentialsProvider: credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
4.上传图片
let transferManager = AWSS3TransferManager.default()
if let uploadRequest = AWSS3TransferManagerUploadRequest() {
uploadRequest.bucket = "microduino-apps" // 替换为存储桶名
uploadRequest.key = "test.jpg"
uploadRequest.acl = AWSS3ObjectCannedACL.publicRead
uploadRequest.body = URL(string:Bundle.main.resourcePath! + "/test.jpg")
transferManager?.upload(uploadRequest).continue({ (task) -> Any? in
if let error = task.error {
print("upload() failed: [\(error)]")
}
if let exception = task.exception {
print("upload() failed: [\(exception)]")
}
if task.result != nil {
print("upload successfully")
}
return nil
})
}
网友评论