Home > Backend Development > Golang > Why Does Downloading Public Google Drive Files in Go Result in an Empty File?

Why Does Downloading Public Google Drive Files in Go Result in an Empty File?

DDD
Release: 2024-10-29 01:19:02
Original
1062 people have browsed it

Why Does Downloading Public Google Drive Files in Go Result in an Empty File?

Issue with Downloading Public Google Drive Files in Go

In this article, we aim to address an issue encountered when downloading publicly shared zip files from Google Drive using Go. The initial code snippet, provided below, creates a blank file named "file.zip":

<code class="go">package main

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

func main() {
    url := "https://docs.google.com/uc?export=download&amp;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")
}</code>
Copy after login

Investigation

Upon investigation, it was discovered that the issue lies in how Go fetches the URL. The original URL provided, when visited directly in a browser, redirects to a second URL containing an asterisk (*). However, Go encodes the asterisk as *, which Google's systems do not recognize as a valid delimiter.

Bug Identification

It appears that Go's URL handling is causing the issue. According to the URI scheme specifications, reserved characters like the asterisk (*) must be percent-encoded for alternative purposes. While Google Drive generates the redirection URL correctly, Go's implementation may not be handling encoded reserved characters as expected.

Conclusion

The issue arises due to a potential bug in either Google Drive's redirection logic or Go's URL handling. Further investigation is required to determine the exact cause and pinpoint the responsible party. However, the workaround provided in the provided solution allows for successful file download from Google Drive.

The above is the detailed content of Why Does Downloading Public Google Drive Files in Go Result in an Empty File?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template