从Google Drive下载公共文件 – Golang

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

Download public file from Google Drive - Golang

问题

我有一个存储在Google Drive上的zip文件(它是公开共享的)。我想知道如何在Golang中下载它。这段代码只是创建一个名为"file.zip"的空文件:

package main

import (
    "fmt"
    "io"
    "net/http"
    "os"
)

func main() {
    url := "https://docs.google.com/uc?export=download&id=0B2Q7X-dUtUBebElySVh1ZS1iaTQ"
    fileName := "file.zip"
    fmt.Println("正在下载文件...")

    output, err := os.Create(fileName)
    defer output.Close()

    response, err := http.Get(url)
    if err != nil {
        fmt.Println("下载错误", url, "-", err)
        return
    }
    defer response.Body.Close()

    n, err := io.Copy(output, response.Body)

    fmt.Println(n, "字节已下载")
}

希望对你有帮助!

英文:

I have a zip file stored on Google Drive (it is shared publicly). I want to know how to download it in Golang. This current code just creates a blank file named "file.zip":

package main

import (
    "fmt"
    "io"
    "net/http"
    "os"
)

func main() {
    url := "https://docs.google.com/uc?export=download&id=0B2Q7X-dUtUBebElySVh1ZS1iaTQ"
    fileName := "file.zip"
    fmt.Println("Downloading file...")

    output, err := os.Create(fileName)
    defer output.Close()

    response, err := http.Get(url)
    if err != nil {
        fmt.Println("Error while downloading", url, "-", eerrror)
        return
    }
    defer response.Body.Close()

    n, err := io.Copy(output, response.Body)

    fmt.Println(n, "bytes downloaded")
}

答案1

得分: 9

这似乎是一个错误,可能是与Google Drive或golang有关,我不确定是哪个!

问题在于你提供的第一个URL会重定向到第二个URL,看起来像这样:

https://doc-00-c8-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/8i67l6m6cdojptjuh883mu0qqmtptds1/1376330400000/06448503420061938118/*/0B2Q7X-dUtUBebElySVh1ZS1iaTQ?h=16653014193614665626&e=download

请注意URL中的*,根据这个stackoverflow问题,这是合法的。然而,它在作为分隔符时具有特殊含义。

Go使用%2A*进行编码,如下所示:

https://doc-00-c8-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/8i67l6m6cdojptjuh883mu0qqmtptds1/1376330400000/06448503420061938118/%2A/0B2Q7X-dUtUBebElySVh1ZS1iaTQ?h=16653014193614665626&e=download

而Google回复“403 Forbidden”。

Google似乎没有将%2A解析为*

根据维基百科上的这篇文章,URI方案中使用的保留字符(其中包括*):如果需要将该字符用于其他目的,则必须对该字符进行百分比编码。

我对此不够了解,无法说出谁是对的,但由于Google编写了问题的两个部分,肯定是他们的某个地方出了问题!

这是我用于测试的程序

英文:

This appears to be a bug, either with Google drive or with golang, I'm not sure which!

The problem is that the first URL you gave redirects to a second URL which looks something like this

https://doc-00-c8-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/8i67l6m6cdojptjuh883mu0qqmtptds1/1376330400000/06448503420061938118/*/0B2Q7X-dUtUBebElySVh1ZS1iaTQ?h=16653014193614665626&e=download

Note the * in the URL which is legal according to this stack overflow question. However it does have a special meaning as a delimeter.

Go fetches the URL with the * encoded as %2A like this

https://doc-00-c8-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/8i67l6m6cdojptjuh883mu0qqmtptds1/1376330400000/06448503420061938118/%2A/0B2Q7X-dUtUBebElySVh1ZS1iaTQ?h=16653014193614665626&e=download

Which Google replies "403 Forbidden" to.

Google doesn't seem to be resolving the %2A into a *.

According to this article on wikipedia reserved characters (of which * is one) used in a URI scheme: if it is necessary to use that character for some other purpose, then the character must be percent-encoded.

I'm not enough of an expert on this to say who is right, but since Google wrote both parts of the problem it is definitely their fault somewhere!

Here is the program I was using for testing

答案2

得分: 6

我找到了解决方案。
使用:https://googledrive.com/host/ID

而不是:https://docs.google.com/uc?export=download&id=ID

英文:

I found the solution.
Use: https://googledrive.com/host/ID

Instead of: https://docs.google.com/uc?export=download&id=ID

答案3

得分: 3

我还在调查为什么会发生这种情况,与此同时,你可以使用这个解决方法:

http://play.golang.org/p/SzGBAiZdGJ

当发生重定向时,会调用CheckRedirect函数,你可以添加一个不透明路径来避免URL进行编码。

Francesc

英文:

I'm still investigating on why this is happening, in the meanwhile you can use this workaround:

http://play.golang.org/p/SzGBAiZdGJ

CheckRedirect is called when a redirect happens and you can add an Opaque path to avoid having the URL url-encoded.

Francesc

huangapple
  • 本文由 发表于 2013年8月12日 06:38:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/18177419.html
匿名

发表评论

匿名网友

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

确定