在aws s3存储桶上传图像时出错

时间:2016-11-12 08:39:15

标签: file-upload amazon-s3 swift2

我在aws s3存储桶上传图片时遇到此错误:

  

2016-11-12 14:03:43.095 Let's Habit [5144:69128] *由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'* - [NSConcreteData initWithBytes:length:copy :deallocator:]:荒谬长度:18446744073709551615,最大大小:9223372036854775808字节'

我正在使用以下代码来处理请求:

 let  fileURL :NSURL =  NSURL(string:"\(documentsUrl1)MyappDirectory")!
    let filePath = fileURL.path!
    let newimg :UIImage = (captureImage?.resizeWithPercentage(0.1))!
     let imageData = UIImagePNGRepresentation(newimg)
               print(imageData!.length)

    imageData!.writeToFile(filePath as String, atomically: true)

    let uploadRequest = AWSS3TransferManagerUploadRequest()
    uploadRequest.body = fileURL
    uploadRequest.key = fileName
    uploadRequest.bucket = S3BucketName

    self.upload(uploadRequest)


 func upload(uploadRequest: AWSS3TransferManagerUploadRequest) {
let transferManager = AWSS3TransferManager.defaultS3TransferManager()
    transferManager.upload(uploadRequest).continueWithBlock { (task) -> AnyObject! in
         print(task)
        if let error = task.error {
            if error.domain == AWSS3TransferManagerErrorDomain as String {
                if let errorCode = AWSS3TransferManagerErrorType(rawValue: error.code) {
                    switch (errorCode) {
                    case .Cancelled, .Paused:
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in

                        })
                        break;

                    default:
                        print("upload() failed: [\(error)]")
                        break;
                    }
                } else {
                    print("upload() failed: [\(error)]")
                }
            } else {
                print("upload() failed: [\(error)]")
            }
        }

        if let exception = task.exception {
            print("upload() failed: [\(exception)]")
        }

        if task.result != nil {
            print(task.result)

            let url = task.result
            print(url)


            dispatch_async(dispatch_get_main_queue(), { () -> Void in


      print("https://s3-us-west-2.amazonaws.com/\(uploadRequest.bucket!)/\(uploadRequest.key!)")

            })
        }
        return nil
    }
}

如何解决此错误?

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。我的解决方案是将文件名附加到指向目录

的文件URL
uploadRequest.body = fileURL+fileName