美文网首页
在Swift4.0中用AWS上传图片

在Swift4.0中用AWS上传图片

作者: Sultan | 来源:发表于2018-01-10 11:55 被阅读176次

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
           })
           }

github地址:https://github.com/zidong0822/AWS_UploadImage

相关文章

网友评论

      本文标题:在Swift4.0中用AWS上传图片

      本文链接:https://www.haomeiwen.com/subject/sifonxtx.html