英文:
Golang GAE Save Image Url to Blobstore
问题
我正在寻找在Go语言中保存图像的方法,类似于这样的代码:
url := "http://i.imgur.com/m1UIjW1.jpg"
response, e := http.Get(url)
if e != nil {
log.Fatal(e)
}
defer response.Body.Close()
file, err := os.Create("/tmp/asdf.jpg")
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(file, response.Body)
if err != nil {
log.Fatal(err)
}
file.Close()
然而,我正在使用GAE上的Blobstore,而我找到的所有示例似乎都是基于用户浏览器的多部分表单上传...
我该如何使用简单的GET请求在GAE/Blobstore上下载图像:
func handler(w http.ResponseWriter, r *http.Request) {
urlImage := "http://i.imgur.com/m1UIjW1.jpg"
// 当用户调用此根处理程序时,将urlImage下载到Blobstore
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
参考链接:
1: https://stackoverflow.com/questions/22417283/save-an-image-from-url-to-file-go
2: https://cloud.google.com/appengine/docs/go/blobstore/reference
3: https://cloud.google.com/appengine/docs/go/blobstore/
英文:
I am looking to save an image in Go, something like this:
url := "http://i.imgur.com/m1UIjW1.jpg"
response, e := http.Get(url)
if e != nil {
log.Fatal(e)
}
defer response.Body.Close()
file, err := os.Create("/tmp/asdf.jpg")
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(file, response.Body)
if err != nil {
log.Fatal(err)
}
file.Close()
However - I am using Blobstore on GAE and all of the examples I find seem to be based on some multi-part form upload based on a users browser...
How do I download an image on GAE/Blobstore with a simple GET request:
func handler(w http.ResponseWriter, r *http.Request) {
urlImage := "http://i.imgur.com/m1UIjW1.jpg"
//when a user calls this root handle, download urlImage to Blobstore
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
答案1
得分: 2
我认为你的意思是“将图像上传到Blobstore”,而不是“在Blobstore上下载图像”。
以前,你可以使用https://cloud.google.com/appengine/docs/go/blobstore/reference#Create创建一个blobstore
的Writer
,然后在上面写入数据,并关闭它。但是,正如文档中提到的,这种方法现在已经被弃用了;现在,你应该使用cloud/storage
包,就像在https://godoc.org/google.golang.org/cloud/storage#example-NewWriter
的示例中所示:
wc := storage.NewWriter(ctx, "bucketname", "filename1")
wc.ContentType = "image/jpg"
wc.ACL = []storage.ACLRule{{storage.AllUsers, storage.RoleReader}}
if _, err := wc.Write(response.Body); err != nil {
log.Fatal(err)
}
等等,等等——与示例相比,我只改变了内容类型和写入的字节。
基本上,Blobstore已被Cloud Storage取代,你应该使用后者。
英文:
I think, from context, that you mean "upload an image to blobstore" where you say "download an image on blobstore".
Once upon a time you would have created a blobstore
Writer
with https://cloud.google.com/appengine/docs/go/blobstore/reference#Create , then written on it, and closed it. But as the docs mention this is now deprecated; nowadays, you use instead the package cloud/storage
as in the example at https://godoc.org/google.golang.org/cloud/storage#example-NewWriter
:
wc := storage.NewWriter(ctx, "bucketname", "filename1")
wc.ContentType = "image/jpg"
wc.ACL = []storage.ACLRule{{storage.AllUsers, storage.RoleReader}}
if _, err := wc.Write(response.Body); err != nil {
log.Fatal(err)
}
etc, etc -- compared to the example, I've only changed the content type and exactly what bytes you write.
Essentially, blobstore has been replaced by cloud storage, and you should use the latter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论