In the process of learning golang, I'm trying to write a web app with multiple image upload functionality.

I'm using Azure Blob Storage to store images, but I am having trouble streaming the images from the multipart request to Blob Storage.

Here's the handler I've written so far:

func (imgc *ImageController) UploadInstanceImageHandler(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
reader, err := r.MultipartReader()

if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
}

for {
    part, partErr := reader.NextPart()

    // No more parts to process
    if partErr == io.EOF {
        break
    }

    // if part.FileName() is empty, skip this iteration.
    if part.FileName() == "" {
        continue
    }

    // Check file type
    if part.Header["Content-Type"][0] != "image/jpeg" {
        fmt.Printf("
Not image/jpeg!")
        break
    }

    var read uint64
    fileName := uuid.NewV4().String() + ".jpg"
    buffer := make([]byte, 100000000)

    // Get Size
    for {
        cBytes, err := part.Read(buffer)

        if err == io.EOF {
            fmt.Printf("
Last buffer read!")
            break
        }

        read = read + uint64(cBytes)
    }

    stream := bytes.NewReader(buffer[0:read])
    err = imgc.blobClient.CreateBlockBlobFromReader(imgc.imageContainer, fileName, read, stream, nil)

    if err != nil {
        fmt.Println(err)
        break
    }
}

w.WriteHeader(http.StatusOK)

}

In the process of my research, I've read through using r.FormFile, ParseMultipartForm, but decided on trying to learn how to use MultiPartReader.

I was able to upload an image to the golang backend and save the file to my machine using MultiPartReader.

At the moment, I'm able to upload files to Azure but they end up being corrupted. The file sizes seem on point but clearly something is not working.

Am I misunderstanding how to create a io.Reader for CreateBlockBlobFromReader?

Any help is much appreciated!