【问题标题】:Swift/Objective S3 File uploadSwift/Objective S3 文件上传
【发布时间】:2014-12-15 06:18:15
【问题描述】:

我正在尝试使用最近发布的新版本 AWS,我有旧版本在 android 上运行,但我是 swift 和新版本 AWS S3 的新手,并且在过去几天被阻止这个问题。我不断收到无法完成操作的错误消息。 (可可错误 260。)

这是我的应用委托

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let credentialsProvider = AWSCognitoCredentialsProvider.credentialsWithRegionType(
        AWSRegionType.USEast1,
        accountId: cognitoAccountId,
        identityPoolId: cognitoIdentityPoolId,
        unauthRoleArn: cognitoUnauthRoleArn,
        authRoleArn: cognitoAuthRoleArn)
    let defaultServiceConfiguration = AWSServiceConfiguration(
        region: AWSRegionType.USEast1,
        credentialsProvider: credentialsProvider)
    AWSServiceManager.defaultServiceManager().setDefaultServiceConfiguration(defaultServiceConfiguration)
  AWSServiceManager.defaultServiceManager().setDefaultServiceConfiguration(defaultServiceConfiguration)
    return true
}

我尝试将图像上传到存储桶的位置的代码

    let fileUrl = NSURL(string:imagePath!)
    println(imagePath)
    UIImageJPEGRepresentation(self.image, 1).writeToURL(fileUrl, atomically: true)
    var indent:NSString = "closr"
    var uploadRequest:AWSS3TransferManagerUploadRequest =        AWSS3TransferManagerUploadRequest()
    uploadRequest.bucket = "closr-bucket"
    uploadRequest.key = "filename.jpg"
    uploadRequest.contentType = "image/jpeg"
    uploadRequest.body = fileUrl
    uploadRequest.uploadProgress = { (bytesSent:Int64, totalBytesSent:Int64,  totalBytesExpectedToSend:Int64) -> Void in
        dispatch_sync(dispatch_get_main_queue(), {() -> Void in
            println(totalBytesSent)
        })
    }

          AWSS3TransferManager.defaultS3TransferManager().upload(uploadRequest).continueWithBlock { (task) -> AnyObject! in
        if (task.error != nil) {
                //failed
                println("failed")
                println(task.error.code)
                println(task.error.localizedDescription)
        } else {
            //completed
            println("completed")
        }
        return nil
    }

【问题讨论】:

    标签: objective-c amazon-web-services swift amazon-s3 amazon-cognito


    【解决方案1】:

    尝试使用 fileURLWithPath 类方法实例化 NSURL 对象,这是使用 NSURL 访问本地文件的正确方法。

    https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/#//apple_ref/occ/clm/NSURL/fileURLWithPath:isDirectory:

    【讨论】:

    • 酷,有点进步,似乎给我回了'body' can not be nil code error 5
    • 文件路径“/var/mobile/Containers/Data/Application/12AF0267-7DB0-4311-AC60-27CF3C72032B/Documents/photos/closr.jpg”
    • 将 fileUrl 更改为 var fileUrl = NSBundle.mainBundle().URLForResource(imagePath!, withExtension: "jpg")
    • 如果图像与应用程序一起打包,您只能使用 NSBundle.mainBundle().URLForResource。如果图像来自相机或图书馆,你必须相应地处理它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 2021-10-20
    • 2011-08-27
    • 2021-11-28
    • 1970-01-01
    相关资源
    最近更新 更多