SDK または API を使用して同期処理パラメーター x-oss-process=doc/polish を指定し、ドキュメントをインテリジェントに洗練できます。この機能は、ドキュメントの文言、構造、レイアウトを最適化して、より明確で読みやすくします。
前提条件
Intelligent Media Management (IMM) プロジェクトを作成し、それを宛先の Object Storage Service (OSS) バケットにアタッチします。詳細については、「クイックスタート」および「AttachOSSBucket」をご参照ください。
IMM にアクセスするために使用する ID に、必要な権限を付与します。
パラメーター
アクション: doc/polish
次の表にパラメーターを示します。
パラメーター | タイプ | 必須 | 説明 |
content | string | はい | 洗練したいコンテンツ。コンテンツは URL セーフな Base64 でエンコードする必要があります。 説明 コンテンツの長さは最大 19,500 バイトです。 |
format | string | いいえ | 応答を返すモード。有効な値:
|
次の表にレスポンスパラメーターを示します。
パラメーター | タイプ | 説明 |
RequestId | string | リクエスト ID。 |
Output | struct | 出力。 子ノード: Text、FinishReason |
Text | string | 応答のコンテンツ。 親ノード: Output |
FinishReason | string | 結果のステータス。有効な値:
親ノード: Output |
SDK の使用
次のコードは、一般的な SDK を使用して処理パラメーターを指定し、ドキュメントをインテリジェントに洗練する方法の例を示しています。他の SDK を使用するには、これらの例に基づいてコードを調整してください。
Java
Java SDK 3.17.4 以降が必要です。
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.common.utils.IOUtils;
import com.aliyun.oss.model.GenericResult;
import com.aliyun.oss.model.ProcessObjectRequest;
import java.io.IOException;
import java.util.Formatter;
public class Demo {
public static void main(String[] args) throws ClientException, com.aliyuncs.exceptions.ClientException {
// バケットが配置されているリージョンのエンドポイントにエンドポイントを設定します。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Alibaba Cloud リージョンの ID (例: cn-hangzhou) を指定します。
String region = "cn-hangzhou";
// 環境変数からアクセス資格情報を取得します。このコードを実行する前に、OSS_ACCESS_KEY_ID および OSS_ACCESS_KEY_SECRET 環境変数が設定されていることを確認してください。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// バケット名を指定します。
String bucketName = "examplebucket";
// ファイル名を指定します。これはプレースホルダーとしてのみ使用されます。インテリジェントなドキュメントの洗練を使用する場合、このファイルの内容は読み取られません。
String key ="example.docx";
// 洗練するテキストを指定します。
String content = "The solar system consists of the Sun and the celestial bodies that orbit it, including eight planets. In order of their distance from the Sun, from nearest to farthest, these planets are: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune.";
String encodeContent = BinaryUtil.toBase64String(content.getBytes()).replaceAll("\\+","-")
.replaceAll("/","_").replaceAll("=","");
// OSSClient インスタンスを作成します。
// OSSClient インスタンスが不要になったら、shutdown メソッドを呼び出してリソースを解放します。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
StringBuilder sbStyle = new StringBuilder();
Formatter styleFormatter = new Formatter(sbStyle);
// インテリジェントなドキュメントの洗練命令を構築します。
styleFormatter.format("doc/polish,content_%s",
encodeContent);
System.out.println(sbStyle.toString());
ProcessObjectRequest request = new ProcessObjectRequest(bucketName, key, sbStyle.toString());
GenericResult processResult = ossClient.processObject(request);
String json = IOUtils.readStreamAsString(processResult.getResponse().getContent(), "UTF-8");
processResult.getResponse().getContent().close();
System.out.println(json);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
PHP
PHP SDK 2.7.0 以降が必要です。
<?php
// 環境変数からアクセス資格情報を取得します。このコードを実行する前に、OSS_ACCESS_KEY_ID および OSS_ACCESS_KEY_SECRET 環境変数が設定されていることを確認してください。
$ak = getenv('OSS_ACCESS_KEY_ID');
$sk = getenv('OSS_ACCESS_KEY_SECRET');
// バケット名 (例: examplebucket) を指定します。
$bucket = 'examplebucket';
// ファイル名を指定します。これはプレースホルダーとしてのみ使用されます。インテリジェントなドキュメントの洗練を使用する場合、このファイルの内容は読み取られません。
$objectKey = 'example.docx';
// 洗練するテキストを指定します。
$txt = "The solar system consists of the Sun and the celestial bodies that orbit it, including eight planets. In order of their distance from the Sun, from nearest to farthest, these planets are: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune.";
$base64url = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($txt));
// インテリジェントなドキュメントの洗練命令を構築します。
$body = sprintf("x-oss-process=doc/polish,content_%s", $base64url);
$httpVerb = 'POST';
$contentMd5 = base64_encode(md5($body, true));
$contentType = '';
$date = gmdate('D, d M Y H:i:s T');
$stringToSign = $httpVerb . "\n" . $contentMd5 . "\n" . $contentType . "\n" . $date . "\n" . "/{$bucket}/{$objectKey}?x-oss-process";
$signature = base64_encode(hash_hmac('sha1', $stringToSign, $sk, true));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://{$bucket}.oss-cn-hangzhou.aliyuncs.com/{$objectKey}?x-oss-process");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Date: ' . $date,
'Authorization: OSS ' . $ak . ':' . $signature,
'Content-Type: ' . $contentType,
'Content-Md5:' . $contentMd5,
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($response === false) {
echo "Error: " . curl_error($ch);
} else {
if ($httpcode == 200) {
var_dump($response);
} else {
echo "Error: HTTP code " . $httpcode;
}
}Go
Go SDK 3.0.2 以降が必要です。
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
type TextData struct {
RequestId string `json:"RequestId"`
Output struct {
Text string `json:"Text"`
FinishReason string `json:"FinishReason"`
} `json:"Output"`
}
func main() {
// 環境変数から一時的なアクセス資格情報を取得します。このコードを実行する前に、OSS_ACCESS_KEY_ID、OSS_ACCESS_KEY_SECRET、および OSS_SESSION_TOKEN 環境変数が設定されていることを確認してください。
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// OSSClient インスタンスを作成します。
client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "", oss.SetCredentialsProvider(&provider), oss.AuthVersion(oss.AuthV4), oss.Region("cn-hangzhou"))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// バケット名 (例: examplebucket) を指定します。
bucketName := "examplebucket"
bucket, err := client.Bucket(bucketName)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
params := make(map[string]interface{})
params["x-oss-process"] = nil
// 洗練するテキストを指定します。
txt := "The solar system consists of the Sun and the celestial bodies that orbit it, including eight planets. In order of their distance from the Sun, from nearest to farthest, these planets are: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune."
// インテリジェントなドキュメントの洗練命令を構築します。
data := fmt.Sprintf("x-oss-process=doc/polish,content_%v", base64.URLEncoding.EncodeToString([]byte(txt)))
// example.docx はバケット内のファイルで、プレースホルダーとして使用されます。インテリジェントなドキュメントの洗練を使用する場合、このファイルの内容は読み取られません。
response, err := bucket.Do("POST", "example.docx", params, nil, strings.NewReader(data), nil)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
defer response.Body.Close()
jsonData, err := io.ReadAll(response.Body)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
var text TextData
err = json.Unmarshal(jsonData, &text)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Printf("RequestId:%v\n", text.RequestId)
fmt.Printf("Text:%v\n", text.Output.Text)
fmt.Printf("FinishReason:%v\n", text.Output.FinishReason)
}
API の使用
インテリジェントなドキュメントの洗練を使用する場合、ファイル名を指定する必要があります。ただし、ファイル名はプレースホルダーとしてのみ使用されます。この機能は、content パラメーターの値を洗練するコンテンツとして使用します。
通常モード
洗練された結果全体を一度に返します。
リクエストの例
処理するファイル: example.doc
推敲用テキスト:「太陽系は、太陽、およびその周りを公転する天体(8 つの惑星を含む)で構成されています。太陽から近い順に、水星、金星、地球、火星、木星、土星、天王星、海王星です。」
応答フォーマット: json
POST /example.doc?x-oss-process HTTP/1.1
Host: doc-demo.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 28 Oct 2022 06:40:10 GMT
Authorization: OSS4-HMAC-SHA256 Credential=LTAI********************/20250417/cn-hangzhou/oss/aliyun_v4_request,Signature=a7c3554c729d71929e0b84489addee6b2e8d5cb48595adfc51868c299c0c218e
x-oss-process=doc/polish,content_5aSq6Ziz57O755Sx5aSq6Ziz5Lul5Y-K546v57uV5YW26L-Q6KGM55qE5aSp5L2T5p6E5oiQ77yM5YW25Lit5YyF5ous5YWr5aSn6KGM5pif44CC6L-Z5Lqb6KGM5pif5LiO5aSq6Ziz55qE6Led56a777yM5LuO6L-R5Yiw6L-c5L6d5qyh5piv77ya5rC05pif44CB6YeR5pif44CB5Zyw55CD44CB54Gr5pif44CB5pyo5pif44CB5Zyf5pif44CB5aSp546L5pif44CB5rW3546L5pif44CC応答の例
HTTP/1.1 200 OK
Server: AliyunOSS
Date: Thu, 10 Aug 2023 11:56:21 GMT
Content-Type: application/json;charset=UTF-8
Connection: close
Vary: Accept-Encoding
x-oss-request-id: 6597BEF94479D8313302D71D
x-oss-server-time: 2106
Content-Encoding: gzip
{
"RequestId":"6597BEF94479D8313302D71D",
"Output":{
"Text":"The solar system is composed of the Sun and numerous celestial bodies that orbit it, including eight major planets. In order of their distance from the Sun, from nearest to farthest, these eight planets are: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune.",
"FinishReason":"stop"
}
}SSE モード
洗練された結果をストリーミング方式でセグメントに分けて返します。
リクエストの例
処理するファイル: example.doc
推敲するテキスト:「太陽系は、太陽と、8 つの惑星を含むその周りを公転する天体で構成されています。太陽から近い順に、これらの惑星は水星、金星、地球、火星、木星、土星、天王星、海王星です。」
応答フォーマット: event-stream
POST /example.doc?x-oss-process HTTP/1.1
Host: doc-demo.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 28 Oct 2022 06:40:10 GMT
Authorization: OSS4-HMAC-SHA256 Credential=LTAI********************/20250417/cn-hangzhou/oss/aliyun_v4_request,Signature=a7c3554c729d71929e0b84489addee6b2e8d5cb48595adfc51868c299c0c218e
x-oss-process=doc/polish,format_event-stream,content_5aSq6Ziz57O755Sx5aSq6Ziz5Lul5Y-K546v57uV5YW26L-Q6KGM55qE5aSp5L2T5p6E5oiQ77yM5YW25Lit5YyF5ous5YWr5aSn6KGM5pif44CC6L-Z5Lqb6KGM5pif5LiO5aSq6Ziz55qE6Led56a777yM5LuO6L-R5Yiw6L-c5L6d5qyh5piv77ya5rC05pif44CB6YeR5pif44CB5Zyw55CD44CB54Gr5pif44CB5pyo5pif44CB5Zyf5pif44CB5aSp546L5pif44CB5rW3546L5pif44CC応答の例
HTTP/1.1 200 OK
Server: AliyunOSS
Date: Thu, 10 Aug 2023 11:55:03 GMT
Content-Type: text/event-stream;charset=UTF-8
Transfer-Encoding: chunked
Connection: close
x-oss-request-id: 690487C31AFF653634FED36C
x-oss-server-time: 2356
id: 0
event: Result
data: {"RequestId":"690487C31AFF653634FED36C","Output":{"Text":"The","FinishReason":"null"}}
id: 1
event: Result
data: {"RequestId":"690487C31AFF653634FED36C","Output":{"Text":"The solar","FinishReason":"null"}}
id: 2
event: Result
data: {"RequestId":"690487C31AFF653634FED36C","Output":{"Text":"The solar system is","FinishReason":"null"}}
id: 3
event: Result
data: {"RequestId":"690487C31AFF653634FED36C","Output":{"Text":"The solar system is composed of the","FinishReason":"null"}}
id: 4
event: Result
data: {"RequestId":"690487C31AFF653634FED36C","Output":{"Text":"The solar system is composed of the Sun and numerous celestial","FinishReason":"null"}}
id: 5
event: Result
data: {"RequestId":"690487C31AFF653634FED36C","Output":{"Text":"The solar system is composed of the Sun and numerous celestial bodies that orbit it, including","FinishReason":"null"}}
id: 6
event: Result
data: {"RequestId":"690487C31AFF653634FED36C","Output":{"Text":"The solar system is composed of the Sun and numerous celestial bodies that orbit it, including eight major planets. In order","FinishReason":"null"}}
id: 7
event: Result
data: {"RequestId":"690487C31AFF653634FED36C","Output":{"Text":"The solar system is composed of the Sun and numerous celestial bodies that orbit it, including eight major planets. In order of their distance from the Sun, from nearest","FinishReason":"null"}}
id: 8
event: Result
data: {"RequestId":"690487C31AFF653634FED36C","Output":{"Text":"The solar system is composed of the Sun and numerous celestial bodies that orbit it, including eight major planets. In order of their distance from the Sun, from nearest to farthest, these","FinishReason":"null"}}
id: 9
event: Result
data: {"RequestId":"690487C31AFF653634FED36C","Output":{"Text":"The solar system is composed of the Sun and numerous celestial bodies that orbit it, including eight major planets. In order of their distance from the Sun, from nearest to farthest, these eight planets are:","FinishReason":"null"}}
id: 10
event: Result
data: {"RequestId":"690487C31AFF653634FED36C","Output":{"Text":"The solar system is composed of the Sun and numerous celestial bodies that orbit it, including eight major planets. In order of their distance from the Sun, from nearest to farthest, these eight planets are: Mercury, Venus,","FinishReason":"null"}}
id: 11
event: Result
data: {"RequestId":"690487C31AFF653634FED36C","Output":{"Text":"The solar system is composed of the Sun and numerous celestial bodies that orbit it, including eight major planets. In order of their distance from the Sun, from nearest to farthest, these eight planets are: Mercury, Venus, Earth, Mars, Jupiter,","FinishReason":"null"}}
id: 12
event: Result
data: {"RequestId":"690487C31AFF653634FED36C","Output":{"Text":"The solar system is composed of the Sun and numerous celestial bodies that orbit it, including eight major planets. In order of their distance from the Sun, from nearest to farthest, these eight planets are: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Ura","FinishReason":"null"}}
id: 13
event: Result
data: {"RequestId":"690487C31AFF653634FED36C","Output":{"Text":"The solar system is composed of the Sun and numerous celestial bodies that orbit it, including eight major planets. In order of their distance from the Sun, from nearest to farthest, these eight planets are: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune","FinishReason":"null"}}
id: 14
event: Result
data: {"RequestId":"690487C31AFF653634FED36C","Output":{"Text":"The solar system is composed of the Sun and numerous celestial bodies that orbit it, including eight major planets. In order of their distance from the Sun, from nearest to farthest, these eight planets are: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune.","FinishReason":"stop"}}クォータと制限
インテリジェントなドキュメントの洗練は、同期処理 (x-oss-process) のみをサポートします。
リクエストの送信には POST メソッドを使用する必要があります。
匿名アクセスはサポートされていません。すべてのリクエストは署名および承認される必要があります。