有许多用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(不幸的是,这是客观的,我会非常感激如果你可以帮我快速版......)。谢谢!
==== EDIT
我一直在关注@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
}
但我对这一行有疑问:
let imageURL = NSBundle.mainBundle().URLForResource("image", withExtension: ext)!
当我在这里只有imageView.image = image
时,如何获得imageURL?
答案 0 :(得分:3)
答案 1 :(得分:3)
根据@ the-pantless-coder的输入,您可以将图像保存到临时文件中,并在上传完成后删除文件。
尝试:
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
let path = NSTemporaryDirectory().stringByAppendingString("image.jpeg")
if let data = UIImageJPEGRepresentation(image, 0.8) {
data.writeToFile(path, atomically: true)
}
self.dismissViewControllerAnimated(true, completion: {})
let credentialsProvider = AWSCognitoCredentialsProvider(regionType:CognitoRegionType,
identityPoolId:CognitoIdentityPoolId)
let configuration = AWSServiceConfiguration(region:CognitoRegionType, credentialsProvider:credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
let ext = "jpeg"
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest.body = path
uploadRequest.key = NSProcessInfo.processInfo().globallyUniqueString + "." + ext
uploadRequest.bucket = S3BucketName
uploadRequest.contentType = "image/" + ext
}