将图像上传到 S3

Uploading an image to S3

我没有接触 AWS 库,但是每当我尝试将图像上传到 S3 时都会收到此错误。

我有 Xcode 7 和最新版本的 AWS SDK 2.2.6。

我使用此代码上传图片:

        // Upload image

        let path: String = NSTemporaryDirectory() 
        let path_url: NSURL = (NSURL(string: path)?.URLByAppendingPathComponent("prof.png"))!
        let imageData: NSData = UIImageJPEGRepresentation(image, 0.4)!
        imageData.writeToFile(path_url.URLString, atomically: true)

        let url: NSURL = NSURL(fileURLWithPath: path as String)

        let upload_request = AWSS3TransferManagerUploadRequest()

        upload_request.bucket = s3_bucket_name
        upload_request.ACL = AWSS3ObjectCannedACL.AuthenticatedRead
        upload_request.key = "/prof.png"
        upload_request.body = url;

        upload_request.uploadProgress = { (bytesSent, totalBytesSent, totalBytesExpectedToSend) -> Void in
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                if totalBytesExpectedToSend > 0 {
                    print("\(totalBytesSent)/\(totalBytesExpectedToSend)total  bytes sent")
                }
            })
        }

        //

它以前工作过,但我不确定当我通过 cocoapods 或 xcode 7.

更新 AWS 库时是否发生了这种情况

我删除了 /Pods、Podfile.lock 和 DerivedData 文件夹,我还清理了项目并重新安装了 AWS 2.2.6 (pod install)

我用另一种方式保存临时文件。

    // temporary

    let fileName = NSProcessInfo.processInfo().globallyUniqueString.stringByAppendingString(".png")
    let fileURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent(fileName)
    let imageData = UIImageJPEGRepresentation(image, 0.4)!

    do {
        try imageData.writeToURL(fileURL, options: .DataWritingAtomic)
    } catch _ {
        print("didn't write into it.")
    }

    // ...

并更新正文url。

    upload_request.body = fileURL;