4

As the Google Drive API document said about downloading a file:

https://www.googleapis.com/drive/v3/files/FILEID?alt=media%26key=API_KEY

If we paste this link to Chrome or Safari It will start downloading.

But the problem is

  • The name is changed to FIELDID
  • The extension is gone

For example

I have file mywork.fbx I upload to google drive, and the FileID is ABCDEFG

Then I go to the link below.

https://www.googleapis.com/drive/v3/files/ABCDEFG?alt=media%26key=MYAPIKEY

I got the file that name is ABCDEFG without extension

It should be mywork.fbx or anyname.fbx not just ABCDEFG

Tried

  • add &download="mywork.fbx"
  • use PHP header('Content-Disposition: attachment; filename="mywork.fbx"'); filename is mywork.fbx but the bandwidth is also mine too It's not only Google Drive!!!
$file_url = 'https://www.googleapis.com/drive/v3/files/ABCDEFG?alt=media&key=KEY';
header('Content-Disposition: attachment; filename="mywork.fbx"');
readfile($file_url);
  • check articles in stack overflow but I can't found the right one
Chat Dp
  • 555
  • 5
  • 15

2 Answers2

2
  • You want to download a file which is not Google Docs using the browser.
  • You want to download a file with the filename which has the filename and extension of the original file.
  • You are using the API key.
    • In this case, the file is publicly shared.

If my understanding is correct, how about this answer? In this answer, webContentLink is used as the endpoint. The official document says as follows.

webContentLink:

A link for downloading the content of the file in a browser using cookie based authentication. In cases where the content is shared publicly, the content can be downloaded without any credentials.

Modified endpoint:

Please try to access the following endpoint with your browser.

https://drive.google.com/uc?export=download&id={fileId}
  • When you use this, please set the file ID. In this case, the file is not Google Docs.
  • When it accesses to the above endpoint, the file with the filename can be retrieved.

Reference:

If I misunderstood your question and this was not the direction, I apologize.

Community
  • 1
  • 1
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • 2
    It will work only a small file, not a large file, and the large file will redirect to the virus scan warning page instead of downloading immediately. – Chat Dp Oct 18 '19 at 12:05
  • @chatdev Thank you for replying. Yes. In your question, I could understand that you want to download the file using the browser. So I thought that this was suitable for your situation. If my understanding is not correct, please tell me. And also can I ask you about the detail information about your goal? – Tanaike Oct 19 '19 at 02:13
  • My goal is not using a browser directly; I want to install an in-house iOS app, so I have to create manifest.plist there be a field about a URL file. So I try to create a PHP using Header Content-Disposition It’s work, but the bandwidth is my server too not only Google. I want the URL that will not redirect and download immediately with the right extension. The PHP way is almost correct, but it should not take server bandwidth. Maybe the problem of PHP is the readfile(URL) function – Chat Dp Oct 20 '19 at 03:31
  • @chatdev Thank you for replying. You want the endpoint for directly downloading the large file with the original filename. I could understand about your goal like this. If my understanding is correct, in your case, how about the following 2 methods? 1. How about [this method](https://stackoverflow.com/a/48133859)? In this case, the file can be downloaded without displaying the redirect page for running the virus scan. Furthermore, in this case, no API key is required. – Tanaike Oct 20 '19 at 06:24
  • @chatdev 2. When you want to use Drive API, how about downloading the file metadata and file data with and without `alt=media`, respectively? Then, the file is created using the retrieved filename. In this case, 2 Drive API calls are required. If these were not useful for your situation, I apologize. – Tanaike Oct 20 '19 at 06:24
  • @chatdev About 1st method, when the file is downloaded, the original filename can be retrieved from the response header. Although this is for golang, you can see the sample script at [here](https://github.com/tanaikech/goodls). – Tanaike Oct 20 '19 at 06:39
  • your first method is to solve my goal by using PHP + Curl it will return the long string that says ' .... redirect to HREF="direct link"\A ....' I cut the string to get only the direct link. Thanks! – Chat Dp Oct 20 '19 at 13:12
  • 1
    @chatdev Thank you for replying. I'm glad your issue was resolved. Thank you, too. – Tanaike Oct 20 '19 at 23:45
0

Look at the originalFilename property of https://developers.google.com/drive/api/v3/reference/files. You can use that in a content disposition header, see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

pinoyyid
  • 21,499
  • 14
  • 64
  • 115