title: Golang processing images via multipart and streaming to Azure
body: <p>In the process of learning golang, I'm trying to write a web app with multiple image upload functionality.</p>
  
  <p>I'm using Azure Blob Storage to store images, but I am having trouble streaming the images from the multipart request to Blob Storage.</p>
  
  <p>Here's the handler I've written so far:</p>
  
  <pre><code>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("\nNot 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("\nLast 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)
  </code></pre>
  
  <p>}</p>
  
  <p>In the process of my research, I've read through using r.FormFile, ParseMultipartForm, but decided on trying to learn how to use MultiPartReader.</p>
  
  <p>I was able to upload an image to the golang backend and save the file to my machine using MultiPartReader.</p>
  
  <p>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.</p>
  
  <p>Am I misunderstanding how to create a io.Reader for CreateBlockBlobFromReader?</p>
  
  <p>Any help is much appreciated!</p>