미디어 업로드

몇몇 API 메서드는 일반 본문 외에도 미디어 업로드를 지원합니다. 이 경우, 추가 요청을 얻기 위해 일반 요청 메서드가 오버로드됩니다. 업로드하려면 Stream하세요.

개요

업로드할 Stream의 경우 재개 가능 옵션을 사용해야 합니다. 미디어 업로드를 사용하면 스트림을 더 작은 단위로 업로드할 수 있습니다. 이 기능은 대용량 파일을 전송하거나 네트워크 중단이나 다른 전송의 가능성에 대해 높은 편이며 또한 네트워크 장애 시 대역폭 사용량을 줄일 수 있습니다. 대용량 파일 업로드를 처음부터 다시 시작할 필요가 없기 때문입니다.

ResumableMediaUpload

재개 가능한 미디어 업로드는 Google API .NET 클라이언트 라이브러리의 기능이었습니다. 1.2.0-beta부터 지원됩니다. Google API 고유 라이브러리에는 사용할 수 있습니다.

재개 가능한 미디어 업로드 프로토콜은 Drive API의 미디어 업로드 페이지에서 찾을 수 있습니다. 기본적으로 다룰 주제는 ResumableUpload 이 구현에서는 미디어 콘텐츠가 청크로 업로드됩니다.

기본 청크 크기는 10MB이지만 요청의 ChunkSize 속성을 256KB의 배수로 설정합니다. 요청에 서버 오류가 발생하면 지수 백오프 정책은 업로드되지 않은 바이트를 다시 보내는 데 사용됩니다. 기본적으로 지수 백오프는 각 클라이언트 요청에 대해 사용 설정됩니다. 구성 시 기본 동작을 변경할 수 있습니다. 새 서비스 객체를 <ph type="x-smartling-placeholder"></ph> BaseClientService.InitializerDefaultExponentialBackOffPolicy 속성 및/또는 <ph type="x-smartling-placeholder"></ph> HttpClientInitializer 속성을 IConfigurableHttpClientInitializer의 자체 구현에 추가합니다. 몇 가지 백오프 정책을 추가합니다.

미디어 업로드를 지원하는 방법은 참조 문서에서 확인하세요. 이러한 API 메서드의 경우 편의 UploadUploadAsync 메서드가 추가되었습니다. 이러한 메서드는 Stream를 사용하여 업로드 및 콘텐츠 유형을 매개변수로 사용합니다.

업로드하는 스트림의 위치가 0인지 확인하세요. 그렇지 않으면 다음과 같은 오류가 발생합니다. 'System.InvalidOperationException: The given header was not found'(지정된 헤더를 찾을 수 없음)가 발생했습니다.

프레임워크의 HttpClient 동작으로 인해 클래스에서 업로드 시간이 초과되면 TaskCanceledException이 발생합니다. 이 예외가 표시되면Timeout 서비스 객체에서 사용하는 클라이언트입니다.

샘플 코드

// Create the service using the client credentials.
var service = new DriveService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = "Application_Name"
});

using var uploadStream = System.IO.File.OpenRead("Local_File_Name");

// Create the File resource to upload.
Google.Apis.Drive.v3.Data.File driveFile = new Google.Apis.Drive.v3.Data.File
{
    Name = "Drive_File_Name"
};
// Get the media upload request object.
FilesResource.CreateMediaUpload insertRequest = service.Files.Create(
    driveFile, uploadStream, "image/jpeg");

// Add handlers which will be notified on progress changes and upload completion.
// Notification of progress changed will be invoked when the upload was started,
// on each upload chunk, and on success or failure.
insertRequest.ProgressChanged += Upload_ProgressChanged;
insertRequest.ResponseReceived += Upload_ResponseReceived;

await insertRequest.UploadAsync();

static void Upload_ProgressChanged(IUploadProgress progress) =>
    Console.WriteLine(progress.Status + " " + progress.BytesSent);

static void Upload_ResponseReceived(Google.Apis.Drive.v3.Data.File file) =>
    Console.WriteLine(file.Name + " was uploaded successfully");