2

我正在尝试使用最近发布的AWS新版本,我有旧版本在android上工作,但我对swift和较新版本的AWS S3感到陌生,并且已被阻止这个问题的最后几天。我一直在收到一条错误信息,该操作无法完成。 (可可错误260)Swift/Objective S3文件上传

这里是我的应用程序委托

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 
    } 

回答

3

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

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

+0

冷静,在进步位,似乎给我回“身体”不能是零代码误差5 – user1408682 2014-10-20 10:29:46

+0

文件路径“的/ var /移动/集装箱/数据/应用/ 12AF0267-7DB0- 4311-AC60-27CF3C72032B/Documents/photos/closr.jpg“ – user1408682 2014-10-20 10:30:22

+0

将fileUrl更改为var fileUrl = NSBundle.mainBundle()。URLForResource(imagePath !, withExtension:”jpg“) – user1408682 2014-10-20 10:30:46