kloudinary

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

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

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 15 Imported by: 0

README

badge

Kloudinary

Kloudinary is a Golang wrapper around the official Cloudinary SDK. It streamlines some redundant parts of using the official SDK and can get you up and running within minutes of installation.

Features

  • Simplifies file uploads to Cloudinary.
  • Supports multiple file uploads in one go.
  • Easily destroy assets by public ID.
  • Transform images with minimal code.
  • Configurable asset upload settings.

Installation

To install Kloudinary, run:

go get github.com/caleberi/kloudinary

Usage

Initialization

First, you need to initialize the AssetUploadManager with your Cloudinary credentials:

package main

import (
	"github.com/caleberi/kloudinary"
	"log"
)

func main() {
	cloudName := "your-cloud-name"
	apiKey := "your-api-key"
	apiSecret := "your-api-secret"

	am, err := kloudinary.NewAssetUploadManager(cloudName, apiKey, apiSecret)
	if err != nil {
		log.Fatalf("Failed to initialize AssetUploadManager: %v", err)
	}

	// Use am to manage your assets
}
Uploading Files
  1. Upload a Single File To upload a single file, use the UploadSingleFile method:
result, err := am.UploadSingleFile(ctx, "path/to/your/file.jpg")
if err != nil {
    log.Fatalf("Failed to upload file: %v", err)
}
log.Printf("Upload successful: %v", result)
  1. Upload Multiple Files To upload multiple files at once, use the UploadMultipleFiles method:
results := am.UploadMultipleFiles(ctx, "path/to/file1.jpg", "path/to/file2.png")
for _, res := range results {
    log.Printf("Upload result: %v", res)
}
  1. Destroying Assets To destroy an asset by its public ID, use the DestroyAsset method:
destroyResult, err := am.DestroyAsset(ctx, "public_id_of_asset", "")
if err != nil {
    log.Fatalf("Failed to destroy asset: %v", err)
}
log.Printf("Destroy result: %v", destroyResult)
  1. Transforming Images To transform an image, use the TransformImage method:
url, err := am.TransformImage(ctx, "public_id_of_image", "transformation_string")
if err != nil {
    log.Fatalf("Failed to transform image: %v", err)
}
log.Printf("Transformed image URL: %s", url)

Configuration

The AssetUploadManager can be configured with various settings:

am.FileTypeSupported = []string{"jpg", "png", "gif"}
am.MaxAssetSize = 10 * 1024 * 1024 // 10 MB
am.MaxUploadTimeout = 30 * time.Second
am.MaxNumberOfConcurrentUploads = 5

License

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

Contributing

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

Acknowledgments

  • The official Cloudinary SDK

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractFromMap

func ExtractFromMap[K, V comparable](data, result map[K]V, fn func(value V) bool)

func Filter

func Filter[T comparable](data []T, fn func(v T) bool) []T

func ForEach

func ForEach[T any](data []T, fn func(v T))

func Map

func Map[T, V comparable](data []T, fn func(v T) V) []V

Types

type AssetUploadManager

type AssetUploadManager struct {
	FileTypeSupported            []string      // supported file types
	MaxAssetSize                 int64         // maximum size for an asset
	MaxUploadTimeout             time.Duration // maximum upload timeout for assets to be uploaded
	MaxNumberOfConcurrentUploads int64         // number of concurrent uploads
	Metadata                     Meta          // metadata about asset upload management for this instance
	// contains filtered or unexported fields
}

AssetUploadManager wraps around cloudinary upload package, increasing the number of uploads that can be performed concurrent and also ensures that files are uploaded to the correct location base on the mimetype of the file being uploaded

func NewAssetUploadManager

func NewAssetUploadManager(cloudName string, apiKey string, apiSecret string) (*AssetUploadManager, error)

NewAssetUploadManager New creates a new asset uploader which will upload files and other supported assets to cloudinary server. Inorder, for this to work, you need to configure AssetUploadManager with the following configuration `cloudName`, `apikey`,and `apiSecret` for more information visit: https://cloudinary.com/documentation/

func (*AssetUploadManager) DestroyAsset

func (am *AssetUploadManager) DestroyAsset(ctx context.Context, publicId string, transformation string) (*uploader.DestroyResult, error)

func (*AssetUploadManager) TransformImage

func (am *AssetUploadManager) TransformImage(ctx context.Context, publicId string, transformation string) (string, error)

TransformImage is used to transform image property of a single file on a cloudinary server

func (*AssetUploadManager) UploadMultipleFiles

func (am *AssetUploadManager) UploadMultipleFiles(ctx context.Context, files ...interface{}) []FileUploadResult

func (*AssetUploadManager) UploadSingleFile

func (am *AssetUploadManager) UploadSingleFile(ctx context.Context, file interface{}) (*uploader.UploadResult, error)

UploadSingleFile is used to upload a single file to the server the file can either be a byte slice or a string

type FileUploadResult

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

type Meta

type Meta map[string]interface{}

func (Meta) Add

func (m Meta) Add(key string, value interface{})

func (Meta) Remove

func (m Meta) Remove(key string)

Jump to

Keyboard shortcuts

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