【问题标题】:AWSS3 Uploading failed with errorAWSS3 上传失败并出现错误
【发布时间】:2017-02-17 06:20:16
【问题描述】:

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

016-10-08 14:37:41.741 Vabo[3091:62598] AWSiOSSDK v2.4.10 [调试] 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:]

会话任务失败并出现错误:Error Domain=NSURLErrorDomain Code=-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 {Error Domain=kCFErrorDomainCFNetwork Code=-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
    }
}

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    您能否确保Constants.bucketName 中的存储桶名称正确?您需要在您的帐户中提供一个有效的存储桶名称,该名称具有从您的认知角色执行 putObject 的权限。

    谢谢, 罗汉

    【讨论】:

      猜你喜欢
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 2012-03-07
      • 2014-10-07
      • 2018-02-24
      • 2012-09-24
      • 2019-04-01
      相关资源
      最近更新 更多