4

我正面臨S3的問題。 經過3小時的故障排除(在此期間,我瞭解了IAM角色&設法創建它們)我卡住試圖上傳fb檔案圖片到亞馬遜S3。將jpg上傳到S3:「請求主體意外終止」

我的代碼:

if let imageData = NSData(contentsOf: NSURL(string: url) as! URL) { 

          let fileName = ProcessInfo.processInfo.globallyUniqueString + ".jpg" 
          let fileURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName) 
          let image = UIImage(data: imageData as Data) 
          let imageData = UIImageJPEGRepresentation(image!, 1.0)! 
          do { 
           try imageData.write(to: fileURL! as URL) 
          } catch _ { 
           self.log.error("Could not write to file.") 
          } 

          let transferManager = AWSS3TransferManager.default() 
          let uploadRequest = AWSS3TransferManagerUploadRequest() 
          uploadRequest?.bucket = "app-files" 
          uploadRequest?.key = "user-data/" + awsId! + "_primary_profile_picture.jpg" 
          uploadRequest?.body = fileURL! 

          transferManager.upload(uploadRequest!).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask<AnyObject>) -> Any? in 

           if let error = task.error as? NSError { 
            if error.domain == AWSS3TransferManagerErrorDomain, let code = AWSS3TransferManagerErrorType(rawValue: error.code) { 
             switch code { 
             case .cancelled, .paused: 
              break 
             default: 
              print("Error uploading: \(uploadRequest?.key) Error: \(error)") 
             } 
            } else { 
             print("Error uploading: \(uploadRequest?.key) Error: \(error)") 
            } 
            return nil 
           } 

           let uploadOutput = task.result 
           print("Upload complete for: \(uploadRequest?.key)") 
           return nil 
          }) 
         } 

** **問題 我不斷收到來自S3一The request body terminated unexpectedly錯誤,看起來像這樣:

Error uploading: Optional("user-data/eu-west-1:xxxx-xxxx-xxxx-xxxx-xxxxxxxxxx_primary_profile_picture.jpg") 
Error: Error Domain=com.amazonaws.AWSS3ErrorDomain Code=0 "(null)" 
UserInfo={HostId=XXX, 
Message=The request body terminated unexpectedly, 
Code=IncompleteBody, 
RequestId=1485A0FFBD7819D7} 

我不知道是怎麼回事錯誤,我已調試,並fileName,fileURL,imageData似乎很好

+3

SDK v2.5.1中存在一個bug,如果可以的話,嘗試降級到2.5.0。 – donkon

+0

我不得不升級,因爲迅速3支持:/ with 2.5沒有任何工作(認知,發電機,...) –

+0

斯威夫特3支持在2.5.0中引入該變化不應該打破任何列出的服務...理論上。你能否提供一些關於認知和發電機不起作用的細節? – donkon

回答

7

沒有與2.5.1 SDK中的錯誤,我解釋一下here一點。

基本上,AWSSignature創建上傳的簽名錯誤...

你有兩種方法來解決它:

1)還原成使用2.5.0通過聲明所有你需要的豆莢明確像這樣:(編輯:我剛剛注意到你不能這樣做,因爲SWIFT問題的這一嘗試選項2說不定。)

pod 'AWSCore', '2.5.0' 
pod 'AWSCognito', '2.5.0' 
pod 'AWSLambda', '2.5.0' 
pod 'AWSSNS', '2.5.0' 
pod 'AWSS3', '2.5.0' 

2)自行修改代碼來解決這個問題,直到亞馬遜修復它。您需要做的只是在AWSCore/Authentication/AWSSignature.m文件中註釋掉第783-785行 - 如果您嘗試解鎖文件,您應該會看到文件已被鎖定的消息。

if (self.endOfStream) { 
    return NO; 
} 
+0

第2步解決了它。謝謝:) –

+0

對不起,能否解釋一下如何訪問和修改.m文件?它不是一個鎖定的框架? –

+0

@JamesWhite在訪問它時會詢問您是否要解鎖和編輯。選擇「確定」並繼續:) –

0

確實有一個錯誤與當前的AWSS3 SDK。如果你正在使用的CocoaPods可以安裝2.5.0(SWIFT 3)兼容:

pod 'AWSS3', '2.5.0' 
+0

這是行不通的,因爲: - 'Podfile'需要的'AWSCore(= 2.5.1)' - AWSS3所需的AWSCore(= 2.5.0) 'AWSCNS(2.5.1)'所要求的'AWSCore(= 2.5.1)'''''''''需要'AWSCore(= 2.5.1)' - AWSLambda(2.5.1)' 需要AWSCore(= 2.5.1)',其他模塊必須是2.5.1才能工作(請參閱我的最新評論)。 –

+0

我其實遇到了同樣的問題。所有相關AWS pod的Pod更新爲2.5.0。或者,如果您希望移除AWS pod,然後只安裝AWSS3作爲pod'AWSS3','2.5.0'將安裝所有AWS pod作爲2.5.0 –

+0

,請參閱我的其他評論。由於cognito問題,我的其他aws服務將無法在2.5.0中運行。我無法降級 –