AWSS3上传失败,错误

时间:2016-10-08 09:50:22

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

我想在亚马逊服务器上上传图片,但每次尝试上传图片时,都会在日志中显示错误

  

016-10-08 14:37:41.741 Vabo [3091:62598] AWSiOSSDK v2.4.10 [Debug]       AWSURLSessionManager.m行:543 | - [AWSURLSessionManagerprintHTTPHeadersAndBodyForRequest:]       请求正文:2016-10-08 14:38:44.171 Vabo [3091:62596] AWSiOSSDK v2.4.10 [错误] AWSURLSessionManager.m行:212 |    - [AWSURLSessionManager URLSession:task:didCompleteWithError:]

     

会话任务失败,错误:错误域= NSURLErrorDomain   代码= -1001“请求超时。”       的UserInfo = {NSErrorFailingURLStringKey = https://s3.amazonaws.com/vabo/production/uploads/avatars/200/avatar.jpg,       _kCFStreamErrorCodeKey = -2102,NSErrorFailingURLKey = https://s3.amazonaws.com/vabo/production/uploads/avatars/200/avatar.jpg,       NSLocalizedDescription =请求超时。,_ kCFStreamErrorDomainKey = 4,NSUnderlyingError = 0x7ca545a0 {错误域= kCFErrorDomainCFNetwork代码= -1001“(null)”       UserInfo = {_ kCFStreamErrorDomainKey = 4,_kCFStreamErrorCodeKey = -2102}}}

以下是我用来上传图片的代码。

func fusumaImageSelected(image: UIImage) {
    self.imageView.image = image


    let fileName = NSProcessInfo.processInfo().globallyUniqueString.stringByAppendingString(".jpg")
    let fileURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("upload").URLByAppendingPathComponent(fileName)
    let filePath = fileURL.path!
    let imageData = UIImageJPEGRepresentation(image, 0.5)
    imageData!.writeToFile(filePath, atomically: true)



    let putObjectRequest = AWSS3PutObjectRequest()
    putObjectRequest.body = imageData
    putObjectRequest.contentLength = NSNumber.init(long: (imageData?.length)!)
    putObjectRequest.expires = NSDate().dateByAddingTimeInterval(3600);
    putObjectRequest.contentType = "image/png"
    putObjectRequest.key = "production/uploads/avatars/200/avatar.jpg"
    putObjectRequest.bucket = Constants.S3BucketName

    self.uploadProgessInLoading(putObjectRequest)
    AWSS3.defaultS3().putObject(putObjectRequest).continueWithBlock{(task) -> AnyObject! in

        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 {

        }
        return nil
    }
}

2 个答案:

答案 0 :(得分:1)

要解决此问题,我必须添加一个桥接标头,并且我使用的身份池使用错误的区域。

答案 1 :(得分:0)

这很可能是由于存储桶名称配置不正确造成的。 错误代码如下所述: https://github.com/aws/aws-sdk-ios/blob/d0154d3f1e1cc20fad8c7339208ee208f1d8c516/AWSS3/AWSS3Model.h

请问False中的存储桶名称是否正确?您需要在帐户中提供有效的存储桶名称,并具有从您的cognito角色执行putObject的权限。

谢谢, 罗汉