top of page
Search
profapmegoodtoti

Download File Google Drive Api: Best Practices for Handling Download and Export Actions



My desktop application, written in java, tries to download public files from Google Drive. As i found out, it can be implemented by using file's webContentLink (it's for ability to download public files without user authorization).




Download File Google Drive Api




All you have to do is simple get the host URL for a publicly shared drive folder. To do this, you can upload a plain HTML file and preview it in Google Drive to find your host URL.


You essentially need to first download the normal download link, which however redirects you to the "Download anyway" page. You need to store cookies from this first request, find out the link pointed to by the "Download anyway" button, and then use this link to download the file, but reusing the cookies you got from the first request.


It will redirect you to an index type page with a giant subdomain, but you should be able to see the files in your folder. Then you can right click to save the link to the file you want (I noticed that this direct link also has this big subdomain for googledrive.com). Worked great for me with wget.


If you just want to programmatically (as oppossed to giving the user a link to open in a browser) download a file through the Google Drive API, I would suggest using the downloadUrl of the file instead of the webContentLink, as documented here: -downloads


To download a blob file stored on Drive, use the files.get method with the ID of the file to downloadand the alt=media URL parameter. The alt=media URL parameter tells theserver that a download of content is being requested as an alternative responseformat.


: b/19236190 */ display: none; } .kd-tabbed-horz > article > pre /* Remove extra spacing */ margin: 0; devsite-selector > section[active] /* Remove code section padding */ padding: 0; .filepath color: #fff; margin: 6px; max-width: calc(100% - 160px); /* Give at least 160px for the "View on GitHub" button. */ text-overflow: ellipsis; text-shadow: rgba(0,0,0,0.1) 1px 1px; overflow: hidden; .view-on-github text-shadow: rgba(12,12,12,0.1) 1px 1px; .github-docwidget-include border-radius: 0 !important; margin: 0 -1px; drive/snippets/drive_v3/src/main/java/DownloadFile.java View on GitHub import com.google.api.client.googleapis.json.GoogleJsonResponseException;import com.google.api.client.http.HttpRequestInitializer;import com.google.api.client.http.javanet.NetHttpTransport;import com.google.api.client.json.gson.GsonFactory;import com.google.api.services.drive.Drive;import com.google.api.services.drive.DriveScopes;import com.google.auth.http.HttpCredentialsAdapter;import com.google.auth.oauth2.GoogleCredentials;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.Arrays;/* Class to demonstrate use-case of drive's download file. */public class DownloadFile /** * Download a Document file in PDF format. * * @param realFileId file ID of any workspace document format file. * @return byte array stream if successful, @code null otherwise. * @throws IOException if service account credentials file not found. */ public static ByteArrayOutputStream downloadFile(String realFileId) throws IOException /* Load pre-authorized user credentials from the environment. TODO(developer) - See for guides on implementing OAuth2 for your application.*/ GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() .createScoped(Arrays.asList(DriveScopes.DRIVE_FILE)); HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( credentials); // Build a new authorized API client service. Drive service = new Drive.Builder(new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) .setApplicationName("Drive samples") .build(); try OutputStream outputStream = new ByteArrayOutputStream(); service.files().get(realFileId) .executeMediaAndDownloadTo(outputStream); return (ByteArrayOutputStream) outputStream; catch (GoogleJsonResponseException e) // TODO(developer) - handle error appropriately System.err.println("Unable to move file: " + e.getDetails()); throw e; Python /* Remove extra DevSite2 margin */ .github-docwidget-gitinclude-code devsite-code, devsite-selector>section>devsite-code, devsite-selector>section>.github-docwidget-include, devsite-selector>section>.github-docwidget-gitinclude-code>devsite-code margin: 0; /* Disables includecode margin */ .github-docwidget-gitinclude-code .prettyprint margin: 0; .ds-selector-tabs > section > p /* Remove extra : b/19236190 */ display: none; .kd-tabbed-horz > article > pre /* Remove extra spacing */ margin: 0; devsite-selector > section[active] /* Remove code section padding */ padding: 0; .filepath color: #fff; margin: 6px; max-width: calc(100% - 160px); /* Give at least 160px for the "View on GitHub" button. */ text-overflow: ellipsis; text-shadow: rgba(0,0,0,0.1) 1px 1px; overflow: hidden; .view-on-github text-shadow: rgba(12,12,12,0.1) 1px 1px; .github-docwidget-include border-radius: 0 !important; margin: 0 -1px; drive/snippets/drive-v3/file_snippet/download_file.py View on GitHub from __future__ import print_functionimport ioimport google.authfrom googleapiclient.discovery import buildfrom googleapiclient.errors import HttpErrorfrom googleapiclient.http import MediaIoBaseDownloaddef download_file(real_file_id): """Downloads a file Args: real_file_id: ID of the file to download Returns : IO object with location. Load pre-authorized user credentials from the environment. TODO(developer) - See for guides on implementing OAuth2 for the application. """ creds, _ = google.auth.default() try: # create drive api client service = build('drive', 'v3', credentials=creds) file_id = real_file_id # pylint: disable=maybe-no-member request = service.files().get_media(fileId=file_id) file = io.BytesIO() downloader = MediaIoBaseDownload(file, request) done = False while done is False: status, done = downloader.next_chunk() print(F'Download int(status.progress() * 100).') except HttpError as error: print(F'An error occurred: error') file = None return file.getvalue()if __name__ == '__main__': download_file(real_file_id='1KuPmvGq8yoYgbfW74OENMCB5H0n_2Jm9')Node.js /* Remove extra DevSite2 margin */ .github-docwidget-gitinclude-code devsite-code, devsite-selector>section>devsite-code, devsite-selector>section>.github-docwidget-include, devsite-selector>section>.github-docwidget-gitinclude-code>devsite-code margin: 0; /* Disables includecode margin */ .github-docwidget-gitinclude-code .prettyprint margin: 0; .ds-selector-tabs > section > p /* Remove extra : b/19236190 */ display: none; .kd-tabbed-horz > article > pre /* Remove extra spacing */ margin: 0; devsite-selector > section[active] /* Remove code section padding */ padding: 0; .filepath color: #fff; margin: 6px; max-width: calc(100% - 160px); /* Give at least 160px for the "View on GitHub" button. */ text-overflow: ellipsis; text-shadow: rgba(0,0,0,0.1) 1px 1px; overflow: hidden; .view-on-github text-shadow: rgba(12,12,12,0.1) 1px 1px; .github-docwidget-include border-radius: 0 !important; margin: 0 -1px; drive/snippets/drive_v3/file_snippets/download_file.js View on GitHub /** * Downloads a file * @paramstring realFileId file ID * @returnobj file status * */async function downloadFile(realFileId) // Get credentials and build service // TODO (developer) - Use appropriate auth mechanism for your app const GoogleAuth = require('google-auth-library'); const google = require('googleapis'); const auth = new GoogleAuth( scopes: ' ', ); const service = google.drive(version: 'v3', auth); fileId = realFileId; try const file = await service.files.get( fileId: fileId, alt: 'media', ); console.log(file.status); return file.status; catch (err) // TODO(developer) - Handle error throw err; PHP /* Remove extra DevSite2 margin */ .github-docwidget-gitinclude-code devsite-code, devsite-selector>section>devsite-code, devsite-selector>section>.github-docwidget-include, devsite-selector>section>.github-docwidget-gitinclude-code>devsite-code margin: 0; /* Disables includecode margin */ .github-docwidget-gitinclude-code .prettyprint margin: 0; .ds-selector-tabs > section > p /* Remove extra : b/19236190 */ display: none; .kd-tabbed-horz > article > pre /* Remove extra spacing */ margin: 0; devsite-selector > section[active] /* Remove code section padding */ padding: 0; .filepath color: #fff; margin: 6px; max-width: calc(100% - 160px); /* Give at least 160px for the "View on GitHub" button. */ text-overflow: ellipsis; text-shadow: rgba(0,0,0,0.1) 1px 1px; overflow: hidden; .view-on-github text-shadow: rgba(12,12,12,0.1) 1px 1px; .github-docwidget-include border-radius: 0 !important; margin: 0 -1px; drive/snippets/drive_v3/src/DriveDownloadFile.php View on GitHub use Google\Client;use Google\Service\Drive;function downloadFile() try $client = new Client(); $client->useApplicationDefaultCredentials(); $client->addScope(Drive::DRIVE); $driveService = new Drive($client); $realFileId = readline("Enter File Id: "); $fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M'; $fileId = $realFileId; $response = $driveService->files->get($fileId, array( 'alt' => 'media')); $content = $response->getBody()->getContents(); return $content; catch(Exception $e) echo "Error Message: ".$e; .NET /* Remove extra DevSite2 margin */ .github-docwidget-gitinclude-code devsite-code, devsite-selector>section>devsite-code, devsite-selector>section>.github-docwidget-include, devsite-selector>section>.github-docwidget-gitinclude-code>devsite-code margin: 0; /* Disables includecode margin */ .github-docwidget-gitinclude-code .prettyprint margin: 0; .ds-selector-tabs > section > p /* Remove extra : b/19236190 */ display: none; .kd-tabbed-horz > article > pre /* Remove extra spacing */ margin: 0; devsite-selector > section[active] /* Remove code section padding */ padding: 0; .filepath color: #fff; margin: 6px; max-width: calc(100% - 160px); /* Give at least 160px for the "View on GitHub" button. */ text-overflow: ellipsis; text-shadow: rgba(0,0,0,0.1) 1px 1px; overflow: hidden; .view-on-github text-shadow: rgba(12,12,12,0.1) 1px 1px; .github-docwidget-include border-radius: 0 !important; margin: 0 -1px; drive/snippets/drive_v3/DriveV3Snippets/DownloadFile.cs View on GitHub using Google.Apis.Auth.OAuth2;using Google.Apis.Download;using Google.Apis.Drive.v3;using Google.Apis.Services;namespace DriveV3Snippets // Class to demonstrate use-case of drive's download file. public class DownloadFile /// /// Download a Document file in PDF format. /// /// file ID of any workspace document format file. /// byte array stream if successful, null otherwise. public static MemoryStream DriveDownloadFile(string fileId) try /* Load pre-authorized user credentials from the environment. TODO(developer) - See for guides on implementing OAuth2 for your application. */ GoogleCredential credential = GoogleCredential .GetApplicationDefault() .CreateScoped(DriveService.Scope.Drive); // Create Drive API service. var service = new DriveService(new BaseClientService.Initializer HttpClientInitializer = credential, ApplicationName = "Drive API Snippets" ); var request = service.Files.Get(fileId); var stream = new MemoryStream(); // Add a handler which will be notified on progress changes. // It will notify on each chunk download and when the // download is completed or failed. request.MediaDownloader.ProgressChanged += progress => switch (progress.Status) case DownloadStatus.Downloading: Console.WriteLine(progress.BytesDownloaded); break; case DownloadStatus.Completed: Console.WriteLine("Download complete."); break; case DownloadStatus.Failed: Console.WriteLine("Download failed."); break; ; request.Download(stream); return stream; catch (Exception e) // TODO(developer) - handle error appropriately if (e is AggregateException) Console.WriteLine("Credential Not found"); else throw; return null; This code sample uses a library method that adds the alt=media URL parameterto the underlying HTTP request. 2ff7e9595c


0 views0 comments

Recent Posts

See All

Comments


bottom of page