raidenyn / yandexdisk.client

Yandex Disk API Client for .NET
MIT License
41 stars 14 forks source link

Yandex Disk API Client for .NET

Yandex Disk Rest API client library for .NET Standard

Build status

How to install

From NuGet or MyGet

PM> Install-Package YandexDisk.Client

Changes in version 1.3

How to use

Example of uploading file to Yandex Disk

async Task UploadSample()
{
  //You should have oauth token from Yandex Passport.
  //See https://tech.yandex.ru/oauth/
  string oauthToken = "<token hear>"

  // Create a client instance
  IDiskApi diskApi = new DiskHttpApi(oauthToken);

  //Upload file from local
  await diskApi.Files.UploadFileAsync(path: "/foo/myfile.txt",
                                      overwrite: false,
                                      localFile: @"C:\myfile.txt",
                                      cancellationToken: CancellationToken.None);
}

Example of downloading files from Yandex Disk

async Task DownloadAllFilesInFolder(IDiskApi diskApi)
{
    //Getting information about folder /foo and all files in it
    Resource fooResourceDescription = await diskApi.MetaInfo.GetInfoAsync(new ResourceRequest
                                            {
                                                Path = "/foo", //Folder on Yandex Disk
                                            }, CancellationToken.None);

    //Getting all files from response
    IEnumerable<Resource> allFilesInFolder =
        fooResourceDescription.Embedded.Items.Where(item => item.Type == ResourceType.File);

    //Path to local folder for downloading files
    string localFolder = @"C:\foo";

    //Run all downloadings in parallel. DiskApi is thread safe.
    IEnumerable<Task> downloadingTasks =
        allFilesInFolder.Select(file =>
          diskApi.Files.DownloadFileAsync(path: file.Path,
                                          localPath: System.IO.Path.Combine(localFolder, file.Name)));

    //Wait all done
    await Task.WhenAll(downloadingTasks);
}

How to build

Open solution src/YandexDisk.Client.sln in Visual Studio 2017 (support C# 7.3 is required). Run build solution.