Skip to content

Commit d130fc9

Browse files
authored
Merge pull request #85 from rahul1995/go-image-upload
Add Go image upload example
2 parents 1a9a9af + b379b1c commit d130fc9

10 files changed

Lines changed: 134 additions & 0 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CLOUDINARY_URL=cloudinary://<API_KEY>:<API_SECRET>@<CLOUD_NAME>

examples/go-image-upload/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Uploading Images to Cloudinary with the Go SDK
2+
3+
## 🧰 Uploading with the SDK in a Go project
4+
5+
To use the SDK in a Go project, first configure a new instance of Cloudinary:
6+
7+
```
8+
cld, err := cloudinary.New() // CLOUDINARY_URL environment variable should already be set
9+
OR
10+
cld, err := NewFromParams(cloud_name, api_key, api_secret)
11+
```
12+
13+
Then, customize upload behaviour by configuring UploadParams
14+
15+
```
16+
// can be left empty if default behaviour is needed
17+
uploadParams := uploader.UploadParams{
18+
UseFilename: api.Bool(true),
19+
UseFilenameAsDisplayName: api.Bool(true),
20+
UniqueFilename: api.Bool(false),
21+
}
22+
```
23+
24+
Finally, run the upload method, passing in your image source:
25+
26+
```
27+
filePath := "./path/to/image.jpg" // Can be a remote URL or base64 DataURI as well
28+
res, err := cld.Upload.Upload(context.Background(), filepath, uploadParams)
29+
```
30+
31+
## 🚀 Get Started with This Example
32+
33+
* Create an environment variable file `.env` and inside add:
34+
```
35+
CLOUDINARY_URL="cloudinary://<API_KEY>:<API_SECRET>@<CLOUD_NAME>"
36+
```
37+
38+
39+
* Run the Go script from project directory, i.e. inside the `examples/go-image-upload` directory:
40+
41+
```
42+
go run .
43+
```
44+
45+
The script will run in your terminal and you'll see the URL results logged.

examples/go-image-upload/go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module image_upload
2+
3+
go 1.21.2
4+
5+
require (
6+
github.com/cloudinary/cloudinary-go/v2 v2.5.1 // indirect
7+
github.com/creasty/defaults v1.5.1 // indirect
8+
github.com/google/uuid v1.2.0 // indirect
9+
github.com/gorilla/schema v1.2.0 // indirect
10+
github.com/joho/godotenv v1.5.1 // indirect
11+
)

examples/go-image-upload/go.sum

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
github.com/cloudinary/cloudinary-go/v2 v2.5.1 h1:RZKSfrmYHwXVTKAnjr2dibzpu7ox2QLtoSF/xVznLvM=
2+
github.com/cloudinary/cloudinary-go/v2 v2.5.1/go.mod h1:jtSxa6xbzvu4IwChRJVDcXwVXrTRczhbvq3Z1VSoFdk=
3+
github.com/creasty/defaults v1.5.1 h1:j8WexcS3d/t4ZmllX4GEkl4wIB/trOr035ajcLHCISM=
4+
github.com/creasty/defaults v1.5.1/go.mod h1:FPZ+Y0WNrbqOVw+c6av63eyHUAl6pMHZwqLPvXUZGfY=
5+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6+
github.com/go-test/deep v1.0.7/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8=
7+
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
8+
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
9+
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
10+
github.com/gorilla/schema v1.2.0 h1:YufUaxZYCKGFuAq3c96BOhjgd5nmXiOY9NGzF247Tsc=
11+
github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
12+
github.com/heimdalr/dag v1.0.1/go.mod h1:t+ZkR+sjKL4xhlE1B9rwpvwfo+x+2R0363efS+Oghns=
13+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
14+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
15+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
16+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
17+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
18+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
19+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
670 KB
Loading
5.26 MB
Loading
640 KB
Loading
757 KB
Loading
2.84 MB
Loading

examples/go-image-upload/main.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
8+
"github.com/cloudinary/cloudinary-go/v2"
9+
"github.com/cloudinary/cloudinary-go/v2/api"
10+
"github.com/cloudinary/cloudinary-go/v2/api/uploader"
11+
12+
"github.com/joho/godotenv"
13+
)
14+
15+
func main() {
16+
if err := godotenv.Load(); err != nil {
17+
fmt.Println("Error loading .env file")
18+
return
19+
}
20+
21+
cld, err := cloudinary.New()
22+
if err != nil {
23+
fmt.Println("Error in creating Cloudinary instance", err)
24+
return
25+
}
26+
27+
filePaths := []string{
28+
"./images/aldebaran-s-uXchDIKs4qI-unsplash.jpg",
29+
"./images/brian-mcgowan-I0fDR8xtApA-unsplash.jpg",
30+
"./images/spacex-OHOU-5UVIYQ-unsplash.jpg",
31+
"./images/spacex-VBNb52J8Trk-unsplash.jpg",
32+
"./images/stellan-johansson-1PP0Fc-KSd4-unsplash.jpg",
33+
"https://images.unsplash.com/photo-1451187863213-d1bcbaae3fa3?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=880&q=80",
34+
}
35+
36+
for _, filepath := range filePaths {
37+
uploadParams := uploader.UploadParams{
38+
UseFilename: api.Bool(true),
39+
UseFilenameAsDisplayName: api.Bool(true),
40+
UniqueFilename: api.Bool(false),
41+
}
42+
43+
res, err := cld.Upload.Upload(context.Background(), filepath, uploadParams)
44+
45+
if err != nil {
46+
fmt.Println("failed to upload file", err)
47+
continue
48+
}
49+
fmt.Println("Successfully uploaded", filepath)
50+
51+
jsonRes, err := json.MarshalIndent(res, "", " ")
52+
if err != nil {
53+
fmt.Println("Could not format result in JSON")
54+
fmt.Println("Upload result -", res)
55+
}
56+
fmt.Println("Upload result -", string(jsonRes))
57+
}
58+
}

0 commit comments

Comments
 (0)