我查看了许多亚马逊文档,但没有找到足够的信息来上传和下载图像到S3使用Swift。
我该怎么做?Swift - AWS S3从照片库上传图片并下载它
4
A
回答
8
做很多研究,我已经得到了这个工作后,
import AWSS3
import AWSCore
上传:
我假定你已经实现UIImagePickerControllerDelegate
了。
第1步:
举行URL创建变量:
var imageURL = NSURL()
创建上传完成处理的obj:
var uploadCompletionHandler: AWSS3TransferUtilityUploadCompletionHandlerBlock?
步骤2:从imagePickerController(_:didFinishPickingMediaWithInfo:)
获取图像URL:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]){
//getting details of image
let uploadFileURL = info[UIImagePickerControllerReferenceURL] as! NSURL
let imageName = uploadFileURL.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! as String
// getting local path
let localPath = (documentDirectory as NSString).stringByAppendingPathComponent(imageName!)
//getting actual image
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
let data = UIImagePNGRepresentation(image)
data!.writeToFile(localPath, atomically: true)
let imageData = NSData(contentsOfFile: localPath)!
imageURL = NSURL(fileURLWithPath: localPath)
picker.dismissViewControllerAnimated(true, completion: nil)
}
步骤3::呼叫后imageURL
集此uploadImage
方法在此委托方法的imageURL
设定值将图片上传到您的存储区:
func uploadImage(){ //defining bucket and upload file name let S3BucketName: String = "bucketName" let S3UploadKeyName: String = "public/testImage.jpg" let expression = AWSS3TransferUtilityUploadExpression() expression.uploadProgress = {(task: AWSS3TransferUtilityTask, bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) in dispatch_async(dispatch_get_main_queue(), { let progress = Float(totalBytesSent)/Float(totalBytesExpectedToSend) print("Progress is: \(progress)") }) } self.uploadCompletionHandler = { (task, error) -> Void in dispatch_async(dispatch_get_main_queue(), { if ((error) != nil){ print("Failed with error") print("Error: \(error!)"); } else{ print("Sucess") } }) } let transferUtility = AWSS3TransferUtility.defaultS3TransferUtility() transferUtility.uploadFile(imageURL, bucket: S3BucketName, key: S3UploadKeyName, contentType: "image/jpeg", expression: expression, completionHander: uploadCompletionHandler).continueWithBlock { (task) -> AnyObject! in if let error = task.error { print("Error: \(error.localizedDescription)") } if let exception = task.exception { print("Exception: \(exception.description)") } if let _ = task.result { print("Upload Starting!") } return nil; } }
下载:
func downloadImage(){ var completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock? let S3BucketName: String = "bucketName" let S3DownloadKeyName: String = "public/testImage.jpg" let expression = AWSS3TransferUtilityDownloadExpression() expression.downloadProgress = {(task: AWSS3TransferUtilityTask, bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) in dispatch_async(dispatch_get_main_queue(), { let progress = Float(totalBytesSent)/Float(totalBytesExpectedToSend) print("Progress is: \(progress)") }) } completionHandler = { (task, location, data, error) -> Void in dispatch_async(dispatch_get_main_queue(), { if ((error) != nil){ print("Failed with error") print("Error: \(error!)") } else{ //Set your image var downloadedImage = UIImage(data: data!) } }) } let transferUtility = AWSS3TransferUtility.defaultS3TransferUtility() transferUtility.downloadToURL(nil, bucket: S3BucketName, key: S3DownloadKeyName, expression: expression, completionHander: completionHandler).continueWithBlock { (task) -> AnyObject! in if let error = task.error { print("Error: \(error.localizedDescription)") } if let exception = task.exception { print("Exception: \(exception.description)") } if let _ = task.result { print("Download Starting!") } return nil; } }
非常感谢jzorz
0
如果你想要的是下载的图片,这是一个更简洁正确的方法:
func downloadImage(bucketName: String, fileName: String, completion: (image: UIImage?, error: NSError?) -> Void) {
let transferUtility = AWSS3TransferUtility.defaultS3TransferUtility()
transferUtility.downloadDataFromBucket(bucketName, key: fileName, expression: nil) { (task, url, data, error) in
var resultImage: UIImage?
if let data = data {
resultImage = UIImage(data: data)
}
completion(image: resultImage, error: error)
}
}
相关问题
- 1. swift:从照片库中拍摄图片并上传到firebase
- 2. 如何从浏览器下载图片并上传到Amazon S3
- 3. 从图库上传图片到S3 AWS android123
- 4. 从s3下载图片
- 5. Cordova FileTransfer:将图片上传至AWS s3
- 6. 的图片上传到S3
- 7. 手机照片上传至S3
- 8. 改善S3图片上传
- 9. 上传图片点击下载图片
- 10. 从亚马逊S3下载图片
- 11. 从Google照片上传多张照片
- 12. 社区引擎和用于照片上传的AWS s3
- 13. 上传图片和下载
- 14. 如何将图片从aws s3下载到imageview?
- 15. 使用AWSiOSSDK.framework花费太长时间从AWS S3下载图片
- 16. 从数据库c下载照片#
- 17. 如何在AWS s3上传图片,上传时出错
- 18. AngularJs图片上传到S3
- 19. 从其网址下载图片并直接将其上传到AWS
- 20. 上传照片
- 21. 亚马逊s3图片//立即从网址下载图片
- 22. 从照片库中加载图像
- 23. 将图片从EC2上传到S3
- 24. 查看上传图片的亚马逊s3会让我下载图片
- 25. aws ec2 modern.ie图片上传
- 26. Swift 3从本地directory下载图片
- 27. 从照片库
- 28. [iOS]从照片库中选择图片
- 29. phonegap从iOS照片库复制图片
- 30. 应用程序在AWS S3存储桶上传图片时崩溃了swift 3
你是怎么设法让它工作的?我遵循相同的教程,但它不适合我。吨的东西打印在我的控制台,我不知道从哪里开始... – jo3birdtalk
其实,我从github下载了他们的项目,并更改存储桶和钥匙,但未能上传..... – jo3birdtalk
你有没有尝试我的答案,经过测试,我已经上传了吗? –