2016-02-13 49 views
0

我試圖在S3桶上發送照片。好吧,這是很快就沒有記錄的地獄,而且sdks在這裏和那裏都是碎片化的,它在我身邊很糟糕,目前沒有任何工作:亞馬遜S3斯威夫特 - 簡單的上傳需要年齡,最後不會出現在桶裏

我的上傳過程持續進行,但速度非常慢(讓我們說一個5 meg圖像需要10分鐘),大多數時候,上傳會凍結並重新開始。

但奇怪的是,它終於成功時,文件不會顯示在我的桶中。我曾試着將文件上傳到不存在的水桶和過程仍然繼續(???)

這裏是我的憑據LOC(桶是華東加州)

 let credentialsProvider = AWSCognitoCredentialsProvider(
     regionType: AWSRegionType.USEast1, identityPoolId: "us-east-1:47e88493-xxxx-xxxx-xxxx-xxxxxxxxx") 

    let defaultServiceConfiguration = AWSServiceConfiguration(
     region: AWSRegionType.USEast1, credentialsProvider: credentialsProvider) 

    AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = defaultServiceConfiguration 

現在這裏是我的上傳功能

func uploadImage(){ 

     //defining bucket and upload file name 
     let S3BucketName: String = "witnesstestbucket" 
     let S3UploadKeyName: String = "public/testImage.jpg" 


     let expression = AWSS3TransferUtilityUploadExpression() 
     expression.uploadProgress = {(task: AWSS3TransferUtilityTask, bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) in 
      dispatch_async(dispatch_get_main_queue(), { 
       let progress = Float(totalBytesSent)/Float(totalBytesExpectedToSend) 
       print("Progress is: \(progress)") 
       print (Float(totalBytesExpectedToSend)) 

      }) 
     } 

     self.uploadCompletionHandler = { (task, error) -> Void in 
      dispatch_async(dispatch_get_main_queue(), { 
       if ((error) != nil){ 
        print("Failed with error") 
        print("Error: \(error!)"); 
       } 
       else{ 
        print("Sucess") 
       } 
      }) 
     } 

     let transferUtility = AWSS3TransferUtility.defaultS3TransferUtility() 

     transferUtility.uploadData(imagedata, bucket: S3BucketName, key: S3UploadKeyName, contentType: "image/jpeg", expression: expression, completionHander: uploadCompletionHandler).continueWithBlock { (task) -> AnyObject! in 
      if let error = task.error { 
       print("Error: \(error.localizedDescription)") 
      } 
      if let exception = task.exception { 
       print("Exception: \(exception.description)") 
      } 
      if let _ = task.result { 
       print("Upload Starting!") 
      } 

      return nil; 
     } 
} 






@IBAction func post(sender: UIButton) { 
    // AW S3 upload 


    uploadImage() 

} 

要清楚,我的圖象 - NSData的來自一個UIImage,從collectionviewcell牽強:

self.imagedata = UIImageJPEGRepresentation(img!, 05.0)! 

有什麼我可以更新,以瞭解我的錯在哪裏? 在此先感謝:)

回答

1

OK,試圖用uploaddata要求上傳NSData的文件,這裏是工作的代碼與正確的URL swift2轉換:

func uploadImage(){ 

     let img:UIImage = fullimage!.image! 

     // create a local image that we can use to upload to s3 
     let path:NSString = NSTemporaryDirectory().stringByAppendingString("image2.jpg") 
     let imageD:NSData = UIImageJPEGRepresentation(img, 0.2)! 
     imageD.writeToFile(path as String, atomically: true) 

     // once the image is saved we can use the path to create a local fileurl 
     let url:NSURL = NSURL(fileURLWithPath: path as String) 

     // next we set up the S3 upload request manager 
     uploadRequest = AWSS3TransferManagerUploadRequest() 
     // set the bucket 
     uploadRequest?.bucket = "witnesstest" 
     // 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/image2.jpeg" 
     // set the content type 
     uploadRequest?.contentType = "image/jpeg" 
     // 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 
       self.amountUploaded = totalBytesSent 
       self.filesize = totalBytesExpectedToSend; 
       print(self.filesize) 
       print(self.amountUploaded) 
      }) 
     } 

     // now the upload request is set up we can creat the transfermanger, the credentials are already set up in the app delegate 
     let transferManager:AWSS3TransferManager = AWSS3TransferManager.defaultS3TransferManager() 
     // start the upload 

     transferManager.upload(uploadRequest).continueWithBlock { (task) -> AnyObject? in 


      // once the uploadmanager finishes check if there were any errors 
      if(task.error != nil){ 
       print("%@", task.error); 
      }else{ // if there aren't any then the image is uploaded! 
       // this is the url of the image we just uploaded 
       print("https://s3.amazonaws.com/witnesstest/foldername/image2.jpeg"); 
      } 

      return "all done"; 
      } 

} 

我要感謝巴雷特Breshears爲幫助,他的GithubSource是從2014年開始的,但我可以設法很容易地將其轉換,因爲這段代碼清晰並且有很好的評論