2

我正在尝试使用最近发布的新版本 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
    }
4

1 回答 1

3

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

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

于 2014-10-20T10:15:31.007 回答