yadisk

package module
v0.0.0-...-4927738 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2025 License: MIT Imports: 11 Imported by: 0

README

go-yadisk

go-yadisk is a Go SDK for interacting with Yandex.Disk using its REST API.
It provides a simple interface for managing files and folders, uploading, downloading, and retrieving disk information.

Installation

To install go-yadisk, use the following command:

go get github.com/A-ndrey/go-yadisk

Usage

Here’s a basic example of using go-yadisk to retrieve disk information:

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/A-ndrey/go-yadisk"
)

func main() {
    // Create a new client with your OAuth token
    client := yadisk.NewClient("YOUR_OAUTH_TOKEN", yadisk.DefaultHost)

    // Get disk information
    diskInfo, err := client.DiskMetaInfo(context.Background(), yadisk.DiskMetaInfoParams{})()
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Used space: %d\n", diskInfo.UsedSpace)
    fmt.Printf("Total space: %d\n", diskInfo.TotalSpace)
}

Features

go-yadisk supports the following features:

  • Retrieve disk information.
  • Upload files to Yandex.Disk.
  • Download files from Yandex.Disk.
  • Delete files and folders.
  • List files and folders.
  • Work with public resources.

Authorization

To use go-yadisk, you need an OAuth token.
You can obtain it by creating an application in Yandex.OAuth.

Documentation

Detailed API documentation is available on the Yandex.Disk REST API official site.

License

This project is licensed under the MIT License.
See the LICENSE file for details.

Contributing

Contributions are welcome!
Feel free to open issues or submit pull requests.


Note: This project is not an official SDK by Yandex and is not affiliated with or supported by Yandex.

Documentation

Index

Constants

View Source
const DefaultHost = "cloud-api.yandex.net"

Variables

This section is empty.

Functions

This section is empty.

Types

type CleanTrashParams

type CleanTrashParams struct {
	Fields []string `param:"fields"`
	Async  bool     `param:"force_async"`
	Path   string   `param:"path"`
}

type Client

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

func NewClient

func NewClient(token string, host string, opts ...Opt) *Client

func (*Client) CleanTrash

func (c *Client) CleanTrash(ctx context.Context, params CleanTrashParams) (*Link, error)

func (*Client) CopyResource

func (c *Client) CopyResource(ctx context.Context, params CopyResourceParams) (*Link, error)

func (*Client) CreateDirectory

func (c *Client) CreateDirectory(ctx context.Context, params CreateDirectoryParams) (*Link, error)

func (*Client) DeleteResource

func (c *Client) DeleteResource(ctx context.Context, params DeleteResourceParams) (*Link, error)

func (*Client) DiskMetaInfo

func (c *Client) DiskMetaInfo(ctx context.Context, params DiskMetaInfoParams) (*Disk, error)

func (*Client) Download

func (c *Client) Download(ctx context.Context, downloadURL string, w io.Writer) error
func (c *Client) DownloadFileLink(ctx context.Context, params DownloadFileLinkParams) (*Link, error)
func (c *Client) DownloadPublicResourceLink(ctx context.Context, params DownloadPublicResourceLinkParams) (*Link, error)

func (*Client) ListFiles

func (c *Client) ListFiles(ctx context.Context, params ListFilesParams) (*FileResourceList, error)

func (*Client) ListFilesLastUploaded

func (c *Client) ListFilesLastUploaded(ctx context.Context, params ListFilesLastUploadedParams) (*LastUploadedResourceList, error)

func (*Client) ListPublicResources

func (c *Client) ListPublicResources(ctx context.Context, params ListPublicResourcesParams) (*PublicResourceList, error)

func (*Client) MoveResource

func (c *Client) MoveResource(ctx context.Context, params MoveResourceParams) (*Link, error)

func (*Client) OperationStatus

func (c *Client) OperationStatus(ctx context.Context, operationID string) (*Operation, error)

func (*Client) PublicResourceMetaInfo

func (c *Client) PublicResourceMetaInfo(ctx context.Context, params PublicResourceMetaInfoParams) (*PublicResource, error)

func (*Client) PublishResource

func (c *Client) PublishResource(ctx context.Context, params PublishResourceParams) (*Link, error)

func (*Client) ResourceMetaInfo

func (c *Client) ResourceMetaInfo(ctx context.Context, params ResourceMetaInfoParams) (*Resource, error)

func (*Client) RestoreResource

func (c *Client) RestoreResource(ctx context.Context, params RestoreResourceParams) (*Link, error)

func (*Client) SaveToDisk

func (c *Client) SaveToDisk(ctx context.Context, params SaveToDiskParams) (*Link, error)

func (*Client) TrashContent

func (c *Client) TrashContent(ctx context.Context, params TrashContentParams) (*TrashResource, error)

func (*Client) UnpublishResource

func (c *Client) UnpublishResource(ctx context.Context, params UnpublishResourceParams) (*Link, error)

func (*Client) UpdateResourceUserData

func (c *Client) UpdateResourceUserData(ctx context.Context, params UpdateResourceUserDataParams) (*Resource, error)

func (*Client) Upload

func (c *Client) Upload(ctx context.Context, uploadURL string, r io.Reader) error

func (*Client) UploadFileByURL

func (c *Client) UploadFileByURL(ctx context.Context, params UploadFileByURLParams) (*Link, error)
func (c *Client) UploadFileLink(ctx context.Context, params UploadFileLinkParams) (*ResourceUploadLink, error)

type CommentIDs

type CommentIDs struct {
	PrivateResource string `json:"private_resource,omitempty"`
	PublicResource  string `json:"public_resource,omitempty"`
}

type CopyResourceParams

type CopyResourceParams struct {
	FromPath  string   `param:"from,required"`
	ToPath    string   `param:"path,required"`
	Fields    []string `param:"fields"`
	Async     bool     `param:"force_async"`
	Overwrite bool     `param:"overwrite"`
}

type CreateDirectoryParams

type CreateDirectoryParams struct {
	Path   string   `param:"path,required"`
	Fields []string `param:"fields"`
}

type DeleteResourceParams

type DeleteResourceParams struct {
	Path        string   `param:"path,required"`
	Fields      []string `param:"fields"`
	Async       bool     `param:"force_async"`
	MD5         string   `param:"md5"`
	Permanently bool     `param:"permanently"`
}

type Disk

type Disk struct {
	PaidMaxFileSize            int            `json:"paid_max_file_size,omitempty"`
	MaxFileSize                int            `json:"max_file_size,omitempty"`
	TotalSpace                 int            `json:"total_space,omitempty"`
	TrashSize                  int            `json:"trash_size,omitempty"`
	UsedSpace                  int            `json:"used_space,omitempty"`
	IsPaid                     bool           `json:"is_paid,omitempty"`
	RegTime                    *time.Time     `json:"reg_time,omitempty"`
	SystemFolders              *SystemFolders `json:"system_folders,omitempty"`
	User                       *User          `json:"user,omitempty"`
	UnlimitedAutouploadEnabled bool           `json:"unlimited_autoupload_enabled,omitempty"`
	Revision                   int            `json:"revision,omitempty"`
}

type DiskMetaInfoParams

type DiskMetaInfoParams struct {
	Fields []string `param:"fields"`
}

type DownloadFileLinkParams

type DownloadFileLinkParams struct {
	Path   string   `param:"path,required"`
	Fields []string `param:"fields"`
}

type DownloadPublicResourceLinkParams

type DownloadPublicResourceLinkParams struct {
	PublicKey string   `param:"public_key,required"`
	Fields    []string `param:"fields"`
	Path      string   `param:"path"`
}

type EmbeddedPublicResourceList

type EmbeddedPublicResourceList struct {
	Sort      string           `json:"sort,omitempty"`
	PublicKey string           `json:"public_key,omitempty"`
	Items     []PublicResource `json:"items,omitempty"`
	Limit     int              `json:"limit,omitempty"`
	Offset    int              `json:"offset,omitempty"`
	Path      string           `json:"path,omitempty"`
	Total     int              `json:"total,omitempty"`
}

type Error

type Error struct {
	Code        int
	Message     string `json:"message,omitempty"`
	Description string `json:"description,omitempty"`
	Err         string `json:"error,omitempty"`
}

func (Error) Error

func (e Error) Error() string

type Exif

type Exif struct {
	DateTime     *time.Time `json:"date_time,omitempty"`
	GpsLongitude float64    `json:"gps_longitude,omitempty"`
	GpsLatitude  float64    `json:"gps_latitude,omitempty"`
}

type FileResourceList

type FileResourceList struct {
	Items  []Resource `json:"items,omitempty"`
	Limit  int        `json:"limit,omitempty"`
	Offset int        `json:"offset,omitempty"`
}

type LastUploadedResourceList

type LastUploadedResourceList struct {
	Items []Resource `json:"items,omitempty"`
	Limit int        `json:"limit,omitempty"`
}
type Link struct {
	URL       string `json:"href,omitempty"`
	Method    string `json:"method,omitempty"`
	Templated bool   `json:"templated,omitempty"`
}

type ListFilesLastUploadedParams

type ListFilesLastUploadedParams struct {
	Fields      []string `param:"fields"`
	Limit       int      `param:"limit"`
	MediaType   string   `param:"media_type"`
	PreviewCrop bool     `param:"preview_crop"`
	PreviewSize string   `param:"preview_size"`
	Sort        string   `param:"sort"`
}

type ListFilesParams

type ListFilesParams struct {
	Fields      []string `param:"fields"`
	Limit       int      `param:"limit"`
	Offset      int      `param:"offset"`
	MediaType   string   `param:"media_type"`
	PreviewCrop bool     `param:"preview_crop"`
	PreviewSize string   `param:"preview_size"`
	Sort        string   `param:"sort"`
}

type ListPublicResourcesParams

type ListPublicResourcesParams struct {
	Fields       []string `param:"fields"`
	Limit        int      `param:"limit"`
	Offset       int      `param:"offset"`
	PreviewCrop  bool     `param:"preview_crop"`
	PreviewSize  string   `param:"preview_size"`
	ResourceType string   `param:"type"`
}

type MoveResourceParams

type MoveResourceParams struct {
	FromPath  string   `param:"from,required"`
	ToPath    string   `param:"path,required"`
	Fields    []string `param:"fields"`
	Async     bool     `param:"force_async"`
	Overwrite bool     `param:"overwrite"`
}

type Operation

type Operation struct {
	Status Status `json:"status,omitempty"`
}

type Opt

type Opt func(*Client)

func WithHttpClient

func WithHttpClient(httpClient *http.Client) Opt

type Preview

type Preview struct {
	URL  string `json:"url,omitempty"`
	Name string `json:"name,omitempty"`
}

type PublicResource

type PublicResource struct {
	AntivirusStatus string                      `json:"antivirus_status,omitempty"`
	ViewsCount      int                         `json:"views_count,omitempty"`
	ResourceID      string                      `json:"resource_id,omitempty"`
	Share           *ShareInfo                  `json:"share,omitempty"`
	File            string                      `json:"file,omitempty"`
	Owner           *UserPublicInformation      `json:"owner,omitempty"`
	Size            int                         `json:"size,omitempty"`
	PhotosliceTime  *time.Time                  `json:"photoslice_time,omitempty"`
	Embedded        *EmbeddedPublicResourceList `json:"_embedded,omitempty"`
	Exif            *Exif                       `json:"exif,omitempty"`
	MediaType       string                      `json:"media_type,omitempty"`
	Preview         string                      `json:"preview,omitempty"`
	Type            string                      `json:"type,omitempty"`
	MimeType        string                      `json:"mime_type,omitempty"`
	Revision        int                         `json:"revision,omitempty"`
	PublicURL       string                      `json:"public_url,omitempty"`
	Path            string                      `json:"path,omitempty"`
	MD5             string                      `json:"md5,omitempty"`
	PublicKey       string                      `json:"public_key,omitempty"`
	SHA256          string                      `json:"sha256,omitempty"`
	Name            string                      `json:"name,omitempty"`
	Created         *time.Time                  `json:"created,omitempty"`
	Sizes           []Preview                   `json:"sizes,omitempty"`
	Modified        *time.Time                  `json:"modified,omitempty"`
	CommentIDs      *CommentIDs                 `json:"comment_ids,omitempty"`
}

type PublicResourceList

type PublicResourceList struct {
	Items  []Resource `json:"items,omitempty"`
	Type   string     `json:"type,omitempty"`
	Limit  int        `json:"limit,omitempty"`
	Offset int        `json:"offset,omitempty"`
}

type PublicResourceMetaInfoParams

type PublicResourceMetaInfoParams struct {
	PublicKey   string   `param:"public_key,required"`
	Fields      []string `param:"fields"`
	Limit       int      `param:"limit"`
	Offset      int      `param:"offset"`
	Path        string   `param:"path"`
	PreviewCrop bool     `param:"preview_crop"`
	PreviewSize string   `param:"preview_size"`
	Sort        string   `param:"sort"`
}

type PublishResourceParams

type PublishResourceParams struct {
	Path   string   `param:"path,required"`
	Fields []string `param:"fields"`
}

type Resource

type Resource struct {
	AntivirusStatus  string         `json:"antivirus_status,omitempty"`
	ResourceID       string         `json:"resource_id,omitempty"`
	Share            *ShareInfo     `json:"share,omitempty"`
	File             string         `json:"file,omitempty"`
	Size             int            `json:"size,omitempty"`
	PhotosliceTime   *time.Time     `json:"photoslice_time,omitempty"`
	Embedded         *ResourceList  `json:"_embedded,omitempty"`
	Exif             *Exif          `json:"exif,omitempty"`
	CustomProperties map[string]any `json:"custom_properties,omitempty"`
	MediaType        string         `json:"media_type,omitempty"`
	Preview          string         `json:"preview,omitempty"`
	Type             string         `json:"type,omitempty"`
	MimeType         string         `json:"mime_type,omitempty"`
	Revision         int            `json:"revision,omitempty"`
	PublicURL        string         `json:"public_url,omitempty"`
	Path             string         `json:"path,omitempty"`
	MD5              string         `json:"md5,omitempty"`
	PublicKey        string         `json:"public_key,omitempty"`
	Sha256           string         `json:"sha256,omitempty"`
	Name             string         `json:"name,omitempty"`
	Created          *time.Time     `json:"created,omitempty"`
	Sizes            []Preview      `json:"sizes,omitempty"`
	Modified         *time.Time     `json:"modified,omitempty"`
	CommentIDs       *CommentIDs    `json:"comment_ids,omitempty"`
}

type ResourceList

type ResourceList struct {
	Sort   string     `json:"sort,omitempty"`
	Items  []Resource `json:"items,omitempty"`
	Limit  int        `json:"limit,omitempty"`
	Offset int        `json:"offset,omitempty"`
	Path   string     `json:"path,omitempty"`
	Total  int        `json:"total,omitempty"`
}

type ResourceMetaInfoParams

type ResourceMetaInfoParams struct {
	Path        string   `param:"path,required"`
	Fields      []string `param:"fields"`
	Limit       int      `param:"limit"`
	Offset      int      `param:"offset"`
	PreviewCrop bool     `param:"preview_crop"`
	PreviewSize string   `param:"preview_size"`
	Sort        string   `param:"sort"`
}
type ResourceUploadLink struct {
	OperationID string `json:"operation_id,omitempty"`
	URL         string `json:"href,omitempty"`
	Method      string `json:"method,omitempty"`
	Templated   bool   `json:"templated,omitempty"`
}

type RestoreResourceParams

type RestoreResourceParams struct {
	Path      string   `param:"path,required"`
	Fields    []string `param:"fields"`
	Async     bool     `param:"force_async"`
	Name      string   `param:"name"`
	Overwrite bool     `param:"overwrite"`
}

type SaveToDiskParams

type SaveToDiskParams struct {
	PublicKey string   `param:"public_key,required"`
	Fields    []string `param:"fields"`
	Async     bool     `param:"force_async"`
	Name      string   `param:"name"`
	Path      string   `param:"path"`
	SavePath  string   `param:"save_path"` // default 'Downloads'
}

type ShareInfo

type ShareInfo struct {
	IsRoot  bool   `json:"is_root,omitempty"`
	IsOwned bool   `json:"is_owned,omitempty"`
	Rights  string `json:"rights,omitempty"`
}

type Status

type Status string
const (
	OpStatusSuccess    Status = "success"
	OpStatusFailed     Status = "failed"
	OpStatusInProgress Status = "in_progress"
)

type SystemFolders

type SystemFolders struct {
	Odnoklassniki string `json:"odnoklassniki,omitempty"`
	Google        string `json:"google,omitempty"`
	Instagram     string `json:"instagram,omitempty"`
	Vkontakte     string `json:"vkontakte,omitempty"`
	Attach        string `json:"attach,omitempty"`
	Mailru        string `json:"mailru,omitempty"`
	Downloads     string `json:"downloads,omitempty"`
	Applications  string `json:"applications,omitempty"`
	Facebook      string `json:"facebook,omitempty"`
	Social        string `json:"social,omitempty"`
	Messenger     string `json:"messenger,omitempty"`
	Calendar      string `json:"calendar,omitempty"`
	Photostream   string `json:"photostream,omitempty"`
	Screenshots   string `json:"screenshots,omitempty"`
	Scans         string `json:"scans,omitempty"`
}

type TrashContentParams

type TrashContentParams struct {
	Path        string   `param:"path,required"`
	Fields      []string `param:"fields"`
	Limit       int      `param:"limit"`
	Offset      int      `param:"offset"`
	PreviewCrop bool     `param:"preview_crop"`
	PreviewSize string   `param:"preview_size"`
	Sort        string   `param:"sort"`
}

type TrashResource

type TrashResource struct {
	AntivirusStatus  string         `json:"antivirus_status,omitempty"`
	ResourceID       string         `json:"resource_id,omitempty"`
	Share            *ShareInfo     `json:"share,omitempty"`
	File             string         `json:"file,omitempty"`
	Size             int            `json:"size,omitempty"`
	PhotosliceTime   *time.Time     `json:"photoslice_time,omitempty"`
	Embedded         *ResourceList  `json:"_embedded,omitempty"`
	Exif             *Exif          `json:"exif,omitempty"`
	CustomProperties map[string]any `json:"custom_properties,omitempty"`
	OriginPath       string         `json:"origin_path,omitempty"`
	MediaType        string         `json:"media_type,omitempty"`
	Preview          string         `json:"preview,omitempty"`
	Type             string         `json:"type,omitempty"`
	MimeType         string         `json:"mime_type,omitempty"`
	Revision         int            `json:"revision,omitempty"`
	Deleted          *time.Time     `json:"deleted,omitempty"`
	PublicURL        string         `json:"public_url,omitempty"`
	Path             string         `json:"path,omitempty"`
	MD5              string         `json:"md5,omitempty"`
	PublicKey        string         `json:"public_key,omitempty"`
	Sha256           string         `json:"sha256,omitempty"`
	Name             string         `json:"name,omitempty"`
	Created          *time.Time     `json:"created,omitempty"`
	Sizes            []Preview      `json:"sizes,omitempty"`
	Modified         *time.Time     `json:"modified,omitempty"`
	CommentIDs       *CommentIDs    `json:"comment_ids,omitempty"`
}

type TrashResourceList

type TrashResourceList struct {
	Sort   string          `json:"sort,omitempty"`
	Items  []TrashResource `json:"items,omitempty"`
	Limit  int             `json:"limit,omitempty"`
	Offset int             `json:"offset,omitempty"`
	Path   string          `json:"path,omitempty"`
	Total  int             `json:"total,omitempty"`
}

type UnpublishResourceParams

type UnpublishResourceParams struct {
	Path   string   `param:"path,required"`
	Fields []string `param:"fields"`
}

type UpdateResourceUserDataParams

type UpdateResourceUserDataParams struct {
	Path   string   `param:"path,required"`
	Fields []string `param:"fields"`
	Body   struct {
		CustomProperties map[string]any `json:"custom_properties"`
	}
}

type UploadFileByURLParams

type UploadFileByURLParams struct {
	Path             string   `param:"path,required"`
	URL              string   `param:"url,required"`
	DisableRedirects bool     `param:"disable_redirects"`
	Fields           []string `param:"fields"`
}

type UploadFileLinkParams

type UploadFileLinkParams struct {
	Path      string   `param:"path,required"`
	Fields    []string `param:"fields"`
	Overwrite bool     `param:"overwrite"`
}

type User

type User struct {
	RegTime     *time.Time `json:"reg_time,omitempty"`
	DisplayName string     `json:"display_name,omitempty"`
	UID         string     `json:"uid,omitempty"`
	Country     string     `json:"country,omitempty"`
	IsChild     bool       `json:"is_child,omitempty"`
	Login       string     `json:"login,omitempty"`
}

type UserPublicInformation

type UserPublicInformation struct {
	Login       string `json:"login,omitempty"`
	DisplayName string `json:"display_name,omitempty"`
	UID         string `json:"uid,omitempty"`
}

Directories

Path Synopsis
cmd
ydcli command

Jump to

Keyboard shortcuts

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