yadisk

package module
v0.0.0-...-7d6fc1f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2019 License: MIT Imports: 9 Imported by: 0

README

Yandex.Disk Client

This is unofficial Yandex.Disk API client in Go.

Getting started

Installation

Import it to your project:

import "github.com/yurykabanov/go-yandex-disk"
Usage

Instantiate client:

client := yadisk.New(&http.Client{ /* ... */ })
// or
client := yadisk.NewFromAccessToken("YOUR-OAUTH-ACCESS-TOKEN")
// or
client := yadisk.NewFromConfigAndToken(yandexOauthConfig, oauthToken, context.TODO())

and use it:

# Upload
link, err := client.RequestUploadLink(context.TODO(), "/some-path/uploaded-file.txt", false)
status, err := client.Upload(context.TODO(), link, anyIoReader)

# Download
link, err := client.RequestDownloadLink(context.TODO(), "/some-path/existing-file.txt")
resp, err := client.Download(context.TODO(), link)
defer resp.Body.Close()
// resp.Body is io.Reader for requested file

# Actions
client.Copy(context.TODO(), "/some-path/source-file.txt", "/some-path/destination-file.txt", false)
client.Move(context.TODO(), "/some-path/source-file.txt", "/some-path/destination-file.txt", false)
client.Delete(context.TODO(), "/some-path/existing-file.txt", false)
client.Mkdir(context.TODO(), "/some-path/new-directory")

More detailed examples could be found in examples/ directory.

Supported methods

This client currently support the following methods:

  • Upload and download
  • Actions: copy, move, delete and create directory
  • Disk stats
  • File meta information actions (read/write)
  • Publishing resources and performing actions on them
  • Working with Trash

Running the tests

# Unit tests
go test . -v

# Integration tests
go test ./test/integration/ -v -tags=integration -access-token=YOUR-OAUTH-ACCESS-TOKEN

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApiError

type ApiError struct {
	// HTTP response code (not included in original json).
	StatusCode int `json:"-"`

	// Human readable message.
	Message string `json:"message"`

	// Detailed isError description to help the developer.
	Description string `json:"description"`

	// Error ID for programmatic processing.
	ErrorID string `json:"error"`
}

func (ApiError) Error

func (err ApiError) Error() string

type Client

type Client struct {
	// contains filtered or unexported fields
}

func New

func New(client *http.Client) *Client

func NewFromAccessToken

func NewFromAccessToken(accessToken string) *Client

func NewFromConfigAndToken

func NewFromConfigAndToken(config *oauth2.Config, token *oauth2.Token, ctx context.Context) *Client

func (*Client) Copy

func (c *Client) Copy(ctx context.Context, src, dst string, overwrite bool) (*Link, int, error)

Copy file or directory.

src - The path to the resource to copy. dst - The path to the copy of the resource that is being created. The name of the file can be up to 255 characters. The path can be up to 32760 characters long. permanently -- Whether to permanently the file. It is used if the resource is copied to a folder that already contains a resource with the same name.

Method returns Link to created resource, status code and error (if any).

NOTE: for files and empty directories status code is "201 Created" and for non-empty directories it is "202 Accepted" which means the operation has been started, but hasn't been finished yet. The application MUST track the status of operation by itself.

See: https://tech.yandex.com/disk/api/reference/copy-docpage/

func (*Client) CreateDirectory

func (c *Client) CreateDirectory(ctx context.Context, path string) (*Link, error)

Create directory.

path - The path to the folder being created. The maximum length of the folder name is 255 characters; the maximum length of the path is 32760 characters.

Method returns Link to created resource or error.

See: https://tech.yandex.com/disk/api/reference/create-folder-docpage/

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, path string, permanently bool) (*Link, int, error)

Delete file or directory.

path - The path to the resource to delete. permanently - The flag for permanent deletion. False means file will be moved into the Trash.

Method returns Link only if operation hasn't been completed yet. It also returns status code and error (if any).

NOTE: for files and empty directories status code is "204 No content" and for non-empty directories it is "202 Accepted" which means the operation has been started, but hasn't been finished yet. The application MUST track the status of operation by itself.

See: https://tech.yandex.com/disk/api/reference/delete-docpage/

func (*Client) Download

func (c *Client) Download(ctx context.Context, link *Link) (*http.Response, error)

Download file from given link.

link - Previously requested link.

NOTE: this method lacks proper documentation, possible errors are not described. It WILL NOT return an error on successful request with 4xx-5xx HTTP response codes. The application MUST check response code by itself.

See: https://tech.yandex.com/disk/api/reference/content-docpage/

func (*Client) GetDisk

func (c *Client) GetDisk(ctx context.Context) (*Disk, error)

Data about a user's Disk.

Method returns Disk stats or error.

See: https://tech.yandex.com/disk/api/reference/capacity-docpage/

func (*Client) Move

func (c *Client) Move(ctx context.Context, src, dst string, overwrite bool) (*Link, int, error)

Move file or directory.

src - The path to the resource to move. dst - The path to the new location of the resource. The name of the file can be up to 255 characters. The path can be up to 32760 characters long. permanently - Whether to permanently files. It is used if the resource is moved to a folder that already contains a resource with the same name.

Method returns Link to created resource, status code and error (if any).

NOTE: for files and empty directories status code is "201 Created" and for non-empty directories it is "202 Accepted" which means the operation has been started, but hasn't been finished yet. The application MUST track the status of operation by itself.

See: https://tech.yandex.com/disk/api/reference/move-docpage/

func (c *Client) RequestDownloadLink(ctx context.Context, path string) (*Link, error)

Request download URL for file with given path.

path - The path to the file to download.

Method returns a Link if it has succeeded.

See: https://tech.yandex.com/disk/api/reference/content-docpage/

func (c *Client) RequestUploadLink(ctx context.Context, path string, overwrite bool) (*Link, error)

Request upload URL to upload file to the given path.

path - The path where you want to upload the file. The name of the uploaded file can be up to 255 characters. The path can be up to 32760 characters long. permanently -- Whether to permanently the file. It is used if the file is uploaded to a folder that already contains a file with the same name.

Method returns a Link if it has succeeded. Note that link is accessible only for 30 minutes.

See: https://tech.yandex.com/disk/api/reference/upload-docpage/

func (*Client) Upload

func (c *Client) Upload(ctx context.Context, link *Link, r io.Reader) (int, error)

Upload file's content to the requested link.

link - Previously requested link. r - io.Reader of file contents.

Method returns HTTP status code (because there's no answer).

NOTE: though possible errors are described, there's no documentation on exact response bodies. Thus this method WILL NOT return an error on successful request with 4xx-5xx HTTP response codes. The application MUST check response code by itself.

See: https://tech.yandex.com/disk/api/reference/upload-docpage/

type Disk

type Disk struct {
	// The cumulative size of the files in the Trash, in bytes.
	TrashSize int64 `json:"trash_size"`

	// The total space available to the user on Yandex.Disk, in bytes.
	TotalSpace int64 `json:"total_space"`

	// The cumulative size of the files already stored on Yandex.Disk, in bytes.
	UsedSpace int64 `json:"used_space"`

	// Absolute addresses of Yandex.Disk system folders. Folder names depend on
	// the user interface language that was in use when the user's personal Disk
	// was created. For example, the Downloads folder is created for an English-
	// speaking user, Загрузки for a Russian-speaking user, and so on.
	//
	// The following folders are currently supported:
	// - applications - folder for application files
	// - downloads - folder for files downloaded from the internet (not from the
	//   user's device)
	SystemFolders map[string]string `json:"system_folders"`

	// Maximum file size.
	MaxFileSize int64 `json:"max_file_size"`

	// Indicated unlimited autoupload from mobile devices.
	UnlimitedAutouploadEnabled bool

	// Indicated presences of paid storage.
	IsPaid bool `json:"is_paid"`

	// Authenticated user.
	User User `json:"user"`

	// Ya.Disk revision.
	Revision int64 `json:"revision"`
}

Data about free and used space on Yandex.Disk

type FilesResourceList

type FilesResourceList struct {
	// Array of recently uploaded files (Resource).
	Items []Resource `json:"items"`

	// The maximum number of items in the items array; set in the request.
	Limit int64 `json:"limit"`

	// How much to offset the beginning of the list from the first resource in
	// the folder.
	Offset int64 `json:"offset"`
}

Flat list of all files on Yandex.Disk in alphabetical order.

type LastUploadedResourceList

type LastUploadedResourceList struct {
	// Array of recently uploaded files (Resource).
	Items []Resource `json:"items"`

	// The maximum number of items in the items array; set in the request.
	Limit int64 `json:"limit"`
}

A list of files recently added to Yandex.Disk, sorted by upload date (from later to earlier).

type Link struct {
	// URL. It may be a URL template; see the templated key.
	Href string `json:"href"`

	// The HTTP method for requesting the URL from the href key.
	Method string `json:"method"`

	// Indicates a URL template according to RFC 6570.
	Templated bool `json:"templated"`
}

The object contains the URL for requesting resource metadata.

type Operation

type Operation struct {
	Status OperationStatus `json:"status"`
}

The status of the operation. Operations are launched when you copy, move, or delete non-empty folders. The URL for requesting status is returned in response to these types of requests.

type OperationStatus

type OperationStatus string

The status of the operation.

const (
	// Operation completed successfully.
	OperationStatusSuccess OperationStatus = "success"

	// Operation failed; try repeating the initial request to copy, move or
	// delete.
	OperationStatusFailure OperationStatus = "failure"

	// Operation started but not yet completed.
	OperationStatusInProgress OperationStatus = "in-progress"
)

type PublicResourcesList

type PublicResourcesList struct {
	// Array of recently uploaded files (Resource).
	Items []Resource `json:"items"`

	// The maximum number of items in the items array; set in the request.
	Limit int64 `json:"limit"`

	// Resource type.
	Type ResourceType `json:"type"`

	// How much to offset the beginning of the list from the first resource in
	// the folder.
	Offset int64 `json:"offset"`
}

type Resource

type Resource struct {
	// Key of a published resource.
	// It is included in the response only if the specified file or folder is
	// published.
	PublicKey string `json:"public_key"`

	// Link to a published resource.
	// It is included in the response only if the specified file or folder is
	// published.
	PublicUrl string `json:"public_url"`

	// The resources located in the folder (contains the ResourceList object).
	// It is included in the response only when folder metainformation is
	// requested.
	Embedded ResourceList `json:"_embedded"`

	// Link to a small image (preview) for the file. It is included in the
	// response only for files that support graphic formats.
	// The preview can only be requested using the OAuth token of a user who has
	// access to the file itself.
	Preview string `json:"preview"`

	// Resource name.
	Name string `json:"name"`

	// An object with all attributes set with the Adding metainformation for
	// a resource request. Contains only keys in the name:value format (cannot
	// contain objects or arrays).
	CustomProperties map[string]string `json:"custom_properties"`

	// The date and time when the resource was created, in ISO 8601 format.
	Created time.Time `json:"created"`

	// 	The date and time when the resource was modified, in ISO 8601 format.
	Modified time.Time `json:"modified"`

	// Full path to the resource on Yandex.Disk.
	// In metainformation for a published folder, paths are relative to the
	// folder itself. For published files, the value of the key is always "/".
	// For a resource located in the Trash, this attribute may have a unique ID
	// appended to it (for example, trash:/foo_1408546879). Use this ID to
	// differentiate the resource from other deleted resources with the same
	// name.
	Path string `json:"path"`

	// Path to the resource before it was moved to the Trash.
	// Included in the response only for a request for metainformation about
	// a resource in the Trash.
	OriginPath string `json:"origin_path"`

	// MD5 hash of the file.
	Md5 string `json:"md5"`

	// Resource type.
	Type ResourceType `json:"type"`

	// The MIME type of the file.
	MimeType string `json:"mime_type"`

	// File size.
	Size int64 `json:"size"`
}

Resource description or metainformation about a file or folder. Included in the response to the request for metainformation.

type ResourceList

type ResourceList struct {
	// The field used for sorting the list.
	Sort string `json:"sort"`

	// The key of a published folder that contains resources from this list.
	// It is included in the response only if metainformation about a public
	// folder is requested.
	PublicKey string `json:"public_key"`

	// Array of resources (Resource) contained in the folder.
	// Regardless of the requested sorting, resources in the array are ordered
	// by type: first all the subfolders are listed, then all the files.
	Items []Resource `json:"items"`

	// The maximum number of items in the items array; set in the request.
	Limit int64 `json:"limit"`

	// How much to offset the beginning of the list from the first resource in
	// the folder.
	Offset int64 `json:"offset"`

	// The path to the folder whose contents are described in this ResourceList
	// object.
	// For a public folder, the value of the attribute is always "/".
	Path string `json:"path"`

	// The total number of resources in the folder.
	Total string `json:"total"`
}

The list of resources in the folder. Contains Resource objects and list properties.

type ResourceType

type ResourceType string

Resource type.

const (
	// Resource type directory.
	ResourceTypeDirectory ResourceType = "dir"

	// Resource type file.
	ResourceTypeFile ResourceType = "file"
)

type User

type User struct {
	// User's country.
	Country string `json:"country"`

	// User's login.
	Login string `json:"login"`

	// User's displayed name.
	DisplayName string `json:"display_name"`

	// User's ID
	UID string `json:"uid"`
}

Directories

Path Synopsis
examples
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL