>  기사  >  백엔드 개발  >  Golang을 사용하여 Google 드라이브에서 공개 파일을 다운로드할 때 빈 \"file.zip\"이 나타나는 이유는 무엇입니까?

Golang을 사용하여 Google 드라이브에서 공개 파일을 다운로드할 때 빈 \"file.zip\"이 나타나는 이유는 무엇입니까?

Barbara Streisand
Barbara Streisand원래의
2024-10-28 05:48:30621검색

Why am I getting a blank

Golang을 사용하여 Google 드라이브에서 공개 파일 다운로드

문제

Golang을 사용하여 Google 드라이브에서 공개 파일을 다운로드하려는 프로그래머에게 문제가 발생합니다. 공개 파일을 공유했음에도 불구하고 현재 코드에서는 빈 "file.zip"이 생성됩니다.

<code class="go">import (
    "fmt"
    "io"
    "net/http"
    "os"
)

// Main function
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, "-", error)
        return
    }
    defer response.Body.Close()
    n, err := io.Copy(output, response.Body)
    fmt.Println(n, "bytes downloaded")
}</code>

해결 방법

조사 결과 해당 URL이 다른 URL로 리디렉션되는 것으로 확인되었습니다. 특수 문자 "*"가 포함되어 있습니다. 이 문자는 Golang에서 올바르게 처리되지 않으며 "%2A" 대신 "*"로 인코딩되어 "403 Forbidden" 응답이 발생합니다.

해결책은 특수 문자를 처리하도록 프로그램을 수정하는 것입니다. 올바르게:

<code class="go">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()

url = strings.Replace(url, "%2A", "%252A", -1) // Replace * with its percent-encoded form

response, err := http.Get(url)</code>

위 내용은 Golang을 사용하여 Google 드라이브에서 공개 파일을 다운로드할 때 빈 \"file.zip\"이 나타나는 이유는 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.