Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DevSite Import: Download Media #1407

Open
grant opened this issue Jun 6, 2019 · 0 comments

Comments

Projects
None yet
3 participants
@grant
Copy link

commented Jun 6, 2019

Notice: The DevSite page below will soon be deleted per b/133171590. Please add this sample to GitHub if it is relevant still:

https://developers.google.com/api-client-library/dotnet/guide/media_download


Download Media

Resumable media download has been a feature in the Google API .NET client library since 1.4.0-beta. The Google API-specific libraries contain convenience methods for interacting with this feature.

The resumable media download protocol is similar to the resumable media upload protocol which is described, for example, on the media upload page for the Drive API.

The main class of interest is MediaDownloader. In this implementation of resumable media download, the media content is downloaded in chunks (chunk size is configurable).

Sample Code

If methods in the API-specific libraries contain the "supportsMediaDownload" parameter in the Discovery document, then the Download and DownloadAsync convenience methods are available in the request class. Those methods download the media data into a Stream object that you provide. For example:

{
    // Create the service using the client credentials.
    var storageService = new StorageService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "APP_NAME_HERE"
        });
    // Get the client request object for the bucket and desired object.
    var getRequest = storageService.Objects.Get("BUCKET_HERE", "OBJECT_HERE");
    using (var fileStream = new System.IO.FileStream(
        "FILE_PATH_HERE",
        System.IO.FileMode.Create,
        System.IO.FileAccess.Write))
    {
        // Add a handler which will be notified on progress changes.
        // It will notify on each chunk download and when the
        // download is completed or failed.
        getRequest.MediaDownloader.ProgressChanged += Download_ProgressChanged;
        getRequest.Download(fileStream);
    }
}

static void Download_ProgressChanged(IDownloadProgress progress)
{
    Console.WriteLine(progress.Status + " " + progress.BytesDownloaded);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.