Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I upload files to an Amazon S3 server with qt or c++ ??
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
c++httpqt 5.4.1
9 Posts 5 Posters 7.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SujaMM
    wrote on last edited by
    #1

    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 ??

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      HI,

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

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SujaMM
        wrote on last edited by
        #3

        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 ??

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          Hi,

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

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SujaMM
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SujaMM
              wrote on last edited by SujaMM
              #6

              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;
              }
              
              I 1 Reply Last reply
              0
              • S SujaMM

                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;
                }
                
                I Offline
                I Offline
                Inathero
                wrote on last edited by
                #7

                @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 >.<

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Abelix
                  wrote on last edited by
                  #8

                  I am also very interested in this topic. Did someone manage to send files?

                  SGaistS 1 Reply Last reply
                  0
                  • A Abelix

                    I am also very interested in this topic. Did someone manage to send files?

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Abelix hi and welcome to devnet,

                    It was a long time ago but yes. You have to follow the specification and it works.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved