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

use*_*930 6 amazon-s3 amazon-web-services ios swift

用obj C写的有很多很多例子,但我正在寻找一个Swift解决方案.到目前为止,我能找到的只有这一个https://github.com/awslabs/aws-sdk-ios-samples/tree/master/S3TransferManager-Sample/Swift但对我来说并不是那么清楚.

我已经配置了s3on aws网页,我也创建并填充了文件Constans.swift:

import AWSS3
import Foundation

let CognitoRegionType = AWSRegionType.XXXXX  
let DefaultServiceRegionType = AWSRegionType.XXXXX 
let CognitoIdentityPoolId = "MyCognitoIdentityPoolId"
let S3BucketName = "MyS3BucketName"
Run Code Online (Sandbox Code Playgroud)

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

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

我还有一个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: {})

}
Run Code Online (Sandbox Code Playgroud)

现在我想要的是添加一个新按钮,负责将这张照片上传到我的s3存储桶,就像在本教程中一样:https://www.youtube.com/watch?v = WM54fH8AFUk(不幸的是它在目标c中)在这里,如果你能用快速的版本帮助我,我将非常感激......).谢谢!

====编辑

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

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, 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
    }
Run Code Online (Sandbox Code Playgroud)

但是这行有问题:

let imageURL = NSBundle.mainBundle().URLForResource("image", withExtension: ext)!
Run Code Online (Sandbox Code Playgroud)

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

小智 3

请确保您有一个桥接标头,您可以在其中导入适当的 AWS 标头。GitHub上有一个示例。

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

-罗汉