在Swift4.0中用AWS上传图片

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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,120评论 19 139
  • 很烦躁,无数定下计划都被搁浅
    兔昵阅读 1,450评论 0 0
  • 2016年5月21号 在一起的六年零二十一天 异地恋的第五个年头开始 异地恋,并没有太辛苦,两个人...
    肉肉姑娘阅读 3,751评论 1 1
  • 以前听过一个狗血鸡汤故事,说事业有成的男人总是深得下属爱慕,并且觉得家里的黄脸婆乏味起来,于是掐着时间和女下属各种...
    白堤流连阅读 1,490评论 0 0

友情链接更多精彩内容