2017-02-23 80 views
0

出於某種原因,上傳到S3確實無法正常工作,而我卻能夠使下載功能正常工作。XCode Swift AWS S3上傳不起作用

這是我的上載的代碼:

@IBAction FUNC uploadFile(_發件人:的UIButton){

let CognitoRegionType = AWSRegionType.USWest2 // e.g. AWSRegionType.USEast1 
    let CognitoIdentityPoolId = "us-west-2:3c00122a-866c-4ce4-9dd3-ee23c16e58f3" 

    let DefaultServiceRegionType = AWSRegionType.USWest1 // e.g. AWSRegionType.USEast1 
    let S3BucketName = "snappcastphotos" 


    let credentialsProvider = AWSCognitoCredentialsProvider(regionType:CognitoRegionType, identityPoolId: CognitoIdentityPoolId) 
    let configuration = AWSServiceConfiguration(region: DefaultServiceRegionType , credentialsProvider:credentialsProvider) 
    AWSServiceManager.default().defaultServiceConfiguration = configuration 


    let uploadRequest = AWSS3TransferManagerUploadRequest() 
    uploadRequest?.bucket = S3BucketName 
    uploadRequest?.key = "bingo" 
    uploadRequest?.body = URL(fileURLWithPath: "https://stackoverflow.com/users/rhom/desktop/test.rtf") 

    let transferManager = AWSS3TransferManager.default() 
    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 
    }) 
} 

和這個碼產生以下錯誤/輸出:

錯誤上傳:可選(「bingo」)錯誤:錯誤域= com.amazonaws.AWSS3ErrorDomain代碼= 0「(null)」UserInfo = {HostId = KAsbvIqiY67dr/64f3uvZPB1Lr5Vj7eNNV198DLai/RG/tA + v3To8CBbnbFSM00V2COZnzebx/M =,Message =請求正文意外終止,Code = IncompleteBody,RequestId = A95236E53A1B8FE8}

任何人有什麼想法?我找不到任何線索,爲什麼使用Cognito和S3的下載工作,但上傳不起作用....

回答

0

它看起來更多的授權錯誤。檢查你是否擁有這個特定存儲桶的寫入權限,並檢查你所提供的路徑是否正確,就好像存在不匹配一樣。

+0

是的我在Cognito和S3存儲桶檢查權限,以確保他們有讀/寫...將繼續搜索,但thx評論 – user3424472

0

我終於想通這個問題了:)

事實證明,我已經安裝了AWS框架的舊版本,因爲我是遵循老的教程上手...,並加載使用的CocoaPods指定框架在IOS 8.0的podfile中,但我現在是IOS 10.0!所以將podfile更改爲版本10.0可以解決問題。

我結束了使用的CocoaPods和10.0正確podfile重新安裝AWS框架......但我覺得有一種方法只更新吊艙,但我會考慮以後

希望這有助於任何人誰擁有這個問題也:)