直接および再開可能なメディア アップロード
コレクションでコンテンツを整理 必要に応じて、コンテンツの保存と分類を行います。
このドキュメントでは、Cloud Storage から直接、再開可能なメディア アップロードを クライアント ライブラリを使用できます。
再開可能なメディアのアップロード
大きなメディア ファイルをサーバーにアップロードする場合は、再開可能なメディア アップロードを使用して チャンクごとに送信されます。Google API で生成されるライブラリには、 再開可能なメディア アップロードを操作するためのコンビニエンス メソッドです。
再開可能なメディア アップロードのプロトコルは、再開可能なメディア アップロードと類似しています。 (Google Drive API ドキュメントに記載)のプロトコルを使用します。
プロトコル設計
次のシーケンス図は、再開可能なメディア アップロード プロトコルの仕組みを示しています。

実装の詳細
主なクラスは、次のとおりです。 MediaHttpUploader MediaHttpProgressListener をご覧ください。
サービス固有の生成されたライブラリのメソッドに mediaUpload
が含まれている場合 パラメータ(ディスカバリ ドキュメント) その場合は、これらのメソッド用にコンビニエンス メソッドが作成され、 InputStreamContent 渡します。(Google API でのメディア アップロードの使用について詳しくは、 検出サービスについては、以下をご覧ください。 メディア アップロード。
たとえば、Drive API の insert
メソッドなどです。 は mediaUpload
をサポートしています。次のコードを使用してファイルをアップロードできます。
class CustomProgressListener implements MediaHttpUploaderProgressListener { public void progressChanged(MediaHttpUploader uploader) throws IOException { switch (uploader.getUploadState()) { case INITIATION_STARTED: System.out.println("Initiation has started!"); break; case INITIATION_COMPLETE: System.out.println("Initiation is complete!"); break; case MEDIA_IN_PROGRESS: System.out.println(uploader.getProgress()); break; case MEDIA_COMPLETE: System.out.println("Upload is complete!"); } } } File mediaFile = new File("/tmp/driveFile.jpg"); InputStreamContent mediaContent = new InputStreamContent("image/jpeg", new BufferedInputStream(new FileInputStream(mediaFile))); mediaContent.setLength(mediaFile.length()); Drive.Files.Insert request = drive.files().insert(fileMetadata, mediaContent); request.getMediaHttpUploader().setProgressListener(new CustomProgressListener()); request.execute();
再開可能なメディア アップロード機能を、サービス固有の 表示できます。次に例を示します。
File mediaFile = new File("/tmp/Test.jpg"); InputStreamContent mediaContent = new InputStreamContent("image/jpeg", new BufferedInputStream(new FileInputStream(mediaFile))); mediaContent.setLength(mediaFile.length()); MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, transport, httpRequestInitializer); uploader.setProgressListener(new CustomProgressListener()); HttpResponse response = uploader.upload(requestUrl); if (!response.isSuccessStatusCode()) { throw GoogleJsonResponseException(jsonFactory, response); }
メディアの直接アップロード
再開可能なメディアのアップロードはデフォルトで有効になっていますが、無効にして 直接メディア アップロードを使用します。たとえば、小さいファイルをアップロードする場合などです。直接 メディア アップロードは 1.9.0-betaで導入されました。 Java 用 Google API クライアント ライブラリのバージョンです。
直接メディア アップロードでは、1 回の HTTP リクエストでファイル全体がアップロードされますが、 再開可能なメディア アップロード プロトコル。複数のリクエストでファイルをアップロードします。 直接アップロードを行うと、HTTP リクエストの数は減りますが、 大規模なプロジェクトで発生する可能性のある障害(接続の障害など)の アップロードします。
メディアの直接アップロードの使用方法は、前述の 再開可能なメディアのアップロードと、MediaHttpUploader を指示する次の呼び出し 直接アップロードのみを行う場合:
mediaHttpUploader.setDirectUploadEnabled(true);
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-07-26 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-26 UTC。"],[[["\u003cp\u003eThis document explains how to upload media to Google APIs using the Java Client Library, focusing on resumable and direct upload methods.\u003c/p\u003e\n"],["\u003cp\u003eResumable media upload is recommended for large files, allowing uploads in chunks and offering better resilience to network interruptions, similar to Google Drive's approach.\u003c/p\u003e\n"],["\u003cp\u003eDirect media upload sends the entire file in a single request, suitable for smaller files but with a higher risk of failure for larger ones.\u003c/p\u003e\n"],["\u003cp\u003eThe Java Client Library provides \u003ccode\u003eMediaHttpUploader\u003c/code\u003e and \u003ccode\u003eMediaHttpProgressListener\u003c/code\u003e for managing uploads, with built-in support for resumable uploads and an option to enable direct uploads.\u003c/p\u003e\n"],["\u003cp\u003eCode samples demonstrate how to use both resumable and direct uploads with the library, including progress tracking and handling potential errors.\u003c/p\u003e\n"]]],[],null,["\u003cbr /\u003e\n\nThis document describes how to use direct and resumable media uploads with\nthe Google API Client Library for Java.\n\n\u003cbr /\u003e\n\nResumable media upload\n\nWhen you upload a large media file to a server, use *resumable media upload* to\nsend the file chunk by chunk. The Google API generated libraries contain\nconvenience methods for interacting with resumable media upload.\n\nThe resumable media upload protocol is similar to the resumable media upload\nprotocol described in the [Google Drive API documentation](https://developers.google.com/drive/web/manage-uploads#resumable).\n\nProtocol design\n\nThe following sequence diagram shows how the resumable media upload protocol works:\n\n\nImplementation details\n\nThe main classes of interest are\n[MediaHttpUploader](https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/media/MediaHttpUploader.html)\nand [MediaHttpProgressListener](https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/media/MediaHttpUploaderProgressListener.html).\n\nIf methods in the service-specific generated libraries contain the `mediaUpload`\nparameter in the [Discovery document](https://developers.google.com/discovery/v1/using),\nthen a convenience method is created for these methods that takes an\n[InputStreamContent](https://googleapis.dev/java/google-http-client/latest/com/google/api/client/http/InputStreamContent.html)\nas a parameter. (For more about using media upload with the Google APIs\nDiscovery Service, see\n[Media upload](https://developers.google.com/discovery/v1/using#discovery-doc-methods-mediaupload).)\n\nFor example, the `insert` method of the [Drive API](https://developers.google.com/drive/api/v2/reference)\nsupports `mediaUpload`, and you can use the following code to upload a file: \n\n```java\nclass CustomProgressListener implements MediaHttpUploaderProgressListener {\n public void progressChanged(MediaHttpUploader uploader) throws IOException {\n switch (uploader.getUploadState()) {\n case INITIATION_STARTED:\n System.out.println(\"Initiation has started!\");\n break;\n case INITIATION_COMPLETE:\n System.out.println(\"Initiation is complete!\");\n break;\n case MEDIA_IN_PROGRESS:\n System.out.println(uploader.getProgress());\n break;\n case MEDIA_COMPLETE:\n System.out.println(\"Upload is complete!\");\n }\n }\n}\n\nFile mediaFile = new File(\"/tmp/driveFile.jpg\");\nInputStreamContent mediaContent =\n new InputStreamContent(\"image/jpeg\",\n new BufferedInputStream(new FileInputStream(mediaFile)));\nmediaContent.setLength(mediaFile.length());\n\nDrive.Files.Insert request = drive.files().insert(fileMetadata, mediaContent);\nrequest.getMediaHttpUploader().setProgressListener(new CustomProgressListener());\nrequest.execute();\n```\n\nYou can also use the resumable media upload feature without the service-specific\ngenerated libraries. Here is an example: \n\n```java\nFile mediaFile = new File(\"/tmp/Test.jpg\");\nInputStreamContent mediaContent =\n new InputStreamContent(\"image/jpeg\",\n new BufferedInputStream(new FileInputStream(mediaFile)));\nmediaContent.setLength(mediaFile.length());\n\nMediaHttpUploader uploader = new MediaHttpUploader(mediaContent, transport, httpRequestInitializer);\nuploader.setProgressListener(new CustomProgressListener());\nHttpResponse response = uploader.upload(requestUrl);\nif (!response.isSuccessStatusCode()) {\n throw GoogleJsonResponseException(jsonFactory, response);\n}\n```\n\n\u003cbr /\u003e\n\nDirect media upload\n\nResumable media upload is enabled by default, but you can disable it and use\ndirect media upload instead, for example if you are uploading a small file. Direct\nmedia upload was introduced in the [1.9.0-beta](http://google-api-java-client.blogspot.com/2012/05/version-190-beta-released.html)\nversion of the Google API Client Library for Java.\n\nDirect media upload uploads the whole file in one HTTP request, as opposed to\nthe resumable media upload protocol, which uploads the file in multiple requests.\nDoing a direct upload reduces the number of HTTP requests but increases the\nchance of failures (such as connection failures) that can happen with large\nuploads.\n\nThe usage for direct media upload is the same as what is described above for\nresumable media upload, plus the following call that tells [MediaHttpUploader](https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/media/MediaHttpUploader.html)\nto only do direct uploads: \n\n```java\nmediaHttpUploader.setDirectUploadEnabled(true);\n```"]]