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