Go image and video upload

Cloudinary provides an API for uploading images, videos, and any other kind of file to the cloud. Files uploaded to Cloudinary are stored safely in the cloud with secure backups and revision history. Cloudinary's APIs allow secure uploading from your servers, directly from your visitors' browsers or mobile applications, or fetched via remote public URLs.

Cloudinary's Go SDK wraps Cloudinary's upload API and simplifies the integration. Methods are available for easily performing image and video uploads to the cloud.

This page covers common usage patterns for Go image and video upload with Cloudinary.

For details on all available upload options and parameters, see the Media upload documentation, and the Upload method of the Upload API Reference.

Upload widget

Cloudinary's Upload widget is an interactive, feature rich, simple-to-integrate user interface that enables you to add Cloudinary upload support to your website. The widget can be easily embedded in your web application with just a few lines of JavaScript code, eliminating the need to develop in-house interactive upload capabilities. See the Upload widget documentation for detailed information.

Server-side upload

You can upload images, videos, or any other raw file to Cloudinary using Go code. Uploading is done over HTTPS using a secure protocol based on your account's api_key and api_secret parameters.

Go image upload

The following method uploads an image to the cloud:

Copy to clipboard
resp, err := cld.Upload.Upload(ctx, file, options);

For example, uploading a local image file named 'my_image.jpg':

Copy to clipboard
resp, err := cld.Upload.Upload(ctx, "my_image.jpg", uploader.UploadParams{});

The file to upload can be specified as a local path, a remote HTTP or HTTPS URL, a whitelisted storage bucket (S3 or Google Storage) URL, a base-64 data URI, or an FTP URL. For details, see File source options.

For details on all available upload options and parameters, see the Media upload documentation, and the Upload method of the Upload API Reference.

Go video upload

You upload videos in exactly the same way as images.

The following example uploads dog.mp4 to Cloudinary and stores it in a bi-level folder structure with the public ID dog_closeup. It also performs two eager transformations that resize the video to a square and a small rectangle.

Copy to clipboard
resp, err := cld.Upload.Upload(ctx, "dog.mp4", uploader.UploadParams{
  PublicID:  "my_folder/my_sub_folder/dog_closeup",
  Eager:  "w_300,h_300,c_pad,ac_none|w_160,h_100,c_crop,g_south,ac_none",
  EagerAsync: true,
  EagerNotificationURL: "https://mysite.example.com/notify_endpoint"
});

Upload response

By default, uploading is performed synchronously. Once finished, the uploaded image or video is immediately available for manipulation and delivery. An upload call returns a struct with content similar to the following:

Copy to clipboard
&{
  AssetID:aac8fd89108ff834dd27f979fd9ce77e 
  PublicID:hl22acprlomnycgiudor 
  Version:1591095352 
  VersionID:909700634231dbaaf8b06d7a5940299e
  Signature:86922996d63e596464ea3d7a5e86e8de8123f23f 
  Width:1200 
  Height:1200 
  Format:jpg 
  ResourceType:image 
  CreatedAt:2020-06-02 10:55:52 +0000 UTC 
  Tags:[] 
  Pages:0 
  Bytes:460268 
  Type:upload 
  Etag:2c7e88604ba3f340a0c5bc8cd418b4d9 
  Placeholder:false 
  URL:  http://res.cloudinary.com/demo/image/upload/v1591095352/hl22acprlomnycgiudor.jpg 
  SecureURL:  https://res.cloudinary.com/demo/image/upload/v1591095352/hl22acprlomnycgiudor.jpg 
  AccessMode:public 
  Context:map[] 
  Metadata:map[] 
  Overwritten:true 
  OriginalFilename:my_image 
  Error:{Message:}
}

The response includes HTTP and HTTPS URLs for accessing the uploaded media asset as well as additional information regarding the uploaded asset: The Public ID, resource type, width and height, file format, file size in bytes, a signature for verifying the response and more.

Data uploading options

Cloudinary's Go library supports uploading files from various sources.

  • You can upload an image by specifying a local path of an image file. For example:

    Copy to clipboard
    resp, err := cld.Upload.Upload(ctx,"/home/my_image.jpg",uploader.UploadParams{});
  • If your images are already publicly available online, you can specify their remote HTTP URLs instead of uploading the actual data. In this case, Cloudinary will fetch the image from its remote URL for you. This option allows for a much faster migration of your existing images. Here's an example:

    Copy to clipboard
    resp, err := cld.Upload.Upload(ctx,"https://www.example.com/image.jpg",uploader.UploadParams{});
  • If you have existing images in an Amazon S3 bucket, you can point Cloudinary to their S3 URLs. Note - this option requires a quick manual setup. Contact us and we'll guide you on how to allow Cloudinary access to your relevant S3 buckets.

    Copy to clipboard
    resp, err := cld.Upload.Upload(ctx,"https://www.example.com/image.jpg",uploader.UploadParams{});

Related topics

  • For more information on uploading media assets, see the Media upload documentation.
  • For details on all available upload parameters, see the Upload method of the Upload API Reference.

Direct uploading from the browser

The upload samples shown above allow your server-side Go code to upload media assets to Cloudinary. In this flow, if you have a web form that allows your users to upload images or videos, the media file's data is first sent to your server and then uploaded to Cloudinary.

A more efficient and powerful option is to allow your users to upload images and videos in your client-side code directly from the browser to Cloudinary instead of going through your servers. This method allows for faster uploading and a better user experience. It also reduces load from your servers and reduces the complexity of your Go applications.

You can upload files directly from the browser using signed or unsigned calls to the upload endpoint, as shown in this example: Upload multiple files using a form.

Related topics

  • For more information on uploading media assets, see the Media upload documentation.
  • For details on all available upload parameters, see the Upload method of the Upload API Reference.

✔️ Feedback sent!

Rate this page: