Question

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);
}
Was it helpful?

Solution

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top