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