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:
Failure "Error downloading https://bucket_name.s3.amazonaws.com/ - server replied: Forbidden"
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:
QNetworkAccessManager mgr;
QObject::connect(&mgr,
SIGNAL(finished
(QNetworkReply
*)),
&eventLoop,
SLOT(quit
()));
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart textPart;
textPart.
setHeader(QNetworkRequest
::ContentDispositionHeader,
QVariant("form-data; name=\"key\""));
textPart.setBody("c_test/Pantallazo-10.jpg");
QHttpPart textPart1;
textPart1.
setHeader(QNetworkRequest
::ContentDispositionHeader,
QVariant("form-data; name=\"AWSAccessKeyId\""));
textPart1.setBody("myAccessKeyId");
QHttpPart textPart2;
textPart2.
setHeader(QNetworkRequest
::ContentDispositionHeader,
QVariant("form-data; name=\"Policy\""));
textPart2.setBody("myPolicy");
QHttpPart textPart3;
textPart3.
setHeader(QNetworkRequest
::ContentDispositionHeader,
QVariant("form-data; name=\"Signature\""));
textPart3.setBody("mySignature");
QHttpPart imagePart;
imagePart.
setHeader(QNetworkRequest
::ContentTypeHeader,
QVariant("image/jpeg"));
imagePart.
setHeader(QNetworkRequest
::ContentDispositionHeader,
QVariant("form-data; name=\"file\""));
imagePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(textPart);
multiPart->append(textPart1);
multiPart->append(textPart2);
multiPart->append(textPart3);
multiPart->append(imagePart);
QNetworkRequest request(url);
QNetworkReply *reply = mgr.post(request, multiPart); // POST
eventLoop.exec(); // blocks stack until "finished()" has been called
if (reply->error() == QNetworkReply::NoError) {
qDebug() << "Success";
} else {
//failure
qDebug() << "Failure" <<reply->errorString();
multiPart->setParent(reply);
delete reply;
}
QEventLoop eventLoop;
QNetworkAccessManager mgr;
QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart textPart;
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"key\""));
textPart.setBody("c_test/Pantallazo-10.jpg");
QHttpPart textPart1;
textPart1.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"AWSAccessKeyId\""));
textPart1.setBody("myAccessKeyId");
QHttpPart textPart2;
textPart2.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"Policy\""));
textPart2.setBody("myPolicy");
QHttpPart textPart3;
textPart3.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"Signature\""));
textPart3.setBody("mySignature");
QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\""));
QFile *file = new QFile(QDir::homePath() + "Pantallazo-10.jpg");
file->open(QIODevice::ReadOnly);
imagePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(textPart);
multiPart->append(textPart1);
multiPart->append(textPart2);
multiPart->append(textPart3);
multiPart->append(imagePart);
QUrl url = QUrl(QString("https://bucket_name.s3.amazonaws.com/"));
QNetworkRequest request(url);
QNetworkReply *reply = mgr.post(request, multiPart); // POST
eventLoop.exec(); // blocks stack until "finished()" has been called
if (reply->error() == QNetworkReply::NoError) {
qDebug() << "Success";
} else {
//failure
qDebug() << "Failure" <<reply->errorString();
multiPart->setParent(reply);
delete reply;
}
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.
Bookmarks