Cloudinary
Cloudinary
is a cloud-based image and video management services. It enables users to upload, store, manage, manipulate, and deliver images and video for websites and apps.
Reference: Offical Cloudinary Website
Prepare
- Login to cloudinary
-
Click
Media Library
-
Create a folder that will be used to store files
-
Go to
Settings
โUpload
-
Scroll down to
Upload presets
, ClickAdd upload preset
โ fill in the form and clicksave
Server side (backend)
-
Install cloudinary
go get github.com/cloudinary/cloudinary-go/v2
-
On
upload_file.go
file delete splituploads/
code and changedata
variable toctx
(on parameter 3)File:
pkg/middleware/upload_file.go
data := tempFile.Name() c.Set("dataFile", data) return next(c)
-
On handler
product.go
fileFile:
handlers/product.go
-
Import pakcage
"context" "github.com/cloudinary/cloudinary-go/v2" "github.com/cloudinary/cloudinary-go/v2/api/uploader"
-
On
CreateProduct
method, declarecontext background
,CLOUD_NAME
,API_KEY
,API_SECRET
var ctx = context.Background() var CLOUD_NAME = os.Getenv("CLOUD_NAME") var API_KEY = os.Getenv("API_KEY") var API_SECRET = os.Getenv("API_SECRET")
-
On
CreateProduct
method, Add Cloudinary credentials and Upload file to your Cloudinary folder// Add your Cloudinary credentials ... cld, _ := cloudinary.NewFromParams(CLOUD_NAME, API_KEY, API_SECRET) // Upload file to Cloudinary ... resp, err := cld.Upload.Upload(ctx, filepath, uploader.UploadParams{Folder: "dumbmerch"}); if err != nil { fmt.Println(err.Error()) }
-
On
CreateProduct
method, modify store file URL todatabase
fromresp.SecureURL
product := models.Product{ Name: request.Name, Desc: request.Desc, Price: request.Price, Image: resp.SecureURL, // Modify store file URL to database from resp.SecureURL ... Qty: request.Qty, UserID: userId, Category: category, }
-
-
Make sure modify this below code:
File:
handlers/product.go
-
On
FindProducts
method,delete
pathfile manipulationfor i, p := range products { imagePath := os.Getenv("PATH_FILE") + p.Image products[i].Image = imagePath }
-
On
GetProduct
method,delete
pathfile manipulationproduct.Image = os.Getenv("PATH_FILE") + product.Image
-
-
Add
CLOUD_NAME
,API_KEY
,API_SECRET
variable and the values to.env
File:
.env
CLOUD_NAME=your_cloud_name_here... API_KEY=your_api_key_here... API_SECRET=your_api_secret_here...