Download Files

The Drive API allows you to download files that are stored in Google Drive. Also, you can download exported versions of Google Documents (Documents, Spreadsheets, Presentations, etc.) in formats that your app can handle. Drive also supports providing users direct access to a file via the URL in the webViewLink property.

Depending on the type of download you'd like to perform — a file, a Google Document, or a content link — you'll use one of the following URLs:

The rest of this page provides detailed instructions for performing these types of downloads:

Downloading a file

Using alt=media

To download files, you make an authorized HTTP GET request to the file's resource URL and include the query parameter alt=media. For example:

GET https://www.googleapis.com/drive/v3/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media
Authorization: Bearer ya29.AHESVbXTUv5mHMo3RYfmS1YJonjzzdTOFZwvyOAUVhrs

Downloading the file requires the user to have at least read access. Additionally, your app must be authorized with a scope that allows reading of file content. For example, an app using the drive.readonly.metadata scope would not be authorized to download the file contents. Users with edit permission may restrict downloading by read-only users by setting the viewersCanCopyContent field to true.

Files identified as abusive (malware, etc.) are only downloadable by the owner. Additionally, the query parameter acknowledgeAbuse=true must be included to indicate that the user has acknowledged the risk of downloading potential malware. Your application should interactively warn the user before using this query parameter.

Examples

Here are examples of performing a file download with our Drive API client libraries.

Java

String fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId)
        .executeMediaAndDownloadTo(outputStream);

Python

file_id = '0BwwA4oUTeiV1UVNwOHItT0xfa2M'
request = drive_service.files().get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
    status, done = downloader.next_chunk()
    print "Download %d%%." % int(status.progress() * 100)

PHP

$fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
$content = $driveService->files->get($fileId, array(
  'alt' => 'media' ));

.NET

var fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
var request = driveService.Files.Get(fileId);
var stream = new System.IO.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 +=
    (IDownloadProgress 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);

Ruby

file_id = '0BwwA4oUTeiV1UVNwOHItT0xfa2M'
content = drive_service.get_file(file_id, download_dest: StringIO.new)

Node.js

var fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
var dest = fs.createWriteStream('/tmp/photo.jpg');
drive.files.get({
   fileId: fileId,
   alt: 'media'
})
.on('end', function() {
  console.log('Done');
})
.on('error', function(err) {
  console.log('Error during download', err);
})
.pipe(dest);

Partial download

Partial download involves downloading only a specified portion of a file. You can specify the portion of the file you want to dowload by using a byte range with the Range header. For example:

Range: bytes=500-999

Downloading Google Documents

The following examples demonstrate how to download a Google Document in PDF format using the client libraries:

Java

String fileId = "1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().export(fileId, "application/pdf")
        .executeMediaAndDownloadTo(outputStream);

Python

file_id = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo'
request = drive_service.files().export_media(fileId=file_id,
                                             mimeType='application/pdf')
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
    status, done = downloader.next_chunk()
    print "Download %d%%." % int(status.progress() * 100)

PHP

$fileId = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo';
$content = $driveService->files->export($fileId, 'application/pdf', array(
  'alt' => 'media' ));

.NET

var fileId = "1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo";
var request = driveService.Files.Export(fileId, "application/pdf");
var stream = new System.IO.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 +=
        (IDownloadProgress 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);

Ruby

file_id = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo'
content = drive_service.export_file(file_id,
                                    'application/pdf',
                                    download_dest: StringIO.new)

Node.js

var fileId = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo';
var dest = fs.createWriteStream('/tmp/resume.pdf');
drive.files.export({
   fileId: fileId,
   mimeType: 'application/pdf'
})
.on('end', function() {
  console.log('Done');
})
.on('error', function(err) {
  console.log('Error during download', err);
})
.pipe(dest);

Google Doc formats and supported export MIME types map to each other as follows:

Google Doc Format Conversion Format Corresponding MIME type
Documents HTML text/html
Plain text text/plain
Rich text application/rtf
Open Office doc application/vnd.oasis.opendocument.text
PDF application/pdf
MS Word document application/vnd.openxmlformats-officedocument.wordprocessingml.document
Spreadsheets MS Excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Open Office sheet application/x-vnd.oasis.opendocument.spreadsheet
PDF application/pdf
CSV (first sheet only) text/csv
Drawings JPEG image/jpeg
PNG image/png
SVG image/svg+xml
PDF application/pdf
Presentations MS PowerPoint application/vnd.openxmlformats-officedocument.presentationml.presentation
PDF application/pdf
Plain text text/plain
Apps Scripts JSON application/vnd.google-apps.script+json

Viewing files in a browser

If you want to allow an unauthorized user to view a file directly in a web browser instead of through the API, use the webContentLink. You can either redirect a user to this URL, or offer it as a clickable link.

Send feedback about...

Drive REST API
Drive REST API