-1

任何人都可以帮助我使用 qt/c++ 将图像上传到 azure 存储

这是我的代码。构造标题时似乎有错误。*

    QByteArray data;
    QFile file("Bg.jpg");
      if (file.open(QIODevice::ReadOnly))
       {
            data.append(file.readAll());   //let's read the file
       }
    QString date = QDateTime::currentDateTime().toString("ddd, d MMM yyyy HH:mm:ss") + " GMT";
    QString header = QString("PUT\n\n\n%1\n\n\n\n\n\n\n\n\nx-ms-blob-type:BlockBlob\nx-ms-date:%2\nx-ms-version:2013-08-15\n/mycontainer/todate/").arg(data.length()).arg(date);

    QString urlUri = "http://myaccount.blob.core.windows.net/mycontainer/todate";
    QByteArray ba = header.toUtf8();

    unsigned char* signature = reinterpret_cast<unsigned char*>(ba.data());
    QByteArray kba = 

    QByteArray::fromBase64("key");
    unsigned char* key = (unsigned char*) kba.data();
    unsigned char result[EVP_MAX_MD_SIZE];
    unsigned int result_len;
    ENGINE_load_builtin_engines();
    ENGINE_register_all_complete();

    HMAC_CTX ctx;
    HMAC_CTX_init(&ctx);
    HMAC_Init_ex(&ctx, key, strlen((const char*)key), EVP_sha256(), NULL);
    HMAC_Update(&ctx, signature, strlen((const char*)signature));
    HMAC_Final(&ctx, result, &result_len);
    HMAC_CTX_cleanup(&ctx);

    QByteArray array = QByteArray::fromRawData((char*)result, result_len);
    array = array.toBase64();  

    String version = "2013-08-15";

    QNetworkAccessManager* manager = new QNetworkAccessManager();
    QNetworkRequest request;
    request.setUrl(QUrl(urlUri));

    request.setRawHeader("Content-  Length",QString::number(data.length()).toStdString().c_str());
    request.setRawHeader("Content-Type","application/octet-stream");
    request.setRawHeader("x-ms-blob-type","BlockBlob");
    request.setRawHeader("x-ms-date", date.toStdString().c_str());
    request.setRawHeader("x-ms-version", version.toStdString().c_str());
    request.setRawHeader("Authorization","SharedKey myaccount:"+array);


    manager->post(request,data);
    connect(manager, SIGNAL(finished(QNetworkReply*)), this,SLOT(manageCloudReply(QNetworkReply*)));

这是对请求的回应

AuthenticationFailed服务器未能验证请求。确保 Authorization 标头的值正确形成,包括签名。RequestId:82eb03cb-adf1-4797-bbc0-86c3e5ca3ec6 Time:2014-03-19T03:52:35.4790761Z在HTTP请求'3Dk7laN3XW+ASbQj0XddfmSnnuYfVSkhg/oRfSJekKY='中找到的MAC签名与任何计算的签名不同。服务器使用以下字符串进行签名:'POST

87163

应用程序/八位字节流

x-ms-blob-type:BlockBlob x-ms-date:Wed, 19 Mar 2014 11:52:05 GMT x-ms-version:2013-08-15 /logboxstorage/mycontainer/todate'。

任何帮助将不胜感激..谢谢!!!!

4

1 回答 1

0

查看以下代码行:

QString header = QString("PUT\n\n\n%1\n\n\n\n\n\n\n\n\nx-ms-blob-type:BlockBlob\nx-ms-date:%2\nx-ms-version:2013-08-15\n/mycontainer/todate/").arg(data.length()).arg(date);

/mycontainer/todate/. 您可以尝试删除它吗?

另请查看以下代码:

QString date = QDateTime::currentDateTime().toString("ddd, d MMM yyyy HH:mm:ss") + " GMT";

currentDateTime()将根据您的计算机返回本地日期/时间。它应该是UTC/GMT日期/时间。尝试使用currentDateTimeUtc().

我注意到的另一件事是您的请求是POST针对服务器的。应该是PUT请求。

于 2014-03-19T05:44:33.853 回答