Golang将URL中的文件保存到AppEngine中的GCS存储桶

huangapple go评论86阅读模式
英文:

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 the wc.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)

huangapple
  • 本文由 发表于 2017年1月23日 01:30:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/41794004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定