>启动uploadToS3()
>文件开始传输发送字节到服务器.
>当发送大约600,000个字节时,上载停止.
> 20-40秒后,应用程序继续上传进度为0%.它就好像文件传输从未开始.
>在整个时间内,记录器中不会出现任何错误.
在我的视图控制器中,我有以下方法上传文件.
func uploadToS3(){ // get the image from a UIImageVIEw that is displaying the selected Image var img: UIImage = imageVIEw.image! // create a local image that we can use to upload to s3 var path: Nsstring = NstemporaryDirectory().stringByAppendingPathComponent("image.png") var imageData: NSData = UIImagePNGRepresentation(img) imageData.writetofile(path as String,atomically: true) // once the image is saved we can use the path to create a local fileurl var url:NSURL = NSURL(fileURLWithPath: path as String)! // next we set up the S3 upload request manager let uploadRequest = awss3transfermanagerUploadRequest() // set the bucket uploadRequest?.bucket = "test-bucket" // I want this image to be public to anyone to vIEw it so I'm setting it to Public Read uploadRequest?.ACL = AWSS3ObjectCannedACL.PublicRead // set the image's name that will be used on the s3 server. I am also creating a folder to place the image in uploadRequest?.key = "foldername/image.png" // set the content type uploadRequest?.ContentType = "image/png" // and finally set the body to the local file path uploadRequest?.body = url; // we will track progress through an AWSNetworkingUploadProgressBlock uploadRequest?.uploadProgress = {[uNowned self](bytesSent:Int64,totalBytesSent:Int64,totalBytesExpectedToSend:Int64) in dispatch_sync(dispatch_get_main_queue(),{ () -> VoID in println("total bytes sent") println(totalBytesSent) println("total bytes expected to send") println(totalBytesExpectedToSend) }) } // Now the upload request is set up we can creat the transfermanger,the credentials are already set up in the app delegate var transferManager:awss3transfermanager = awss3transfermanager.defaultS3TransferManager() // start the upload transferManager.upload(uploadRequest).continueWithExecutor(BFExecutor.mainThreadExecutor(),withBlock:{ [uNowned self] task -> AnyObject in // once the uploadmanager finishes check if there were any errors if(task.error != nil){ println("%@",task.error); }else{ // if there aren't any then the image is uploaded! // this is the url of the image we just uploaded println("https://s3.amazonaws.com/s3-demo-swift/foldername/image.png"); } //self.removeLoadingVIEw() println("all done"); return "" })}
对于任何想要重新创建此应用程序的人
添加到您的Podfile:
pod 'AWscore'pod 'AWSS3'pod 'AWSiOSSDKv2'pod 'AWSCognitoSync'
然后添加包含以下内容的桥接头
#import <AWscore/AWscore.h>#import <AWSS3/AWSS3.h>
在我的AppDelegate中,我有:
func application(application: UIApplication,dIDFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // OverrIDe point for customization after application launch. AWSCognitoCredentialsProvIDer.initialize() var credentialsProvIDer = AWSCognitoCredentialsProvIDer( regionType: AWSRegionType.USEast1,IDentityPoolID: "IDentity pool ID" ) var configuration = AWSServiceConfiguration( region: AWSRegionType.USEast1,credentialsProvIDer: credentialsProvIDer ) AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration return true}
最后,在包含uploadToS3()的视图控制器中,添加import AWSS3.
更新
这是记录错误的最后一部分.
}]2015-05-09 19:24:24.540 CoolApp[4492:55681] AWSiOSSDKv2 [Verbose] AWSURLResponseSerialization.m line:278 | -[AWSXMLResponseSerializer responSEObjectForResponse:originalRequest:currentRequest:data:error:] | Response body: [<?xml version="1.0" enCoding="UTF-8"?><Error><Code>AccessDenIEd</Code><Message>Access DenIEd</Message><RequestID>A03D405FC272808A</RequestID><HostID>bhSw+xQkGrMVd9QWMKMG1qYezPJet8b5L2ZIoGXePoftuupMP3HdgbAgcpstiLefo5yA3m1OJvY=</HostID></Error>](%@,Error Domain=com.amazonaws.AWSS3ErrorDomain Code=1 "The operation Couldn’t be completed. (com.amazonaws.AWSS3ErrorDomain error 1.)" UserInfo=0x7c17cdc0 {HostID=bhSw+xQkGrMVd9QWMKMG1qYezPJet8b5L2ZIoGXePoftuupMP3HdgbAgcpstiLefo5yA3m1OJvY=,Code=AccessDenIEd,Message=Access DenIEd,RequestID=A03D405FC272808A})all done
我的问题是,如何解决这个问题并成功上传图片.
解决方法 如错误消息所述,您的Amazon Cognito IDentity池未正确设置权限. Understanding Amazon Cognito Authentication博客系列( Part 2,Part 3)和 Amazon Cognito Developer Guide是您理解和设置Cognito IDentity的绝佳资源. 总结以上是内存溢出为你收集整理的ios – 将图像上传到S3无法完成上传全部内容,希望文章能够帮你解决ios – 将图像上传到S3无法完成上传所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)