Azure 文档翻译器无法正常工作,但获取响应代码为 202

问题描述 投票:0回答:1

我正在尝试使用 Azure 文档翻译服务将文档从英语翻译为日语。 BLOB 的层次结构如下所示。

|-storage account
|--container (test)
|---translation-input url (ex:https://storageaccount/test/sampleTranslation.docx)
|---translation-output url (ex:https://storageaccount/test/Converted_sampleTranslation.docx)

我正在使用 json 输入使用 httpclient:

{
   "inputs":[
      {
         "storageType":"File",
         "source":{
            "sourceUrl":"https://storageaccount/test/sampleTranslation.docx?sp=racwl&st=24Z&se=2025-07-31T17:58:24Z&spr=https&sv=2020-08-04&sr=c&sig=xsd%3D%3D"
         },
         "targets":[
            {
               "targetUrl":"https://storageaccount/test/Converted_sampleTranslation.docx?sp=racwl&st=kjkZ&se=2025-07-31T17:58:24Z&spr=https&sv=2020-08-04&sr=c&sig=3LT7WCD5Tn7KK1JBTxaSOs%3D",
               "language":"ja"
            }
         ]
      }
   ]
}

代码片段:

HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage();
{

    StringContent content = new StringContent(json, Encoding.UTF8, "application/json");

    request.Method = HttpMethod.Post;
    request.RequestUri = new Uri(endpoint + route);
    request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
    request.Content = content;

    HttpResponseMessage response = await client.SendAsync(request);
    string result = response.Content.ReadAsStringAsync().Result;
    if (response.IsSuccessStatusCode)
    {

        outcome = "https://storageaccount.blob.core.windows.net/test/" + fname;
      
    }
    else
        log.Error("Error in Translate Document");

}

我收到的响应状态为 202(表示成功),但指定的 targeturl 没有发生任何翻译,

注意:相同的代码在几个月前按预期工作。但今天没有得到预期的结果。有什么问题吗?

c# azure azure-cloud-services language-translation
1个回答
0
投票

这对我有用。

  • 我创建了两个容器
  • 对于源文件:
    source
    ,具有
    read &list
    访问权限,对于翻译文件:
    target
    ,具有
    read,write&list
    访问权限,我生成了容器级SAS。

My code
:

using Azure.AI.Translation.Document;
using Azure;
using System;

namespace ConsoleApp3
{
    public class Program
    {
        public static async Task Main()
        {
            string endpoint = "https://doctranslation2703.cognitiveservices.azure.com/";
            string apiKey = "xxxxxxxxxxxxxxxx";
            var client = new DocumentTranslationClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

            Uri target = new Uri("https://translationstrgaccount.blob.core.windows.net/target?sp=rwl&st=2024-03-28T06:20:34Z&se=2024-03-30T14:20:34Z&spr=https&sv=2022-11-02&sr=c&sig=xxxxxxxxxxxxxxxxxx");
            Uri source = new Uri("https://translationstrgaccount.blob.core.windows.net/source?sp=rl&st=2024-03-28T06:19:34Z&se=2024-03-30T14:19:34Z&spr=https&sv=2022-11-02&sr=c&sig=xxxxxxxxxxxxxxxxxx");
                
            DocumentTranslationOperation operation = await client.StartTranslationAsync(new DocumentTranslationInput(sourceUri:source,targetUri: target,"ja"));

            await operation.WaitForCompletionAsync();

            Console.WriteLine($"Succeeded: {operation.DocumentsSucceeded}");

        }
    }
}

OUTPUT

Hi! My name is Vivek

This is a English sentance.

它在容器中创建了一个具有翻译语言的同名文件。

過度の渇望!私の名前はヴィヴェックです

これは英語の文章です。

© www.soinside.com 2019 - 2024. All rights reserved.