在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?