【问题标题】:Uploading Image to S3 fails to complete upload上传图片到 S3 无法完成上传
【发布时间】:2015-07-14 15:28:16
【问题描述】:

我正在将图像上传到 S3,但无法完成文件传输。以下是应用的行为方式。

  1. 发起uploadToS3()
  2. 文件开始传输发送字节到服务器。
  3. 当发送大约 600,000 个字节时,上传停止。
  4. 20-40 秒后,应用程序继续以 0% 的速度上传。就好像文件传输从一开始就没有开始一样。
  5. 在此期间,记录器中没有出现任何错误。

在我的视图控制器中,我有以下上传文件的方法。

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

我的问题是,我该如何解决这个问题并成功上传图片。

【问题讨论】:

  • 您是否看到与S3TransferManager-Sample 相同的问题?您确定您的 Amazon S3 存储桶位于美国标准区域吗?
  • 是的,我在示例项目中遇到了同样的问题。我的存储桶位于美国标准区域。
  • 您是否启用了verbose logging?日志告诉你什么?
  • 日志没有告诉我任何异常情况。在上传暂停期间没有弹出错误。我应该寻找什么具体的东西吗?
  • 你能分享日志吗?它可能有助于实际查看它们。我无法使用示例应用程序重现该问题。我能够毫无问题地上传/下载图像文件。

标签: ios swift amazon-web-services amazon-s3 image-uploading


【解决方案1】:

如错误消息所述,您的 Amazon Cognito 身份池未正确设置权限。 Understanding Amazon Cognito Authentication 博客系列(Part 2Part 3)和 Amazon Cognito Developer Guide 是您了解和设置 Cognito Identity 的重要资源。

【讨论】:

  • 宾果游戏。我意识到这是一个 IAM 权限问题。
  • @jason328 您能否分享帮助您解决问题的确切解决方案。您更改了哪些权限?
猜你喜欢
  • 1970-01-01
  • 2019-05-15
  • 1970-01-01
  • 2014-07-27
  • 2018-09-12
  • 2015-08-24
  • 2016-10-07
  • 1970-01-01
  • 2018-12-16
相关资源
最近更新 更多