提问者:小点点

AWSS3上传失败,出错


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

016-10-08 14:37:41.741 Vabo[3091:62598]AWSiOSSDK v 2 . 4 . 10[调试]AWSURLSessionManager . m line:543 |--[awsurlsessionmanagerprinttpheadersandbodyforrequest:]请求正文:2016-10-08 14:38:44.171 Vabo[3091:62596]awsiossd

会话任务失败,出现错误:错误域=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 =请求超时。,_ kcfsstreamerrordomainkey = 4,NSUnderlyingError = 0x7ca 545 a 0 { Error Domain = kCFErrorDomainCFNetwork Code =-1001 "(null)" UserInfo = { _ kcfsstreamerrordomainkey = 4,_ kcfsstreamerrorcodekey =-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个答案

匿名用户

为了解决这个问题,我必须添加一个桥接标头,我的身份池使用了错误的区域。

匿名用户

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

请确保Constants.bucketName中的bucket名称正确吗?您需要在帐户中提供一个有效的bucket名称,该名称具有从cognito角色执行putObject的权限。

谢谢,罗汉