How can I upload files to an Amazon S3 server with qt or c++ ??



  • Hello,

    I need to upload a picture to a amazon s3 in my application. I'm ussing qt 5.4.1 and c++, is there a way to do this with either qt or c++ or both.

    If it is which one it's the best option and how can I do it ??



  • HI,

    according to this S3 provide a REST API.
    You can use it with standard QNetworkAccessManager::post() requests



  • If I understand correctly I can upload a file to S3 with POST, but what parameters do I need to send, how do I send the credentials, key_id and secret_key ??



  • Hi,

    you have to study the API.
    To set header parameters you can use QNetworkRequest::setHeader() or QNetworkRequest::setRawHeader()



  • I am, but this it's all very new to me, I have to create a policy and a signature and after encode those with base64. But I'm having trouble with this part.

    Thanks for the help



  • I created everything I need and are working, now I'm trying to bring it to qt. But I keep getting this error:

     Failure "Error downloading myurl - server replied: Precondition Failed"
    

    this it's the code I'm ussing, I really don'r know where the problem it's:

    // create custom temporary event loop on stack
       QEventLoop eventLoop;
    
    // "quit()" the event-loop, when the network request "finished()"
    QNetworkAccessManager mgr;
    QNetworkCookieJar cookieJar;
    //mgr.setCookieJar(new QNetworkCookieJar(cookieJar));
    QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
    mgr.setCookieJar(&cookieJar);
    
    QUrl url = QUrl(QString("myurl"));
    QUrlQuery params;
    params.addQueryItem("key", "c_test/Pantallazo-2.png");
    params.addQueryItem("AWSAccessKeyId", "myAccessKeyId");
    params.addQueryItem("Policy", "mypolicy");
    params.addQueryItem("Signature", "mysignature");
    params.addQueryItem("Content-Type", "image/png");
    params.addQueryItem("file", "Pantallazo-2.png");
    
    QByteArray data;
    data = params.query(QUrl::FullyEncoded).toUtf8();
    
    QNetworkRequest request;
    request.setUrl(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    
    QNetworkReply *reply = mgr.post(request, data);  // POST
    eventLoop.exec(); // blocks stack until "finished()" has been called
    
    if (reply->error() == QNetworkReply::NoError) {
        qDebug() << "Success";
    } else {
        //failure
        qDebug() << "Failure" <<reply->errorString();
        delete reply;
    }


  • @SujaMM

    I'm trying to do exactly what you're attempting to do. But in my case, all my code works except for how I create my signature (the response from aws server says that their signature is different than mine, therefore my request won't go through). If its possible, could you show me how you're creating your signature? If I can create my signature correctly and it goes through, I can post the source code for you to pick apart and choose what you want to use.

    I just keep failing on this signature creation business >.<


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.