drake900918 2018-11-17 12:22
浏览 85
已采纳

从url中获取http.Get()图像并写入GridFS

I want to request an image from an url and write that image into a mongoDb GridFS database. The only working approach I got is to save the requested body to an file on OS and open it again.

...

response, err := http.Get("https://via.placeholder.com/350x150")

defer response.Body.Close()

file, _ := os.Create("file-name-placeholder.jpg")

b, _ := io.Copy(file, response.Body)

file.Close()

file, err = os.Open("file-name-placeholder.jpg")

gridFile, err := gridfs.Create("example-file.jpg")

_, err = io.Copy(gridFile, datei)

....

Is there a possibility to instantaneously write into GridFS?

Edit: I tried the shortway like @Vorsprung suggested. In this case there is an empty image in the database. As you can see, this first document has only length 0 and there is no document in the the db.images.chunks.find()collection for the first image-file.

> db.images.files.find()
{ "_id" : ObjectId("5bf0156b2f337b5b0636e711"), "chunkSize" : 261120, "uploadDate" : ISODate("2018-11-17T13:19:39.354Z"), "length" : 0, "md5" : "d41d8cd98f00b204e9800998ecf8427e", "filename" : "googlelogo_color_272x92dp.png" }
{ "_id" : ObjectId("5bf018a52f337b692d8cafd1"), "chunkSize" : 261120, "uploadDate" : ISODate("2018-11-17T13:33:25.208Z"), "length" : 5969, "md5" : "8f9327db2597fa57d2f42b4a6c5a9855", "filename" : "googlelogo_color_272x92dp.png" }
  • 写回答

1条回答 默认 最新

  • dongzuoyue6556 2018-11-17 13:00
    关注

    Have you tried io.Copy(gridFile, response.Body)? seems like an obvious short circuit

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看