如何使用Microsoft Translator Hub的HubUpload Post API及查询上传状态
Since you’ve already got the hang of the other Translator Hub APIs, let’s break down exactly how to use the HubUpload/Get endpoint to track your file upload status.
First, a quick clarification: this API exists to check the progress or outcome of an asynchronous file upload task to the Hub. Uploading files isn’t instant, so you’ll need this endpoint to confirm if your upload succeeded, is still processing, or failed.
Step 1: Get Your trackingId First
You can’t call HubUpload/Get out of nowhere—you need to initiate a file upload via another HubUpload endpoint (like HubUpload/Create or the upload initiation API first). When you send that initial upload request, the response will include a unique integer trackingId—this is the key value you’ll pass to HubUpload/Get.
Step 2: Implement the Status Check (Refined Code)
Here’s a polished version of your sample code with critical details your original snippet missed, plus error handling:
public async Task<string> CheckHubUploadStatusAsync(string accessToken, int trackingId) { using var client = new RestClient("https://hub.microsofttranslator.com/api/HubUpload/Get"); var request = new RestRequest(Method.GET); // Mandatory: Add authentication header (your original code omitted this!) request.AddHeader("Authorization", $"Bearer {accessToken}"); // Add the tracking ID from your initial upload request request.AddParameter("trackingId", trackingId); try { var response = await client.ExecuteAsync(request); if (response.IsSuccessful) { // The response body will have status details (e.g., JSON or plain text) // Common status values: "InProgress", "Completed", "Failed" return $"Upload Status: {response.Content}"; } else { return $"Status check failed: {response.StatusCode} - {response.ErrorMessage}"; } } catch (Exception ex) { return $"Error checking status: {ex.Message}"; } }
Key Things to Remember:
- Authentication is non-negotiable: You must include the
Authorizationheader with your Bearer token. Without this, you’ll get an unauthorized error every time. trackingIdis unique per upload: Store this value right after you start the upload—you can’t track a task without it.- Poll for status if needed: If the response returns
InProgress, wait 10-30 seconds and call the endpoint again until you get a final status (CompletedorFailed). - Parse the response content: Depending on your request setup, the response might be a simple status string or a JSON object with more details (like error messages if the upload failed).
Typical Workflow:
- Call the Hub upload initiation API (e.g.,
HubUpload/Create) to start uploading your file, save the returnedtrackingId. - Wait a short delay, then call
HubUpload/Getwith thetrackingId. - If status is
InProgress, repeat the check after a few seconds. - Once you get
CompletedorFailed, handle the result (e.g., start training a model if successful, debug the error if failed).
内容的提问来源于stack exchange,提问作者Sam

