dougaxing8673 2018-11-02 07:14
浏览 44

从文件和存储文件创建sha256的最佳模式

I am writing a webserver that receives a file as an upload as multipart/form-data. I am generating the file sha256 from the request but due to the nature of the Reader interface, I can't reuse the data to also upload the file to a filer. These files can be a few hundred MBs. What is the best way to store the content? I can duplicate the contents but I am worried that could be wasteful on memory resources.

EDIT

func uploadFile(w http.ResponseWriter, r *http.Request) {
    f, err := r.MultipartForm.File["capture"][0].Open()
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    defer f.Close()
    hash, err := createSha(f)
    if err != nil {
        fmt.Println(err.Error())
        return
    }
}

func createSha(image multipart.File) (hash.Hash, error) {
    sha := sha256.New()
    // This cause the contents of image to no longer be available to be read again to be stored on the filer
    if _, err := io.Copy(sha, image); err != nil {
        return nil, err
    }
    return sha, nil
}
  • 写回答

1条回答 默认 最新

  • dswm97353 2018-11-02 19:18
    关注

    You might use io.MultiWriter(...) to send the data to multiple output streams concurrently, such as a hash and some remote writer.

    For example (roughly):

    sha := sha256.New()
    filer := filer.New(...) // Some Writer that stores the bytes for you?
    err := io.Copy(io.MultiWriter(sha, filer), r)
    // TODO: handle error
    // Now sha.Sum(nil) has the file digest and "filer" got sent all the bytes.
    

    Note that io.Multiwriter can take as many writers as you want, so you could compute additional hashes at the same time (e.g. md5, sha1, etc.) or even send the file to multiple locations, e.g.:

    md5, sha1, sha256, sha512 := md5.New(), sha1.New(), sha256.New(), sha512.New()
    s3Writer, gcsWriter := filer.NewS3Writer(), filer.NewGCSWriter()
    mw := io.MultiWriter(awsWriter, gcsWriter, md5, sha1, sha256, sha512)
    err := io.Copy(mw, r)
    // TODO: handle error
    // Now you've got all the hashes for the file and it's stored in the cloud.
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据