Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the best practices for integrating Cloudinary with Go Fiber for large file uploads
    Cloudinary Go Fiber large file uploads `upload_large` file streaming `upload_widget` BodyLimit StreamRequestBody error handling Cloudinary SDK


What are the best practices for integrating Cloudinary with Go Fiber for large file uploads


To integrate Cloudinary with Go Fiber for large file uploads, follow these best practices:

1. Use the `upload_large` Method:
- Cloudinary's `upload_large` method allows you to upload large files in chunks, which is more efficient and scalable for handling large files.

2. Stream the File:
- Stream the file directly from the client to Cloudinary, bypassing your server. This approach reduces server load and improves performance.

3. Use the `upload_widget`:
- Cloudinary's `upload_widget` is an interactive, feature-rich method for uploading files directly to Cloudinary. It includes a complete graphical interface and supports various file sources.

4. Configure the `BodyLimit`:
- Set the `BodyLimit` in your Fiber application to handle larger files. The default limit is 4MB, which can be increased as needed.

5. Use the `StreamRequestBody`:
- Enable `StreamRequestBody` to handle large files by streaming the request body directly to Cloudinary.

6. Handle Errors:
- Implement error handling to ensure that your application can handle any errors that may occur during the upload process.

7. Monitor Upload Progress:
- Monitor the upload progress to provide a better user experience and handle any errors that may occur during the upload process.

8. Use a Cloudinary SDK:
- Use a Cloudinary SDK for Go to simplify the integration and handle various upload scenarios efficiently.

Here is an example of how to use the `upload_large` method in Go Fiber:

go
package main

import (
    "context"
    "fmt"
    "io"
    "log"
    "net/http"

    "github.com/gofiber/fiber/v2"
    "github.com/cloudinary/cloudinary-go/v2"
    "github.com/cloudinary/cloudinary-go/v2/api/uploader"
)

func main() {
    app := fiber.New(fiber.Config{
        BodyLimit: 100 * 1024 * 1024, // 100MB
    })

    // Initialize Cloudinary
    cldnry, err := cloudinary.NewFromAPIKey("your_api_key", "your_api_secret", "your_cloud_name")
    if err != nil {
        log.Fatal(err)
    }

    // Define the upload preset
    preset := "your_upload_preset"

    // Define the upload method
    upload := func(ctx context.Context, file io.Reader, filename string) (string, error) {
        // Upload the file in chunks
        uploadParams := &uploader.UploadParams{
            PublicID:   filename,
            ResourceType: uploader.File,
            UseFilename: true,
            Preset:     preset,
        }
        uploadResp, err := cldnry.Upload.Upload(ctx, file, uploadParams)
        if err != nil {
            return "", err
        }
        return uploadResp.PublicID, nil
    }

    // Define the upload endpoint
    app.Post("/upload", func(c *fiber.Ctx) error {
        // Get the file from the request body
        file, err := c.FormFile("file")
        if err != nil {
            return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err})
        }

        // Upload the file
        publicID, err := upload(c.Context(), file.File, file.Filename)
        if err != nil {
            return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err})
        }

        // Return the uploaded file's public ID
        return c.Status(fiber.StatusOK).JSON(fiber.Map{"public_id": publicID})
    })

    // Start the server
    log.Fatal(app.Listen(":3000"))
}

This example demonstrates how to handle large file uploads using the `upload_large` method in Go Fiber.

Citations:
[1] https://dev.to/hackmamba/robust-media-upload-with-golang-and-cloudinary-fiber-version-2cmf
[2] https://cloudinary.com/documentation/go_image_and_video_upload
[3] https://github.com/gofiber/fiber/issues/272
[4] https://stackoverflow.com/questions/68215277/how-to-upload-large-files-to-cloudinary-in-react
[5] https://cloudinary.com/documentation/client_side_uploading