如何将我的ios应用程序中的图像上传到存储在aws s3上的存储桶中,并在Swift中完成所有这些操作?

前端之家收集整理的这篇文章主要介绍了如何将我的ios应用程序中的图像上传到存储在aws s3上的存储桶中,并在Swift中完成所有这些操作?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在obj C中有很多很多例子,但我正在寻找一个 Swift解决方案.到目前为止,我所能找到的只是一个 https://github.com/awslabs/aws-sdk-ios-samples/tree/master/S3TransferManager-Sample/Swift,但对我来说并不是那么清楚.

我已经在aws网页上配置了s3,我还创建并填充了文件Constans.swift:

import AWSS3
import Foundation

let CognitoRegionType = AWSRegionType.XXXXX  
let DefaultServiceRegionType = AWSRegionType.XXXXX 
let CognitoIdentityPoolId = "MyCognitoIdentityPoolId"
let S3BucketName = "MyS3BucketName"

我还在AppDelegate.swift中添加了以下行:

let credentialsProvider = AWSCognitoCredentialsProvider(regionType: CognitoRegionType,identityPoolId: CognitoIdentityPoolId)
let configuration = AWSServiceConfiguration(region: DefaultServiceRegionType,credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration

我还有一个Swift课程,带有一个按钮和一个图像视图控制器,到目前为止,当我点击按钮时,我可以从画廊或相机拍摄照片,它显示在图像视图上.这是我的代码负责:

@IBOutlet weak var imageView: UIImageView!

@IBAction func captureImage(sender: AnyObject) {

    let imageFromSource = UIImagePickerController()
    imageFromSource.delegate = self
    imageFromSource.allowsEditing = false

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){

        imageFromSource.sourceType = UIImagePickerControllerSourceType.Camera

    }
    else{

        imageFromSource.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

    }

    self.presentViewController(imageFromSource,animated: true,completion: nil)

}

func imagePickerController(picker: UIImagePickerController,didFinishPickingImage image: UIImage,editingInfo: [String : AnyObject]?) {

    imageView.image = image
    self.dismissViewControllerAnimated(true,completion: {})

}

现在我想是添加一个新的按钮,将负责在本教程中上传照片到我的S3存储,是这样的:https://www.youtube.com/watch?v=WZ54fH8AFUk(不幸的是它在目标C在这里,我会很感激,如果你能帮助我一个迅捷版……).谢谢!

====编辑

我一直在关注@the_pantless_coder发布的教程(这一个https://www.codementor.io/tips/5748713276/how-to-upload-images-to-aws-s3-in-swift),我决定修改我现有的方法imagePickerController,到目前为止它看起来像这样:

func imagePickerController(picker: UIImagePickerController,editingInfo: [String : AnyObject]?) {

        imageView.image = image
        self.dismissViewControllerAnimated(true,completion: {})

        let credentialsProvider = AWSCognitoCredentialsProvider(regionType:CognitoRegionType,identityPoolId:CognitoIdentityPoolId)
        let configuration = AWSServiceConfiguration(region:CognitoRegionType,credentialsProvider:credentialsProvider)
        AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration

        let ext = "png"
        let imageURL = NSBundle.mainBundle().URLForResource("image",withExtension: ext)!

        let uploadRequest = AWSS3TransferManagerUploadRequest()
        uploadRequest.body = imageURL
        uploadRequest.key = NSProcessInfo.processInfo().globallyUniqueString + "." + ext
        uploadRequest.bucket = S3BucketName
        uploadRequest.contentType = "image/" + ext
    }

但是这行有问题:

let imageURL = NSBundle.mainBundle().URLForResource("image",withExtension: ext)!

当我在这里只有imageView.image = image时,如何获得imageURL?

解决方法

请确保您有一个桥接标头,您可以在其中导入相应的AWS标头. GitHub上有一个名为 here的例子.

这样做应该使S3方法可用.

-Rohan

猜你在找的iOS相关文章