Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

azure - Upload large files ( > 4MB) to OneDrive using Graph API and JSON batching

I can upload large files (sie > 4 MB) to OneDrive using the standard process mentioned by Microsoft (create upload session and upload chunk by chunk).

Now I want to do this using JSON batching as the batching can help me to upload the item and also patch properties for the time in one request. But with JSON batching I am seeing an error "The maximum request length supported is 4MB."

Complete error response below:

"error": {

"code": "BadRequest",

"message": "The maximum request length supported is 4MB.",

"innerError": {

  "date": "2021-01-08T16:41:09",

  "request-id": "34d67671-4bbb-42cb-8633-337ee7ef8fc4",

  "client-request-id": "34d67671-4bbb-42cb-8633-337ee7ef8fc4"

}

}

Is there any limitation where JSON batching does not support the upload of large items?

The JSON batching works for me for small items.

My sample code for reference below (I am using Boost Beast library with my C++ code):

            strUploadURL = CreateUploadSession(); // Returns upload URL 

        std::unordered_map<boost::beast::http::field, std::string> fieldMapPtr;
        fieldMapPtr[boost::beast::http::field::content_type] = "application/json";
        std::string strDependsOn;

        std::string strMsgBody;
        strMsgBody.append(""");
        strMsgBody.append(base64_encode(reinterpret_cast<const unsigned char*>(GetItemData()), GetItemData()))); // Base 64 encoding of item data needed for JSON batching
        strMsgBody.append(""");

        std::string strHeaders;         
        strHeaders.append("{"Content-Type":"",");

        std::stringstream stream;
        stream << "bytes " << std::to_string(0) << "-" << std::to_string(strMsgBody.size()-3);
        stream << "/" << std::to_string(strMsgBody.size() - 2);         
        strHeaders.append(""content_range":"");
        strHeaders.append(stream.str());
        strHeaders.append("",");
        strHeaders.append(""Content-Length":"");
        strHeaders.append(to_string(strMsgBody.length()));
        strHeaders.append(""}");

        std::string strBatchRequestOne = PrepareJSONbatchRequest("1", "PUT", strUploadURL, strMsgBody, strHeaders, strDependsOn); // Prepares the JSON request

        strMsgBody = "{";
        strMsgBody.append(FillAttribInfo(dataElemReq.item));
        strMsgBody.append("}");
        strHeaders = "{"Content-Type":"application/json"}";
        strDependsOn = "1";

        std::string strBatchRequestTwo = PrepareJSONbatchRequest("2", "PATCH", strUploadURL, strMsgBody, strHeaders, strDependsOn); // Prepares PATCH part of JSON request

        std::string strJSONBatch = "{"requests":[";
        strJSONBatch.append(strBatchRequestOne);
        strJSONBatch.append(",");
        strJSONBatch.append(strBatchRequestTwo);
        strJSONBatch.append("]}");
        fieldMapPtr[boost::beast::http::field::content_length] = std::to_string(strJSONBatch.size());

        batch.AddRequest(graphQueryBuilderPtr->GetBatchRequestURL().c_str(), 
        boost::beast::http::verb::post, token, dataElemReq.item->GetId(), -1, 0, &fieldMapPtr, 
        strJSONBatch); // Executes JSON batch request

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...