uploadcare-go

module
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2023 License: MIT

README

Golang API client for Uploadcare

license GoDoc

Uploadcare Golang API client that handles uploads and further operations with files by wrapping Uploadcare Upload and REST APIs.

Requirements

go1.13

Installation

Install uploadcare-go with:

go get -u -v github.com/uploadcare/uploadcare-go/...

Then import it using:

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"
)

Configuration

Creating a client:

creds := ucare.APICreds{
	SecretKey: "your-project-secret-key",
	PublicKey: "your-project-public-key",
}

conf := &ucare.Config{
	SignBasedAuthentication: true,
	APIVersion:              ucare.APIv06,
}

client, err := ucare.NewClient(creds, conf)
if err != nil {
	log.Fatal("creating uploadcare API client: %s", err)
}

Usage

For a comprehensive list of examples, check out the API documentation. Below are a few usage examples:

Getting a list of files:

fileSvc := file.NewService(client)

listParams := file.ListParams{
	Stored:  ucare.Bool(true),
	OrderBy: ucare.String(file.OrderBySizeAsc),
}

fileList, err := fileSvc.List(context.Background(), listParams)
if err != nil {
	// handle error
}

// getting IDs of the 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)
}

Uploading a file:

f, err := os.Open("file.png")
if err != nil {
	// handle error
}

uploadSvc := upload.NewService(client)

params := upload.FileParams{
	Data:        f,
	Name:        f.Name(),
	ContentType: "image/png",
}
fID, err := uploadSvc.File(context.Background(), params)
if err != nil {
	// handle error
}

Golang API client documentation
Uploadcare documentation
Upload API reference
REST API reference
Changelog
Contributing guide
Security policy
Support

Directories

Path Synopsis
Package file holds all primitives and logic around the file resource.
Package file holds all primitives and logic around the file resource.
Package group holds all primitives and logic related file entity.
Package group holds all primitives and logic related file entity.
internal
codec
Package codec holds encoding decoding reading writing stuff
Package codec holds encoding decoding reading writing stuff
config
Package config holds all internal configurations
Package config holds all internal configurations
svc
Package svc holds (hides) all common service logic.
Package svc holds (hides) all common service logic.
Package project holds all primitives and logic around the project resource.
Package project holds all primitives and logic around the project resource.
Package ucare provides the binding for the Uploadcare API.
Package ucare provides the binding for the Uploadcare API.
Package uclog holds basic leveled logging primitives.
Package uclog holds basic leveled logging primitives.
Package upload contains all upload related API stuff.
Package upload contains all upload related API stuff.
Package webhook holds all primitives and logic around the webhook resource.
Package webhook holds all primitives and logic around the webhook resource.

Jump to

Keyboard shortcuts

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