Home  >  Article  >  Backend Development  >  Why does my Go program download an empty zip file when I try to download a public file from Google Drive?

Why does my Go program download an empty zip file when I try to download a public file from Google Drive?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-30 14:16:26469browse

Why does my Go program download an empty zip file when I try to download a public file from Google Drive?

Downloading Public Files from Google Drive in Golang

When attempting to download a publicly shared zip file from Google Drive, you may encounter an issue where a blank "file.zip" is created instead. This is due to a potential bug involving either Google Drive or Go.

The root of the problem lies in the fact that the initial download URL provided by Google Drive redirects to a second URL containing an asterisk (*) character as a delimiter. However, when Go attempts to fetch this URL, it encodes the asterisk as *, changing the URL to:

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

Unfortunately, Google responds with "403 Forbidden" to this encoded URL, suggesting that the * encoding is not being successfully interpreted as an asterisk.

To resolve this issue:

  1. Obtain the Corrected URL: Use a web browser or a tool like cURL to access the initially provided download URL and follow the redirects until you reach the final URL containing the asterisk, which should look something like:
https://doc-00-c8-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/8i67l6m6cdojptjuh883mu0qqmtptds1/1376330400000/06448503420061938118/*/0B2Q7X-dUtUBebElySVh1ZS1iaTQ?h=16653014193614665626&e=download
  1. Manually Encode the Asterisk: Manually encode the asterisk (*) character in the corrected URL using percent-encoding. This results in the following URL:
https://doc-00-c8-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/8i67l6m6cdojptjuh883mu0qqmtptds1/1376330400000/06448503420061938118/%2A/0B2Q7X-dUtUBebElySVh1ZS1iaTQ?h=16653014193614665626&e=download
  1. Use the Corrected URL: Update your Go code to use the manually encoded URL. This should allow you to successfully download the zip file from Google Drive.

The above is the detailed content of Why does my Go program download an empty zip file when I try to download a public file from Google Drive?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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