2012-02-06 83 views
0

我試圖把文件在S3使用presigned簽名我的Java Web服務器提供 http://docs.amazonwebservices.com/AmazonS3/latest/dev/PresignedUrlUploadObjectDotNetSDK.html其中使用HTTP PUT

我需要上傳客戶端(目前我的窗戶上傳到S3,當PEM文件應該我提供7使用C++)與亞馬遜服務器握手,我不知道該怎麼做。

當我試圖用「默認上下文」(天真地)發送請求時,它打印出「證書鏈中的自簽名證書」錯誤,並要求我接受或不接受證書。 然後我試圖找出如何添加證書,發現這個代碼: POCO C++ - NET SSL - how to POST HTTPS request

的問題是,我不知道該PEM文件這裏需要。 我想提供我從在亞馬遜網絡服務控制檯X.509下載的PEM文件,但它提出了一個SSL例外:SSL3_GET_SERVER_CERTIFICATE

我的代碼:

URI uri("https://BUCKET.s3.amazonaws.com/nosigfile?Expires=1959682330&AWSAccessKeyId=ACCESSKEY&Signature=DgOifWPmQi%2BASAIDaIOGXla10%2Fw%3D"); 
const Poco::Net::Context::Ptr context(new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, "", "", "cert(x509).pem")); 
Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort(), context); 
HTTPRequest req(HTTPRequest::HTTP_PUT, uri.getPathAndQuery(), HTTPMessage::HTTP_1_1); 
req.setContentLength(contentLength); 
session.sendRequest(req) << streamToSend; 

感謝

回答

0

波科包括證書在項目中。

您將需要any.pem,rootcert.pem,yourappname.xml,您可以在poco測試套件中找到SSL端。

./poco-1.4.1p1-all/NetSSL_OpenSSL/testsuite/{any.pem,rootcert.pem,testsuite.xml} 

一旦你有兩個PEM文件,你的XML,這是在initializeSSL階段使用,您將無法獲得自簽名證書的警告。

class MySSLApp: public Poco::Util::Application 
{ 
public: 
    MySSLApp() 
    { 
     Poco::Net::initializeSSL(); 
     Poco::Net::HTTPStreamFactory::registerFactory(); 
     Poco::Net::HTTPSStreamFactory::registerFactory(); 
    } 

    ~MySSLApp() 
    { 
     Poco::Net::uninitializeSSL(); 
    } 
protected: 
    void initialize(Poco::Util::Application& self) 
    { 
     loadConfiguration(); // load default configuration files, if present 
     Poco::Util::Application::initialize(self); 
    } 

    void myUpload(...) { 
     ... 
     FilePartSource* pFPS = new FilePartSource(szFilename); 
     std::string szHost = "BUCKET.s3.amazonaws.com"; 
     std::string szPath = "/"; 
     int nRespCode = 201; 
     try{ 
      HTTPClientSession s(szHost); 
      HTTPRequest request(HTTPRequest::HTTP_POST, szPath, HTTPMessage::HTTP_1_1); 
      HTMLForm pocoForm(HTMLForm::ENCODING_MULTIPART); 
      pocoForm.set("AWSAccessKeyId",  ACCESSKEY); 
      pocoForm.set("acl",     "public-read"); 
      pocoForm.set("success_action_status", toString(nRespCode)); 
      pocoForm.set("Content-Type",   m_szContentType); 
      pocoForm.set("key",     m_szPath + "/" + m_szDestFileName); 
      pocoForm.set("policy",    m_szPolicy); 
      pocoForm.set("signature",    m_szSignature); 
      pocoForm.addPart("file",    pFPS); 

      pocoForm.prepareSubmit(request); 

      std::ostringstream oszMessage; 
      pocoForm.write(oszMessage); 
      std::string szMessage = oszMessage.str(); 

      //AWS requires a ContentLength set EVEN though it is chunked! 
      request.setContentLength((int) szMessage.length()); 

      s.sendRequest(request) << szMessage; 
      //or: 
      //pocoForm.write(s.sendRequest(request)); 

      HTTPResponse response; 
      std::istream& rs = s.receiveResponse(response); 
      int code = response.getStatus(); 
      if (code != nRespCode) { 
       stringstream s; 
       s << "HTTP Error " << code; 
       throw Poco::IOException(s.str()); 
      } 
     } catch (Exception& exc) { 
      std::cout << exc.displayText() << endl; 
      return; 
     } 
     return; 
    } 
} 

的XML文件將是這個樣子:

<AppConfig> 
<openSSL> 
    <server> 
     <privateKeyFile>${application.configDir}any.pem</privateKeyFile> 
     <caConfig>${application.configDir}rootcert.pem</caConfig> 
     <verificationMode>none</verificationMode> 
     <verificationDepth>9</verificationDepth> 
     <loadDefaultCAFile>true</loadDefaultCAFile> 
     <cypherList>ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH</cypherList> 
     <privateKeyPassphraseHandler> 
      <name>KeyFileHandler</name> 
      <options> 
       <password>secret</password> 
      </options> 
     </privateKeyPassphraseHandler> 
     <invalidCertificateHandler> 
      <name>AcceptCertificateHandler</name> 
      <options> 
      </options> 
     </invalidCertificateHandler> 
    </server> 
    <client> 
     <privateKeyFile>${application.configDir}any.pem</privateKeyFile> 
     <caConfig>${application.configDir}rootcert.pem</caConfig> 
     <verificationMode>relaxed</verificationMode> 
     <verificationDepth>9</verificationDepth> 
     <loadDefaultCAFile>true</loadDefaultCAFile> 
     <cypherList>ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH</cypherList> 
     <privateKeyPassphraseHandler> 
      <name>KeyFileHandler</name> 
      <options> 
       <password>secret</password> 
      </options> 
     </privateKeyPassphraseHandler> 
     <invalidCertificateHandler> 
      <name>AcceptCertificateHandler</name> 
      <options> 
      </options> 
     </invalidCertificateHandler> 
    </client> 
</openSSL> 
</AppConfig>