Amazon S3 Cognito-从图像选择器上传图像-Swift 3 [英] Amazon S3 Cognito - Upload image from image picker - Swift 3

查看:80
本文介绍了Amazon S3 Cognito-从图像选择器上传图像-Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功将静态图像上传到AWS服务器.当我将其与imagepicker结合使用时,我面临着一个棘手的问题,因为即使我以不同的方式命名它们,同一张图像也要上传到AWS.代码如下:

I have done static image upload to AWS server successfully. When i combine it with imagepicker, I am facing a wierd issue due to which same image is being uploaded to AWS even though I pick and name them differently. The code is as follows:

internal func imagePickerController(_ picker: UIImagePickerController,
 didFinishPickingMediaWithInfo info: [String : Any])
 {
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage
    var imageUrl          = info[UIImagePickerControllerReferenceURL] as? NSURL
    let imageName         = imageUrl?.lastPathComponent
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
    let photoURL          = NSURL(fileURLWithPath: documentDirectory)
    let localPath         = photoURL.appendingPathComponent(imageName!)

    print("image name : \(imageName)")

    if !FileManager.default.fileExists(atPath: localPath!.path) {
        do {
            try UIImageJPEGRepresentation(image, 1.0)?.write(to: localPath!)
            print("file saved")
            //let imageData = NSData(contentsOf: localPath!)
            //let finalURL = localPath!

            //this is in swift 2; above 2 lines are its equivalent in swift3. I think the problem lies here
            //let imageData = NSData(contentsOfFile: localPath)!
            //imageURL = NSURL(fileURLWithPath: localPath)

        }catch {
            print("error saving file")
        }
    }
    else {
        print("file already exists")
    }

    self.dismiss(animated: true, completion: nil)

    let credentialProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "identity pool id")
    let configuration = AWSServiceConfiguration(region: .APSoutheast1, credentialsProvider: credentialProvider)
    AWSServiceManager.default().defaultServiceConfiguration = configuration

    //these are the static values I used that worked perfectly fine with separate images
    //let localFileName = "Alerts_bg"
    //let ext = "png"
    //let remoteName = localFileName + "." + ext
    //let imageURL = Bundle.main.url(forResource: localFileName, withExtension: ext)!

    let transferManager = AWSS3TransferManager.default()

    let uploadRequest = AWSS3TransferManagerUploadRequest()!
    uploadRequest.bucket = "bucket"
    let imageAWSName = "ios_" + NSUUID().uuidString + ".jpg"
    uploadRequest.key = imageAWSName
    uploadRequest.body = localPath! as URL
    uploadRequest.contentType = "image/jpg"

    print("req123 : \(uploadRequest)")

    uploadRequest.uploadProgress = { (bytesSent, totalBytesSent, totalBytesExpectedToSend) -> Void in
        DispatchQueue.main.async(execute: {
            //self.amountUploaded = totalBytesSent // To show the updating data status in label.
            //self.fileSize = totalBytesExpectedToSend
            print("progress : \(totalBytesSent)/\(totalBytesExpectedToSend)")
        })
    }

    transferManager.upload(uploadRequest).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask<AnyObject>) -> Any? in
        if let error = task.error {
            print("Upload failed with error: (\(error.localizedDescription))")
        }
        if task.result != nil {

            let s3URL = URL(string: "https://s3-ap-southeast-1.amazonaws.com/bucket/\(imageAWSName)")!
            print("Uploaded to:\(s3URL)")
        }
        return nil
    })

 dismiss(animated:true, completion: nil) //5
 }

我看过很多博客,例如 this ,但是这些是swift的早期版本,我无法在Swift 3中进行转换,也无法将imagepicker与AWS正确结合. 有人帮忙.

I have seen lot of blogs such as this and this but these are in earlier versions of swift and I am unable to convert it in Swift 3 and combine imagepicker with AWS properly. Someone please help.

推荐答案

我找到了解决问题的方法:

I found a solution to my problem:

let imageAWSName = "ios_" + NSUUID().uuidString + ".jpg"

    let image = info[UIImagePickerControllerOriginalImage] as! UIImage
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
    let photoURL          = NSURL(fileURLWithPath: documentDirectory)
    let localPath         = photoURL.appendingPathComponent(imageAWSName)

    if !FileManager.default.fileExists(atPath: localPath!.path) {
        do {
            try UIImageJPEGRepresentation(image, 1.0)?.write(to: localPath!)
            print("file saved")
        }catch {
            print("error saving file")
        }
    }
    else {
        print("file already exists")
    }

    let credentialProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "your identity pool id")
    let configuration = AWSServiceConfiguration(region: .APSoutheast1, credentialsProvider: credentialProvider)
    AWSServiceManager.default().defaultServiceConfiguration = configuration

    let transferManager = AWSS3TransferManager.default()

    let uploadRequest = AWSS3TransferManagerUploadRequest()!
    let yourBucketName = "your bucket name"
    uploadRequest.bucket = yourBucketName

    uploadRequest.key = imageAWSName
    uploadRequest.body = localPath! as URL
    uploadRequest.contentType = "image/jpg"

    uploadRequest.uploadProgress = { (bytesSent, totalBytesSent, totalBytesExpectedToSend) -> Void in
        DispatchQueue.main.async(execute: {
            //self.amountUploaded = totalBytesSent // To show the updating data status in label.
            //self.fileSize = totalBytesExpectedToSend
            print("progress : \(totalBytesSent)/\(totalBytesExpectedToSend)")
        })
    }

    transferManager.upload(uploadRequest).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask<AnyObject>) -> Any? in
        if let error = task.error {
            print("Upload failed with error: (\(error.localizedDescription))")
        }
        if task.result != nil {

            let s3URL = URL(string: "https://s3-ap-southeast-1.amazonaws.com/\(yourBucketName)/\(imageAWSName)")!
            print("Uploaded to:\(s3URL)")
        }
        return nil
    })

    self.picker.dismiss(animated: true, completion: nil)

确保在localPath中使用的imageAWSName始终与我所做的不同.这是主要的事情,否则即使您从选择器中选择了不同的图像,AWS也会多次保存相同的图像.

Make sure the imageAWSName used in localPath is always different as I have done. That is the main thing otherwise AWS will save same image multiple times even though you pick different images from picker.

希望它对以后的人有帮助!

Hope it helps someone in future!

这篇关于Amazon S3 Cognito-从图像选择器上传图像-Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆