英文:
Golang Save File from URL to GCS Bucket in AppEngine
问题
我正在尝试从URL获取图像文件,并使用这里的文档将其保存到GCS存储中。
在我的AppEngine环境中,我无法弄清楚如何从response.Body
创建文件并将其保存到GSC中...在这个环境中,我不能使用os.Create
并引用文件路径,对吗?
- 寻找在部署在AppEngine中时将
response.Body
传递给wc.Write()
方法的提示。
获取文件的代码:
func main() {
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)
}
// 使用io.Copy将响应体直接转储到文件中。这支持大文件
_, err = io.Copy(file, response.Body)
if err != nil {
log.Fatal(err)
}
file.Close()
fmt.Println("Success!")
}
将本地文件保存到GCS:
func (d *demo) createFile(fileName string) {
fmt.Fprintf(d.w, "Creating file /%v/%v\n", d.bucketName, fileName)
wc := d.bucket.Object(fileName).NewWriter(d.ctx)
wc.ContentType = "text/plain"
wc.Metadata = map[string]string{
"x-goog-meta-foo": "foo",
"x-goog-meta-bar": "bar",
}
d.cleanUp = append(d.cleanUp, fileName)
if _, err := wc.Write([]byte("abcde\n")); err != nil {
d.errorf("createFile: unable to write data to bucket %q, file %q: %v", d.bucketName, fileName, err)
return
}
if _, err := wc.Write([]byte(strings.Repeat("f", 1024*4) + "\n")); err != nil {
d.errorf("createFile: unable to write data to bucket %q, file %q: %v", d.bucketName, fileName, err)
return
}
if err := wc.Close(); err != nil {
d.errorf("createFile: unable to close bucket %q, file %q: %v", d.bucketName, fileName, err)
return
}
}
你可以在这里找到有关从URL保存图像文件的更多信息。
英文:
I'm trying to get a image file from URL and save it to GCS storage using the documentation here.
I can't figure out how to create the file from response.Body
to GSC given my AppEngine environment... I can't create using os.Create and reference a file path in this environment, right?
- Looking for tips on passing the
response.Body
to thewc.Write()
method while deployed in AppEngine.
Code to get file:
func main() {
url := "http://i.imgur.com/m1UIjW1.jpg"
// don't worry about errors
response, e := http.Get(url)
if e != nil {
log.Fatal(e)
}
defer response.Body.Close()
//open a file for writing
file, err := os.Create("/tmp/asdf.jpg")
if err != nil {
log.Fatal(err)
}
// Use io.Copy to just dump the response body to the file. This supports huge files
_, err = io.Copy(file, response.Body)
if err != nil {
log.Fatal(err)
}
file.Close()
fmt.Println("Success!")
}
Save a local file to GCS
func (d *demo) createFile(fileName string) {
fmt.Fprintf(d.w, "Creating file /%v/%v\n", d.bucketName, fileName)
wc := d.bucket.Object(fileName).NewWriter(d.ctx)
wc.ContentType = "text/plain"
wc.Metadata = map[string]string{
"x-goog-meta-foo": "foo",
"x-goog-meta-bar": "bar",
}
d.cleanUp = append(d.cleanUp, fileName)
if _, err := wc.Write([]byte("abcde\n")); err != nil {
d.errorf("createFile: unable to write data to bucket %q, file %q: %v", d.bucketName, fileName, err)
return
}
if _, err := wc.Write([]byte(strings.Repeat("f", 1024*4) + "\n")); err != nil {
d.errorf("createFile: unable to write data to bucket %q, file %q: %v", d.bucketName, fileName, err)
return
}
if err := wc.Close(); err != nil {
d.errorf("createFile: unable to close bucket %q, file %q: %v", d.bucketName, fileName, err)
return
}
}
答案1
得分: 0
看起来你正在寻找一个Writer和一个bytes.Buffer:
var buff bytes.Buffer
writer := NewWriter(buff)
n, err := io.Copy(writer, response.Body)
英文:
It seems like you are looking for a Writer and a bytes.Buffer:
var buff bytes.Buffer
writer := NewWriter(buff)
n, err := io.Copy(writer, response.Body)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论