2015-08-20 123 views
0

我使用Phonegap捕獲視頻文件並將其上傳到Amazon S3和Amazon S3 SDK以獲取請求的預簽名url。在本地使用PHP服務器時,捕獲和上傳工作正常,從亞馬遜獲取預先簽名的URL也沒有問題。但是,當我結合所有這些東西:嘗試使用生成的預簽名url將捕獲的視頻上傳到亞馬遜時,我得到「我們計算的請求籤名與您提供的簽名不匹配。」Phonegap Amazon S3 SDK:簽名不匹配

這裏是我的方法獲取預籤的網址並上傳到亞馬遜:

uploadFile: function (file) { 
    var //path = file.fullPath, 
     path = file.localURL, 
     name = file.name, 
     type = file.type; 

    var s3 = new AWS.S3(), 
     s3_params = { 
     Bucket: 'BUCKET_NAME', 
     Key: name, 
     Expires: 6000 
     //ContentType: type, 
     //ACL: 'public-read' 
    }; 

    s3.getSignedUrl('putObject', s3_params, function(err, data){ 
     if(err){ 
      console.log(err); 
     } 
     else{ 
      var ft = new FileTransfer(), 
       url = encodeURI(data); 
      console.log(data); 
      ft.onprogress = function(progressEvent) { 
       if (progressEvent.lengthComputable) { 
        console.log(progressEvent.loaded/progressEvent.total); 
       } else { 
        console.log(progressEvent); 
       } 
      }; 

      ft.upload(
       path, 
       url, 
       function(result) { 
        console.log('Upload success: ' + result.responseCode); 
        console.log(result.bytesSent + ' bytes sent'); 
       }, 
       function(error) { 
        console.log('Error uploading file ' + path + ': ' + error.code); 
       }, 
       { 
        httpMethod : 'PUT', 
        fileName : name, 
        mimeType : type, 
        chunkedMode : false, 
        headers: { 
         'Content-Type' : type, 
         'x-amz-acl' : 'public-read' 
        } 
       } 
      ); 
     } 
    }); 
} 

難道我做錯了什麼?

我知道有很多問題/ SO上與此相關的問題,但沒有答案似乎爲我工作...

請參閱完整的測試程序在這裏: https://github.com/terreb/Phonegap-Media-Capture-S3-Upload

請指教!

+0

我希望你是不是在你的應用程序中嵌入您的AWS憑據。這並不安全,但並不真正相關。爲什麼將ContentType和ACL註釋掉?您不能在請求中包含然後從簽名過程中排除它們。此外,你不會從亞馬遜「獲得」一個簽名的URL。「這是由您的代碼在本地生成的,並且如果參數與您發送的請求不完全一致,那麼它將與您的代碼一樣不正確。 –

+0

真正的應用程序從PHP服務器獲取預先簽名的URL。這個例子只是爲了隔離這個問題,因爲在這兩種情況下我都會得到相同的錯誤:從服務器接收到預先簽名的url或者在應用程序中生成它。 ContentType和ACL被註釋掉,以表明我知道它們並已經嘗試過。我知道這個問題很可能是一個缺失或錯誤的參數,這就是我想知道的。 – terreb

回答

0

不太確定,但它可能會解決問題。

根據their documentation你可能需要在你的s3_params添加 'ServerSideEncryption'是這樣

s3_params = { 
    Bucket: 'BUCKET_NAME', 
    Key: name, 
    Expires: 6000 
    ServerSideEncryption: 'AES256' 
}; 

希望這有助於

+0

不好,不幸的是它沒有幫助。 – terreb

0

好吧,看來我發現了問題:getSignedUrl返回ALREADY編碼的網址。

修正方法:

uploadFile: function (file) { 
var path = file.localURL, 
    name = file.name, 
    type = file.type; 

var s3 = new AWS.S3(), 
    s3_params = { 
    Bucket: 'BUCKET_NAME', 
    Key: name, 
    Expires: 6000, 
    ContentType: type, 
    ACL: 'public-read' 
}; 

s3.getSignedUrl('putObject', s3_params, function(err, data){ 
    if(err){ 
     console.log(err); 
    } 
    else{ 
     var ft = new FileTransfer(), 
      url = data; 

     ft.onprogress = function(progressEvent) { 
      if (progressEvent.lengthComputable) { 
       console.log(progressEvent.loaded/progressEvent.total); 
      } else { 
       console.log(progressEvent); 
      } 
     }; 

     ft.upload(
      path, 
      url, 
      function(result) { 
       console.log('Upload success: ' + result.responseCode); 
       console.log(result.bytesSent + ' bytes sent'); 
      }, 
      function(error) { 
       console.log('Error uploading file ' + path + ': ' + error.code); 
      }, 
      { 
       httpMethod : 'PUT', 
       fileName : name, 
       mimeType : type, 
       chunkedMode : false, 
       headers: { 
        'Content-Type' : type, 
        'x-amz-acl' : 'public-read' 
       } 
      } 
     ); 
    } 
}); 

}

+0

嗯,現在我有另一個問題:文件上傳在iOS和Android上都可以正常使用上面的代碼,但從Amazon S3下載時文件無法讀取。他們確實顯示出合適的尺寸...... – terreb