cloudinary

package module
v0.0.0-...-53eda7d Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2020 License: MIT Imports: 13 Imported by: 1

README

cloudinary

GitHub go.mod Go version GoDoc

Cloudinary API wrapper in Go.

Installation

go get -u -v github.com/komfy/cloudinary

Example

package main

import (
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/komfy/cloudinary"
)

type downloadHandler struct {
	cs *cloudinary.Service
}
//Creating a handler to donwload from form.
//I prefer to use handlers, because it's easier to add some external services into it's logic.
func (h downloadHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
	if req.Method != http.MethodPost {
		http.Error(res, "Method must be POST", http.StatusMethodNotAllowed)
		return
	}
	// Parsing multipart form is not necessary, FormFile invokes it if form isn't parsed.
	file, fh, err := req.FormFile("file")
	upResp, err := h.cs.Upload(fh.Filename, file, false)
	if err != nil {
		http.Error(res, err.Error(), 505)
		return
	}
	url := upResp.SecureURL
	res.Write([]byte(url))
}

func main() {
	// Creates new Service from environmental variable
	cloudinaryURL := os.Getenv("CLOUDINARY_URL")
	if cloudinaryURL == "" {
		log.Fatalln("there is no env variable with given name")
	}
	s, err := cloudinary.NewService(cloudinaryURL)
	if err != nil {
		log.Fatalln(err)
	}
	// Sending local file.
	file, err := os.Open("example.jpg")
	if err != nil {
		log.Fatalln(err)
	}
	upResp, err := s.Upload(file.Name(), file, false)
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(upResp.URL)
	http.Handle("/download", downloadHandler{cs: s})
	http.ListenAndServe(":3000", nil)
}

Uploading mutiple form files


package main

import (
	"encoding/json"
	"fmt"
	"log"
	"mime/multipart"
	"net/http"
	"os"

	"github.com/komfy/cloudinary"
)

type downloadHandler struct {
	cs *cloudinary.Service
}

//UploadFiles func
func (h downloadHandler) UploadMultipleFiles(files []*multipart.FileHeader) []string {
	urls := []string{}
	for _, file := range files {
		fmt.Println(file.Filename)
		data, err := file.Open()
		if err == nil {
			upResp, err := h.cs.Upload(file.Filename, data, false)
			if err != nil {
				panic(err)
			} else {
				urls = append(urls, upResp.URL)
			}
		}
		data.Close()
	}
	return urls
}

func (h downloadHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
	if req.Method != http.MethodPost {
		http.Error(res, "Method must be POST", http.StatusMethodNotAllowed)
		return
	}

	req.ParseMultipartForm(32 << 20)
	files := req.MultipartForm.File["files"] //name of file input
	urls := h.UploadMultipleFiles(files)
	resBody, _ := json.Marshal(urls)
	res.Write(resBody)
}

func main() {
	// Creates new Service from environmental variable
	cloudinaryURL := os.Getenv("CLOUDINARY_URL")
	if cloudinaryURL == "" {
		log.Fatalln("there is no env variable with given name")
	}
	s, err := cloudinary.NewService(cloudinaryURL)
	if err != nil {
		log.Fatalln(err)
	}

	http.Handle("/download", downloadHandler{cs: s})
	http.ListenAndServe(":3000", nil)
}


License

MIT

Documentation

Overview

Package cloudinary provides an easy way of connection between go and cloudinary

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Service

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

Service represents cloudinary service

func NewService

func NewService(cloudinaryURL string) (*Service, error)

NewService inits a *Service from cloudinaryURL Link should be given in format "cloudinary://api_key:api_secret@cloud_name" After initialization returns ready to use service or an error in case of incorrect URL

func (*Service) Upload

func (s *Service) Upload(fileName string, fileBody io.Reader, randomPublicID bool) (*UploadResponse, error)

Upload uploads a file

type UploadResponse

type UploadResponse struct {
	PublicID     string    `json:"public_id"`
	Width        int       `json:"width"`
	Height       int       `json:"height"`
	Format       string    `json:"format"`
	ResourceType string    `json:"resource_type"`
	CreatedAt    time.Time `json:"created_at"`
	SecureURL    string    `json:"secure_url"`
	URL          string    `json:"url"`
}

UploadResponse ...

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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