문서 파일을 Document AI API로 전송할 때 파일이 20MB 이하인 경우에만 온라인 처리를 사용하여 RawDocument.content 필드에 데이터를 직접 전송할 수 있습니다.
입력 파일은 바이너리 형식이며 Document AI로 전송하기 전에 인코딩해야 합니다.
입력 파일이 온라인 처리 한도를 초과하는 경우 처리를 위해 전송하려면 파일을 Cloud Storage 버킷에 저장해야 하며, 이때 인코딩은 필요하지 않습니다. 자세한 내용은 일괄 처리 문서를 참고하세요.
명령줄 사용
gRPC 요청 내에 바이너리 데이터를 직접 쓸 수 있습니다. 그러나 REST 요청을 할 때는 JSON이 사용됩니다. JSON은 바이너리 데이터를 직접 지원하지 않는 텍스트 형식이므로, Base64 인코딩을 사용하여 바이너리 데이터를 텍스트로 변환해야 합니다.
대부분의 개발 환경에는 바이너리를 ASCII 텍스트 데이터로 인코딩하는 기본 base64 유틸리티가 포함되어 있습니다. 파일을 인코딩하려면 다음 안내를 따르세요.
Linux
base64 명령줄 도구를 사용하여 파일을 인코딩합니다. -w 0 플래그를 사용하여 줄바꿈을 방지해야 합니다.
텍스트 편집기를 통해 바이너리 데이터를 요청에 삽입하는 것은 바람직하지 않으며 실용적이지도 않습니다. 실제로는 클라이언트 코드에 base64 인코딩 파일을 삽입합니다. 지원되는 모든 프로그래밍 언어에는 base64 인코딩 콘텐츠를 위한 기본 메커니즘이 있습니다.
Python
# Import the base64 encoding library.importbase64# Pass the image data to an encoding function.defencode_image(image):withopen(image,"rb")asimage_file:encoded_string=base64.b64encode(image_file.read())returnencoded_string
Node.js
// Read the file into memory.varfs=require('fs');varimageFile=fs.readFileSync('/path/to/file');// Convert the image data to a Buffer and base64 encode it.varencoded=Buffer.from(imageFile).toString('base64');
자바
// Import the Base64 encoding library.importorg.apache.commons.codec.binary.Base64;// Encode the image.StringencodedString=Base64.getEncoder().encodeToString(imageFile.getBytes());
Go
import("bufio""encoding/base64""io""os")// Open image file.f,_:=os.Open("image.jpg")// Read entire image into byte slice.reader:=bufio.NewReader(f)content,_:=io.ReadAll(reader)// Encode image as base64.base64.StdEncoding.EncodeToString(content)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-09-04(UTC)"],[[["\u003cp\u003eBase64 encoding is necessary when sending binary files to the Document AI API via REST requests, as JSON, the format used, does not directly support binary data.\u003c/p\u003e\n"],["\u003cp\u003eFor online processing, files under 20 MB can be sent directly in the \u003ccode\u003eRawDocument.content\u003c/code\u003e field, but they must be Base64 encoded first.\u003c/p\u003e\n"],["\u003cp\u003eFiles exceeding the 20 MB online processing limit must be stored in a Cloud Storage bucket for batch processing, and they do not require Base64 encoding.\u003c/p\u003e\n"],["\u003cp\u003eVarious command-line tools and methods, such as the \u003ccode\u003ebase64\u003c/code\u003e command in Linux and macOS, and \u003ccode\u003eConvert.ToBase64String\u003c/code\u003e in PowerShell, are available to encode files into Base64 format.\u003c/p\u003e\n"],["\u003cp\u003eClient libraries in different programming languages like Python, Node.js, Java, and Go provide built-in mechanisms for Base64 encoding of files, which are preferred over manually editing a JSON request.\u003c/p\u003e\n"]]],[],null,["# Base64 encoding\n===============\n\nWhen sending document files to the Document AI API, you can send\ndata directly in the [`RawDocument.content`](/document-ai/docs/reference/rest/v1/RawDocument) field with\n[online processing](/document-ai/docs/send-request#online-process) **only** if\nyour file is [20 MB or less](/document-ai/quotas#content_limits).\nThe input file will be in a binary format, which must be encoded\nbefore sending to Document AI.\n\nIf your input file exceeds the online processing limits, it must be stored in a\nCloud Storage bucket in order to be sent for processing, which does not\nrequire encoding. Refer to the\n[batch processing documentation](/document-ai/docs/send-request#batch-process) for details.\n\nUsing the command line\n----------------------\n\nWithin a gRPC request, you can simply write binary data out directly;\nhowever, JSON is used when making a REST request. JSON\nis a text format that does not directly support binary data, so you will need to\nconvert such binary data into text using\n[Base64](https://en.wikipedia.org/wiki/Base64) encoding.\n\nMost development environments contain a native `base64` utility to\nencode a binary into ASCII text data. To encode a file: \n\n### Linux\n\nEncode the file using the `base64` command line tool, making sure to\nprevent line-wrapping by using the `-w 0` flag: \n\n```\nbase64 INPUT_FILE -w 0 \u003e OUTPUT_FILE\n```\n\n### macOS\n\nEncode the file using the `base64` command line tool: \n\n```\nbase64 -i INPUT_FILE -o OUTPUT_FILE\n```\n\n### Windows\n\nEncode the file using the `Base64.exe` tool: \n\n```\nBase64.exe -e INPUT_FILE \u003e OUTPUT_FILE\n```\n\n### PowerShell\n\nEncode the file using the `Convert.ToBase64String` method: \n\n```\n[Convert]::ToBase64String([IO.File]::ReadAllBytes(\"./INPUT_FILE\")) \u003e OUTPUT_FILE\n```\n\nCreate a JSON request file, inlining the base64-encoded data: \n\n### JSON\n\n\n```json\n{\n \"skipHumanReview\": skipHumanReview,\n \"rawDocument\": {\n \"mimeType\": \"\u003cvar translate=\"no\"\u003eMIME_TYPE\u003c/var\u003e\",\n \"content\": \"\u003cvar translate=\"no\"\u003eBASE64_ENCODED_DATA\u003c/var\u003e\"\n },\n \"fieldMask\": \"\u003cvar translate=\"no\"\u003eFIELD_MASK\u003c/var\u003e\"\n}\n```\n\n\u003cbr /\u003e\n\nUsing client libraries\n----------------------\n\nEmbedding binary data into requests through text editors is neither\ndesirable or practical. In practice, you will be embedding base64 encoded files\nwithin client code. All supported programming languages have built-in mechanisms\nfor base64 encoding content. \n\n### Python\n\n # Import the base64 encoding library.\n import base64\n\n # Pass the image data to an encoding function.\n def encode_image(image):\n with open(image, \"rb\") as image_file:\n encoded_string = base64.b64encode(image_file.read())\n return encoded_string\n\n### Node.js\n\n // Read the file into memory.\n var fs = require('fs');\n var imageFile = fs.readFileSync('/path/to/file');\n\n // Convert the image data to a Buffer and base64 encode it.\n var encoded = Buffer.from(imageFile).toString('base64');\n\n### Java\n\n // Import the Base64 encoding library.\n import org.apache.commons.codec.binary.Base64;\n\n // Encode the image.\n String encodedString = Base64.getEncoder().encodeToString(imageFile.getBytes());\n\n### Go\n\n import (\n \"bufio\"\n \"encoding/base64\"\n \"io\"\n \"os\"\n )\n\n // Open image file.\n f, _ := os.Open(\"image.jpg\")\n\n // Read entire image into byte slice.\n reader := bufio.NewReader(f)\n content, _ := io.ReadAll(reader)\n\n // Encode image as base64.\n base64.StdEncoding.EncodeToString(content)"]]