ios - AWSS3 上传失败并出现错误

标签 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 line:543 | -[AWSURLSessionManagerprintHTTPHeadersAndBodyForRequest:] Request body: 2016-10-08 14:38:44.171 Vabo[3091:62596] AWSiOSSDK v2.4.10 [Error] AWSURLSessionManager.m line:212 | -[AWSURLSessionManager URLSession:task:didCompleteWithError:]

Session task failed with error: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." 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=The request timed out., _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
    }
}

最佳答案

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

关于ios - AWSS3 上传失败并出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39930982/

相关文章:

ios - 将元素附加到 Firebase 数组

amazon-web-services - 在AWS EKS中,如何安装和访问etcd,kube-apiserver等?

amazon-web-services - 使用一个 cloudFormation 模板从 zip 文件创建一个 lambda 并创建一个 S3 存储桶

javascript - Amazon S3 POST api,并使用 NodeJS 签署策略

ios - (Swift, EarlGrey) 将 slider 移动到中间

ios - Kingfisher nil 在展开可选值时

ios - 尝试在Xcode Objective-C中创建按钮

iphone - 我的应用程序仅在美国商店有售,为什么?

swift - 在 SceneKit 中按所需方向旋转后如何平移相机节点位置?

swift - 我怎样才能获得所需的数据?