我没有接触 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.")
}
// ...
并更新正文网址。
upload_request.body = fileURL;
关于ios - 将图像上传到 S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33152114/