1

I want to get the file size of a file hosted on Amazon S3 in my Qt app.

The code below works great for non-S3 files, but for signed S3 links it prints out QVariant(Invalid)

It seems that Content-Length isn't in the S3 HTTP header ... so how do I get the file size?

void MainWindow::requestFileSize(QString url)
{
    QNetworkRequest req;
    m_netmanager = new QNetworkAccessManager(this);
    QUrl strict_url = QUrl::fromEncoded(url.toStdString().c_str(),QUrl::StrictMode);
    req.setUrl(strict_url);
    m_reply = m_netmanager->head(req);
    connect(m_reply, SIGNAL(metaDataChanged()), this, SLOT(fileSize()));
}

void MainWindow::fileSize()
{
    qDebug() << "Content Length: " << m_reply->header(QNetworkRequest::ContentLengthHeader);
}
stukennedy
  • 1,088
  • 1
  • 9
  • 25
  • For future reference, please post a self-contained code that we can build and run. – László Papp Sep 25 '13 at 07:08
  • @stukennedy can you post the link, so that it can be verified whether content-length header is actually present in the response header or not. – adnan kamili Sep 25 '13 at 10:40
  • it's easy enough to put the code I posted into a new project and build it, (you just need the header definitions and include dependencies) without me cluttering up the post with code that is unrelated to the question. Hope that's ok ... Anyway, see below for the solution, just figured it out. – stukennedy Sep 25 '13 at 10:45

1 Answers1

0

I've figured out how to get around this.

Looks like Amazon S3 won't let you see the whole HTTP header on a head request ... but you can do a GET, retrieve the header and then delete the reply without getting any of the body.

This works:

void MainWindow::requestFileSize(const QString &url)
{
    QNetworkRequest req;
    m_netmanager = new QNetworkAccessManager(this);
    req.setUrl(QUrl(url));
    m_reply = m_netmanager->get(req);
    connect(m_reply, SIGNAL(metaDataChanged()), this, SLOT(fileSize()));
}

void MainWindow::fileSize()
{
    qDebug() << "Content Length: " << m_reply->header(QNetworkRequest::ContentLengthHeader);
    m_reply->deleteLater();
    m_netmanager->deleteLater();
}
stukennedy
  • 1,088
  • 1
  • 9
  • 25