class

YandexDisk::Client

Inherits Reference < Object

A client for the Yandex.Disk REST API.

Covers the whole documented surface: disk info, metadata and listings, directories, copy/move/delete, upload and download, publishing, custom properties, the Trash, and the background operations the API starts for anything it cannot finish inline.

client = YandexDisk::Client.new(ENV["YANDEX_DISK_TOKEN"])

client.mkdir_p("disk:/reports/2026")
client.upload("chart.png", "disk:/reports/2026/chart.png")
puts client.publish("disk:/reports/2026/chart.png").public_url

Instances are safe to share between fibers: every request checks a connection out of a pool for its duration.

Constants

DEFAULT_BASE_URL = "https://cloud-api.yandex.net/v1/disk"
LIST_PAGE_SIZE = 200

The API caps a listing page at 1000 items; 200 keeps responses small enough to stay responsive on a slow link.

Log = ::Log.for("yandex_disk.client")

Constructors

new(token : String, retry_policy : RetryPolicy = RetryPolicy.new, pool : ConnectionPool = ConnectionPool.new, base_url : String = DEFAULT_BASE_URL)
Source

Instance methods

close
Source
copy(from : String, to : String, overwrite : Bool = false, force_async : Bool = false) : OperationHandle | Nil

Copies from to to.

Returns nil when the API finished inline, or an OperationHandle when it moved the work to the background — which it does for non-empty folders. Pass the handle to #wait to block until it settles.

Source
delete(path : String, permanently : Bool = true, force_async : Bool = false) : OperationHandle | Nil

Deletes a resource. Directories are removed recursively.

With permanently false the resource lands in the Trash, where #trash_restore can still bring it back.

Source
disk_info

Total, used and trashed space, plus the paths of the system folders.

Source
download_public_to(public_key : String, destination : Path | String, path : String | Nil = nil) : Int64

Downloads a published resource without saving it to the Disk first.

Source
download_to(path : String, destination : Path | String) : Int64

Downloads path to the local file destination, returning its size.

The file is reopened and truncated on every attempt, so a retry after a half-finished transfer starts clean rather than appending to a stump.

Source
empty_trash

Permanently removes everything in the Trash.

Source
exists?(path : String) : Bool
Source
files(limit : Int32 = 20, offset : Int32 = 0, media_type : String | Nil = nil, fields : String | Nil = nil, sort : String | Nil = nil) : Array(Resource)

A flat list of every file on the Disk, newest path order, ignoring the directory tree. Useful for "find that file" without walking folders.

Source
find(path : String) : Resource | Nil

Metadata for a single resource, or nil when it does not exist.

Source
last_uploaded(limit : Int32 = 20, media_type : String | Nil = nil, fields : String | Nil = nil) : Array(Resource)

The most recently uploaded files, newest first.

Source
list(path : String, fields : String | Nil = nil, sort : String | Nil = nil) : Array(Resource)

Every entry directly inside path, following pagination.

Returns an empty array when the directory does not exist, so callers writing into a fresh remote root do not have to special-case it. Pass fields to trim the response down to the keys you actually read.

Source
metadata(path : String, fields : String | Nil = nil, limit : Int32 = 0, offset : Int32 = 0, sort : String | Nil = nil, preview_size : String | Nil = nil) : Resource

Metadata for a single resource. Raises NotFoundError when absent.

For a directory the first page of its contents comes back in Resource#items; use #list to page through all of it.

Source
mkdir(path : String) : Bool

Creates a single directory. Returns false when it already existed.

Source
mkdir_p(path : String) : Nil

Creates path and every missing parent directory.

Source
move(from : String, to : String, overwrite : Bool = false, force_async : Bool = false) : OperationHandle | Nil

Moves or renames from to to. See #copy for the return value.

Source
operation(id : String) : Operation

The current state of a background operation.

Source
public_metadata(public_key : String, path : String | Nil = nil, limit : Int32 = LIST_PAGE_SIZE, offset : Int32 = 0, sort : String | Nil = nil) : Resource

Metadata for someone else's published resource.

public_key is either the key or the https://yadi.sk/... URL. For a published folder, path addresses an entry inside it.

Source
publish(path : String) : Resource

Publishes a resource and returns it with public_url filled in.

The publish call itself only answers with a metadata link, so this makes a second request to read back the key and URL that were just minted.

Source
published(limit : Int32 = 20, offset : Int32 = 0, type : String | Nil = nil, fields : String | Nil = nil) : Array(Resource)

Everything the account currently has published.

Source
put_upload(href : String, body : IO, content_length : Int64, content_range : String | Nil = nil) : HTTP::Client::Response

Sends one PUT to a pre-authorised upload href.

The upload host is not the API host and takes no OAuth header. Retrying is left to the caller: the body has to be rewound between attempts, and only the caller knows how.

Source
request(method : String, path : String, params : Hash(String, String) = {} of String => String, expect : Enumerable(Int32) = {200}, body : String | Nil = nil) : HTTP::Client::Response

Issues an arbitrary API call with retries and error mapping applied.

Source
retry_policy
Source
save_public_to_disk(public_key : String, path : String | Nil = nil, name : String | Nil = nil) : OperationHandle | Nil

Copies a published resource into the account's Downloads folder.

Source
set_custom_properties(path : String, properties : Hash(String, JSON::Any | Nil)) : Resource

Attaches arbitrary key/value metadata to a resource.

Values merge with whatever is already there; pass nil to drop a key. The API caps the whole object at 1 KB including keys and punctuation.

client.set_custom_properties("disk:/dump.sql", {
  "source" => JSON::Any.new("db-01"),
  "stale"  => nil,
})
Source
trash_delete(path : String) : OperationHandle | Nil

Permanently removes one entry from the Trash.

Source
trash_list(path : String = "/", sort : String | Nil = nil) : Array(Resource)

Everything inside a Trash directory, following pagination.

Source
trash_metadata(path : String = "/", fields : String | Nil = nil, limit : Int32 = 0, offset : Int32 = 0, sort : String | Nil = nil) : Resource

Metadata for an entry in the Trash. Paths here live in the trash:/ namespace: a deleted disk:/a/b.txt becomes trash:/b.txt.

Source
trash_restore(path : String, name : String | Nil = nil, overwrite : Bool = false, force_async : Bool = false) : OperationHandle | Nil

Restores a trashed resource to where it came from, recreating the original folder if it is gone. name renames it on the way back.

Source
unpublish(path : String) : Nil

Revokes public access, deactivating any link already handed out.

Source
upload(local : Path | String, remote : String, overwrite : Bool = true, chunk_size : Int64 = Uploader::DEFAULT_CHUNK_SIZE, & : Int64, Int64 -> ) : Int64

Uploads a local file, chunking and retrying as needed.

This is the everyday entry point; construct an Uploader directly only when you want to reuse one across many files with a fixed chunk size.

client.upload("dump.sql.gz", "disk:/backups/dump.sql.gz") do |sent, total|
  print "\r#{sent * 100 // total}%"
end
Source
upload(local : Path | String, remote : String, overwrite : Bool = true, chunk_size : Int64 = Uploader::DEFAULT_CHUNK_SIZE) : Int64

Uploads a local file, chunking and retrying as needed.

This is the everyday entry point; construct an Uploader directly only when you want to reuse one across many files with a fixed chunk size.

client.upload("dump.sql.gz", "disk:/backups/dump.sql.gz") do |sent, total|
  print "\r#{sent * 100 // total}%"
end
Source
upload_from_url(url : String, path : String, disable_redirects : Bool = false) : OperationHandle

Asks Yandex to fetch url itself and store it at path.

Always asynchronous — the returned handle is the only way to learn whether the download succeeded.

Source
wait(handle : OperationHandle, timeout : Time::Span = 5.minutes, interval : Time::Span = 1.second) : Operation

Polls handle until it settles, and returns the final state.

Raises OperationFailedError if the API reports failure and OperationTimeoutError if it is still running when timeout elapses — a still-running operation is not the same as a finished one, and silently returning "in-progress" would invite exactly that confusion.

Source