How can I access Amazon AWS S3 using GSOAP for C and C++?

前端 未结 2 1832
再見小時候
再見小時候 2021-02-20 02:38

I have searched everywhere for this and I could not find a single decent code. How can I access Amazon AWS S3 service using GSOAP?

2条回答
  •  不知归路
    2021-02-20 03:02

    The code below is from the OP. Originally, the post contained both the question and the answer and I am turning it into a Q&A format.

    The signature has to be of the format

    base64encode((HMAC-SHA1(ActionName+"AmazonS3"+XMLTimestamp)))
    

    The HMAC, SHA1and B64 utils are available in openssl.

    The format for SOAP requests is given by the wsdl.

    The REST interface is different.

    After wsdl2h to generate the header and soapcpp2 to generate the GSOAP client code the following will be the code to access the service:

    Requirements : OpenSSL, GSOAP.

    Build with the compiler preprocessor directive WITH_OPENSSL. Link with libraries libeay32 and ssleay32.

    #include "AmazonS3SoapBinding.nsmap" //generated from soapcpp2
    #include "soapAmazonS3SoapBindingProxy.h" //generated from soapcpp2
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    /* convert to base64 */
    std::string base64_encodestring(char* text, int len) {
        EVP_ENCODE_CTX ectx;
        int size = len*2;
        size = size > 64 ? size : 64;
        unsigned char* out = (unsigned char*)malloc( size );
        int outlen = 0;
        int tlen = 0;
    
        EVP_EncodeInit(&ectx);
        EVP_EncodeUpdate(&ectx,
                out,
                &outlen,
                (const unsigned char*)text,
                len
                );
        tlen += outlen;
        EVP_EncodeFinal( &ectx, out+tlen, &outlen );
        tlen += outlen;
    
        std::string str((char*)out, tlen );
        free( out );
        return str;
    }
    
    /* return the utc date+time in xml format */
    const char* xml_datetime() {
    
      /*"YYYY-mm-ddTHH:MM:SS.000Z\"*/
      const int MAX=25;
      static char output[MAX+1];
      time_t now = time(NULL);
    
      strftime( output, MAX+1, "%Y-%m-%dT%H:%M:%S.000Z", gmtime( &now ) );
    
      std::cout <::iterator itr;
    
        for(itr=buk_resp.ListAllMyBucketsResponse->Buckets->Bucket.begin();
                itr!=buk_resp.ListAllMyBucketsResponse->Buckets->Bucket.end();
                itr++
                ) {
            std::cout<<(*itr)->Name<

提交回复
热议问题