Swift上传到s3存储桶不会结束

时间:2016-11-10 20:16:58

标签: ios swift amazon-web-services amazon-s3

我正在尝试将图片上传到存储桶。建立连接,上传显然开始但没有进展。我认为服务器上的权限是正确的,因为Android应用程序能够上传。 在我的appdelegate中我有这个:

let credentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.USEast1, identityPoolId: "us-east-1:XXXXXX-XXXX-XXXX-XXXX-XXXX”, unauthRoleArn: "arn:aws:iam::XXXXX:role/Cognito_mybucketUnauth_Role", authRoleArn: "arn:aws:iam::XXXXX:role/Cognito_mybucketAuth_Role", identityProviderManager: nil)

let configuration = AWSServiceConfiguration(region: AWSRegionType.USEast1, credentialsProvider: credentialsProvider)
        AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration

这是获取图片并上传

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)

    CampoImagem.image = image

    picker.dismissViewControllerAnimated(true, completion: nil)
    uploadImage()
}


func uploadImage(){

    //defining bucket and upload file name
    let S3BucketName: String = “mybucket"
    let S3UploadKeyName: String = "profile/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;
    }
}
  

打印:上传开始!

我怀疑这是ID和使用aws完成上传的权限,但我认为如果上传无法启动,对吗? 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)