Results 1 to 2 of 2

Thread: How to Upload a Image with POST to Amason S3 in Qt5/c++ ??

  1. #1
    Join Date
    Jun 2015
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default How to Upload a Image with POST to Amason S3 in Qt5/c++ ??

    Hello, I'm very new at qt, but I need to upload an image to a Amazon S3 server, but I keep getting this error:

    Qt Code:
    1. Failure "Error downloading https://bucket_name.s3.amazonaws.com/ - server replied: Forbidden"
    To copy to clipboard, switch view to plain text mode 

    I check all the credencialas and keys and they are working fine because I was able to upload the image with curl comands. The problem was when I tried to implemt the code on qt5.

    So this it's the code I'm ussing:

    Qt Code:
    1. QEventLoop eventLoop;
    2. QNetworkAccessManager mgr;
    3. QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
    4.  
    5. QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    6.  
    7. QHttpPart textPart;
    8. textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"key\""));
    9. textPart.setBody("c_test/Pantallazo-10.jpg");
    10.  
    11. QHttpPart textPart1;
    12. textPart1.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"AWSAccessKeyId\""));
    13. textPart1.setBody("myAccessKeyId");
    14.  
    15. QHttpPart textPart2;
    16. textPart2.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"Policy\""));
    17. textPart2.setBody("myPolicy");
    18.  
    19. QHttpPart textPart3;
    20. textPart3.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"Signature\""));
    21. textPart3.setBody("mySignature");
    22.  
    23. QHttpPart imagePart;
    24. imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
    25. imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\""));
    26. QFile *file = new QFile(QDir::homePath() + "Pantallazo-10.jpg");
    27. file->open(QIODevice::ReadOnly);
    28. imagePart.setBodyDevice(file);
    29. file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
    30.  
    31. multiPart->append(textPart);
    32. multiPart->append(textPart1);
    33. multiPart->append(textPart2);
    34. multiPart->append(textPart3);
    35. multiPart->append(imagePart);
    36.  
    37. QUrl url = QUrl(QString("https://bucket_name.s3.amazonaws.com/"));
    38.  
    39. QNetworkRequest request(url);
    40. QNetworkReply *reply = mgr.post(request, multiPart); // POST
    41. eventLoop.exec(); // blocks stack until "finished()" has been called
    42.  
    43. if (reply->error() == QNetworkReply::NoError) {
    44. qDebug() << "Success";
    45. } else {
    46. //failure
    47. qDebug() << "Failure" <<reply->errorString();
    48. multiPart->setParent(reply);
    49. delete reply;
    50. }
    To copy to clipboard, switch view to plain text mode 

    I really don't know what to do next, I'm pretty stuck on this one.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,359
    Thanks
    36
    Thanked 1,533 Times in 1,484 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to Upload a Image with POST to Amason S3 in Qt5/c++ ??

    Just as a precaution, check that the file was opened successfully.
    Your code assumes that homePath() ends in a directory separator character, I am not sure this is always the case.

    Also maybe connect to the authenticationRequired() signal and check if the slot is being invoked.

    Cheers,
    _

Similar Threads

  1. How to upload image file in FTP server
    By mania in forum Qt Programming
    Replies: 1
    Last Post: 31st July 2014, 08:03
  2. Replies: 0
    Last Post: 28th February 2011, 09:10
  3. How to upload file to HTTP server (POST Method)
    By Alex Snet in forum Qt Programming
    Replies: 8
    Last Post: 24th January 2011, 22:49
  4. How to upload file with form-post using WebKit?
    By msimurin in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2010, 09:46
  5. QHttp::post() - cannot upload file
    By arunredi in forum Qt Programming
    Replies: 5
    Last Post: 16th May 2008, 12:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.