duanpu8830 2017-04-03 14:30
浏览 70

Golang通过多部分处理图像并流式传输到Azure

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!

  • 写回答

2条回答 默认 最新

  • douxianji6181 2017-04-04 01:10
    关注

    A Reader can return both an io.EOF and a valid final bytes read, it looks like the final bytes (cBytes) is not added to read total bytes. Also, careful: if an error is returned by part.Read(buffer) other than io.EOF, the read loop might not exit. Consider ioutil.ReadAll instead.

    CreateBlockBlobFromReader takes a Reader, and part is a Reader, so you may be able to pass the part in directly.

    You may also want to consider Azure block size limits might be smaller than the image, see Asure blobs.

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探