Go SDK

Overview

Cloudinary's Go SDK provides simple, yet comprehensive media upload and management capabilities that you can implement using code that integrates seamlessly with your existing Go application.

Related topics
This guide relates to the latest released version of the Cloudinary Go library.

  • This version of the SDK supports Upload and Admin API capabilities but does not yet support generating delivery URLs.
  • For details on all new features and fixes from previous versions, see the CHANGELOG.

Quickstart

1. Install library

Copy to clipboard
go get github.com/cloudinary/cloudinary-go

2. Import packages

Copy to clipboard
// Import the required packages for upload and admin.

import (
    "context"
    "github.com/cloudinary/cloudinary-go"
    "github.com/cloudinary/cloudinary-go/api/admin"
    "github.com/cloudinary/cloudinary-go/api/uploader"
    "log"
)

3. Add configuration

Copy to clipboard
// Add your Cloudinary credentials.

cld, _ := cloudinary.NewFromParams("<your-cloud-name>", "<your-api-key>", "<your-api-secret>")

Learn more: Account configuration

4. Upload image

Copy to clipboard
// Upload the my_picture.jpg image and set the public_id to "my_image". 

resp, err := cld.Upload.Upload(ctx, "my_picture.jpg", uploader.UploadParams{PublicID: "my_image"});

Learn more: Image and video upload

5. Get image details

Copy to clipboard
// Get details about the image with public id "my_image" and log the secure URL.

resp, err := cld.Admin.Asset(ctx, admin.AssetParams{PublicID: "my_image"});
if err != nil {...}
log.Println(resp.SecureURL)

Learn more: Asset management and administration

Installation

To install the Cloudinary Go SDK, use the go get command:

Copy to clipboard
go get github.com/cloudinary/cloudinary-go

Update

To update the Cloudinary Go SDK to the latest version, use the go get command with the -u option:

Copy to clipboard
go get -u github.com/cloudinary/cloudinary-go

Configuration

For requests to our secure APIs (e.g., image uploads, asset administration) you must have the APIKey and APISecret parameters set. You can find your account-specific configuration credentials in the Dashboard page of the account console.

Setting your CloudName, APIKey and APISecret parameters can be done by initializing the Cloudinary object, or by using the CLOUDINARY_URL environment variable / system property.

The entry point of the library is the Cloudinary struct.

Copy to clipboard
cld, _ := cloudinary.New()

Here's an example of setting the configuration parameters programatically:

Copy to clipboard
cld, _ := cloudinary.NewFromParams("n07t21i7", "123456789012345", "abcdeghijklmnopqrstuvwxyz12")

You can also set the configuration parameters by providing the CLOUDINARY_URL value:

Copy to clipboard
cld, _ := cloudinary.NewFromURL("cloudinary://123456789012345:abcdeghijklmnopqrstuvwxyz12@n07t21i7")

Complete SDK Example

Copy to clipboard
package main

import (
    "context"
    "github.com/cloudinary/cloudinary-go"
    "github.com/cloudinary/cloudinary-go/api/admin"
    "github.com/cloudinary/cloudinary-go/api/admin/search"
    "github.com/cloudinary/cloudinary-go/api/uploader"
    "log"
)

func main() {
    // Start by creating a new instance of Cloudinary using CLOUDINARY_URL environment variable.
    // Alternatively you can use cloudinary.NewFromParams() or cloudinary.NewFromURL().
    var cld, err = cloudinary.New()
    if err != nil {
        log.Fatalf("Failed to intialize Cloudinary, %v", err)
    }

    var ctx = context.Background()

    // Upload an image to your Cloudinary account from a specified URL.
    //
    // Alternatively you can provide a path to a local file on your filesystem,
    // base64 encoded string, io.Reader and more.
    //
    // For additional information see:
    // https://cloudinary.com/documentation/upload_images
    //
    // Upload can be greatly customized by specifying uploader.UploadParams,
    // in this case we set the Public ID of the uploaded asset to "logo".
    uploadResult, err := cld.Upload.Upload(
        ctx,
        "https://cloudinary-res.cloudinary.com/image/upload/cloudinary_logo.png",
        uploader.UploadParams{PublicID: "logo"})
    if err != nil {
        log.Fatalf("Failed to upload file, %v\n", err)
    }

    log.Println(uploadResult.SecureURL)
    // Prints something like:
    // https://res.cloudinary.com/<your cloud name>/image/upload/v1615875158/logo.png

    // uploadResult contains useful information about the asset, like Width, Height, Format, etc.
    // See uploader.UploadResult struct for more details.

    // Now we can use Admin API to see the details about the asset.
    // The request can be customised by providing AssetParams.
    asset, err := cld.Admin.Asset(ctx, admin.AssetParams{PublicID: "logo"})
    if err != nil {
        log.Fatalf("Failed to get asset details, %v\n", err)
    }

    // Print some basic information about the asset.
    log.Printf("Public ID: %v, URL: %v\n", asset.PublicID, asset.SecureURL)

    // Cloudinary also provides a very flexible Search API for filtering and retrieving
    // information on all the assets in your account with the help of query expressions
    // in a Lucene-like query language.
    searchQuery := search.Query{
        Expression: "resource_type:image AND uploaded_at>1d AND bytes<1m",
        SortBy:     []search.SortByField{{"created_at": search.Descending}},
        MaxResults: 30,
    }

    searchResult, err := cld.Admin.Search(ctx, searchQuery)

    if err != nil {
        log.Fatalf("Failed to search for assets, %v\n", err)
    }

    log.Printf("Assets found: %v\n", searchResult.TotalCount)

    for _, asset := range searchResult.Assets {
        log.Printf("Public ID: %v, URL: %v\n", asset.PublicID, asset.SecureURL)
    }
}

Related topics

✔️ Feedback sent!

Rate this page: