HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 将图像上传到S3无法完成上传大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在将图像上传到S3,并且无法完成文件传输.这是应用程序的行为方式.

>启动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,@R_323_10586@lBytesSent:Int64,@R_323_10586@lBytesExpectedToSend:Int64) in

        dispatch_sync(dispatch_get_main_queue(),{ () -> Void in
            println("@R_323_10586@l  bytes sent")
            println(@R_323_10586@lBytesSent)

            println("@R_323_10586@l  bytes expected to send")
            println(@R_323_10586@lBytesExpectedToSend)
        })
    }

    // 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)和 @L_502_3@是您理解和设置Cognito Identity的绝佳资源.

大佬总结

以上是大佬教程为你收集整理的ios – 将图像上传到S3无法完成上传全部内容,希望文章能够帮你解决ios – 将图像上传到S3无法完成上传所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。