Documentation ¶
Overview ¶
Package ucare provides the binding for the Uploadcare API.
import ( "github.com/uploadcare/uploadcare-go/ucare" "github.com/uploadcare/uploadcare-go/file" "github.com/uploadcare/uploadcare-go/group" "github.com/uploadcare/uploadcare-go/upload" "github.com/uploadcare/uploadcare-go/conversion" )
Construct a new Uploadcare client, then use the various domain services to access different parts of the Uploadcare API:
creds := ucare.APICreds{ SecretKey: "your_secret_key", PublicKey: "your_public_key", } conf := &ucare.Config{ SignBasedAuthentication: true, } client, err := ucare.NewClient(creds, conf) if err != nil { // handle error }
Getting a list of files:
// creating a file operations service fileSvc := file.NewService(client) listParams := file.ListParams{ Stored: ucare.String(true), OrderBy: ucare.String(file.OrderBySizeAsc), } fileList, err := fileSvc.List(context.Background(), listParams) if err != nil { // handle error } // getting IDs for the first 100 files ids := make([]string, 0, 100) for fileList.Next() { finfo, err := fileList.ReadResult() if err != nil { // handle error } ids = append(ids, finfo.ID) }
Acquiring file-specific info:
fileID := ids[0] file, err := fileSvc.Info(context.Background(), fileID) if err != nil { // handle error } if file.IsImage { h := file.ImageInfo.Height w := file.ImageInfo.Width fmt.Printf("image size: %dx%d\n", h, w) }
Deleting a file
_, err := fileSvc.Delete(context.Background(), fileID) if err != nil { // handle error }
Storing a single file by ID:
_, err := fileSvc.Store(context.Background(), fileID) if err != nil { // handle error }
Getting a list of groups:
groupSvc := group.New(client) listParams := file.ListParams{ OrderBy: ucare.String(group.OrderByCreatedAtAsc), Limit: ucare.String(20), } groupList, err := groupSvc.List(context.Backgroud(), listParams) if err != nil { // handle error } // getting group IDs groupIDs = make([]string, 0, 100) for groupList.Next() { groupList, err := groupList.ReadResult() if err != nil { // handle error } groupIDs = append(groupIDs, groupList.ID) }
Getting a file group by ID:
groupID := groupIDs[0] group, err := groupSvc.Info(context.Background(), groupID) if err != nil { // handle error } fmt.Printf("group %s contains %d files\n", group.ID, group.FileCount)
Marking all files in a group as stored:
_, err := groupSvc.Store(context.Background(), groupID) if err != nil { // handle error }
Uploading a file
uploadSvc := upload.NewService(client) file, err := os.Open("somefile.png") if err != nil { // handle error } fileParams := upload.FileParams{ Data: file, Name: file.Name(), ToStore: ucare.String(upload.ToStoreTrue), } fileID, err := uploadSvc.File(context.Background(), fileParams) if err != nil { // handle error }
Index ¶
- Constants
- Variables
- func Bool(v bool) *bool
- func BoolVal(v *bool) bool
- func DisableLog()
- func EnableLog(lvl uclog.Level)
- func Int64(v int64) *int64
- func Int64Val(v *int64) int64
- func String(v string) *string
- func StringVal(v *string) string
- func Time(t time.Time) *time.Time
- func Uint64(v uint64) *uint64
- func Uint64Val(v *uint64) uint64
- type APICreds
- type Client
- type Config
- type ReqEncoder
- type UploadAPIAuthFunc
Constants ¶
const ( APIv05 = "v0.5" APIv06 = "v0.6" )
Public configuration constants
Variables ¶
var ( ErrInvalidAuthCreds = errors.New("Incorrect authentication credentials") ErrAuthForbidden = errors.New("Simple authentication over HTTP is " + "forbidden. Please, use HTTPS or signed requests instead") ErrInvalidVersion = errors.New("This feature is not support. " + "Try to change the version (refer to " + "https://uploadcare.com/api-refs/rest-api/v0.6.0/ for " + "more information on what methods belongs to what version).") ErrFileTooLarge = errors.New("Direct uploads only support " + "files smaller than 100MB") )
API response errors
Functions ¶
func BoolVal ¶
BoolVal returns the value of the bool pointer passed in or false if the pointer is nil.
func StringVal ¶
StringVal returns the value of the string pointer passed in or "" if the pointer is nil.
Types ¶
type APICreds ¶
APICreds holds per project API credentials. You can find your credentials on the uploadcare dashboard.
type Client ¶
type Client interface { NewRequest( ctx context.Context, endpoint config.Endpoint, method string, requrl string, data ReqEncoder, ) (*http.Request, error) Do(req *http.Request, resdata interface{}) error }
Client describes API client behaviour
type Config ¶
type Config struct { // HTTPClient allowes you to set custom http client for the calls HTTPClient *http.Client // APIVersion specifies REST API version to be used APIVersion string // SignBasedAuthentication should be true if you want to use // signed uploads and signature based authentication for the // REST API calls. SignBasedAuthentication bool }
Config holds configuration for the client
type ReqEncoder ¶
ReqEncoder exists to encode data into the prepared request. It may encode part of the data to the query string and other part into the request body. It may also set request headers for some payload types (multipart/form-data).
type UploadAPIAuthFunc ¶
UploadAPIAuthFunc is for internal use and should not be used by users