Downloaded image is not opening

I’ve this image file shared publicly at google drive: Cashew SC pic - Abdulhadi Khalifah.jpeg - Google Drive

I want to download it at my computer, so I wrote the below code:

package main

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

func main() {
    // image link, open at browser
	// https://drive.google.com/open?id=1WPWH9zkQ25CASuh3uXs7T3i5PvsEfIKU

	
	url := "https://drive.google.com/download?id=1WPWH9zkQ25CASuh3uXs7T3i5PvsEfIKU"
    // or
    //	url := "https://googledrive.com/host/1WPWH9zkQ25CASuh3uXs7T3i5PvsEfIKU"
	
    fileName := "file.jpeg"
	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, "-", err)
		return
	}
	defer response.Body.Close()

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

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

A file had been downloaded, but once I opened it I got this error:
enter image description here

Have you checked the response’s StatusCode ?

For instance if I put https://drive.google.com/download?id=1WPWH9zkQ25CASuh3uXs7T3i5PvsEfIKU in the browser I get a “NOT FOUND” page

Thanks for your catch, the mistake was in the url, the correct code should be:

	image := "https://drive.google.com/open?id=1WPWH9zkQ25CASuh3uXs7T3i5PvsEfIKU"
	id := image[33:]
	url := "https://docs.google.com/uc?export=download&id=" + id

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.