The Google Drive API supports several types of download and export actions, as listed in the following table:
| Download actions |
|
||||
| Export actions |
|
Before you download or export file content, verify that users can download the
file using the capabilities.canDownload field on the files resource.
For descriptions of the file types mentioned here, including blob and Google Workspace files, see File types.
The rest of this document provides detailed instructions for performing these types of download and export actions.
Download blob file content
To download a blob file stored on Drive, use the files.get method with the ID of the file to download and the
alt=media URL parameter. The alt=media URL parameter tells the server that a
download of content is being requested as an alternative response format.
The alt=media URL parameter is a system
parameter available
across all Google REST APIs. If you use a Drive API client library,
you don't need to explicitly set this parameter as the client library method
adds the alt=media URL parameter to the underlying HTTP request.
The following code samples show how to use the files.get method to download a
file:
Apps Script
/**
* Downloads a file from Drive.
* @param {string} fileId The ID of the file to download.
* @return {Blob} The file content as a Blob.
*/
function downloadFile(fileId) {
var url = 'https://www.googleapis.com/drive/v3/files/' + fileId + '?alt=media';
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
}
});
return response.getBlob();
}
Java
Python
Node.js
PHP
.NET
curl
curl -L "https://www.googleapis.com/drive/v3/files/FILE_ID?alt=media" \
--header "Authorization: Bearer ACCESS_TOKEN" \
--output "FILE_NAME"
Replace the following:
- FILE_ID: the ID of the file to download.
- ACCESS_TOKEN: the access token that grants access to the API.
- FILE_NAME: the name of the output file.
File downloads started from your app must be authorized with a scope that allows
read access to the file content. For example, an app using the
drive.readonly.metadata scope isn't authorized to download the file contents.
The client library code samples use the restricted drive file scope that allows
users to view and manage all of your Drive files. To learn more
about Drive scopes, refer to Choose Google Drive API scopes.
Users with owner permissions (for my Drive files) or
organizer permissions (for shared drive files) can restrict downloading
through the
DownloadRestrictionsMetadata
object. For more information, see Prevent users from downloading, printing, or
copying your file.
Files identified as abusive
(such as harmful software) are only downloadable by the file owner.
Additionally, the get query parameter acknowledgeAbuse=true must be included
to indicate that the user has acknowledged the risk of downloading potentially
unwanted software or other abusive files. Your application should interactively
warn the user before using this query parameter.
Partial download
Partial download involves downloading only a specified portion of a file. You
can specify the portion of the file you want to download by using a byte
range with the
Range header. For example:
Range: bytes=500-999
Download blob file content at an earlier version
You can only download blob file content revisions that are marked as "Keep Forever". If you want to download a revision, set it to "Keep Forever" first. For more information, see Specify revisions to save from auto delete.
To download the content of blob files at an earlier version, use the
revisions.get method with the ID of the
file to download, the ID of the revision, and the alt=media URL parameter. The
alt=media URL parameter tells the server that a download of content is being
requested as an alternative response format. Similar to files.get, the
revisions.get method also accepts the optional query parameter
acknowledgeAbuse and the Range header. For more information, see Manage
long-running operations.
curl
curl -L "https://www.googleapis.com/drive/v3/files/FILE_ID/revisions/REVISION_ID?alt=media" \
--header "Authorization: Bearer ACCESS_TOKEN" \
--output "FILE_NAME"
Replace the following:
- FILE_ID: the ID of the file to download.
- REVISION_ID: the ID of the revision to download.
- ACCESS_TOKEN: the access token that grants access to the API.
- FILE_NAME: the name of the output file.
Download blob file content in a browser
To download the content of blob files stored on Drive within a
browser, instead of through the API, use the webContentLink field of the files resource. If the user has download access to the file,
a link for downloading the file and its contents is returned. You can either
redirect a user to this URL, or offer it as a clickable link.
curl
curl "https://www.googleapis.com/drive/v3/files/FILE_ID?fields=webContentLink" \
--header "Authorization: Bearer ACCESS_TOKEN" \
--header "Accept: application/json"
Replace the following:
- FILE_ID: the ID of the file to get the download link for.
- ACCESS_TOKEN: the access token that grants access to the API.
Download blob file content using long-running operations
To download the content of blob files using long-running operations (LRO), use
the files.download method with the ID of
the file to download. You can optionally set the ID of the revision.
This is the only way to download Google Vids files. If you attempt to export
Google Vids files, you receive a
fileNotExportable error.
For more information, see Manage long-running
operations.
curl
The following curl command initiates a LRO and returns a JSON response. To either download the file or poll this LRO you must make another request using the returned ID to obtain the content URL. Then, you can make a final curl request to that URL to download the file. For more information, see Manage long-running operations.
curl --request POST "https://www.googleapis.com/drive/v3/files/FILE_ID/download?mimeType=video/mp4" \
--header "Authorization: Bearer ACCESS_TOKEN" \
--header "Content-Length: 0" \
--header "Accept: application/json"
Replace the following:
- FILE_ID: the ID of the file to download.
- ACCESS_TOKEN: the access token that grants access to the API.
Export Google Workspace document content
To export Google Workspace document byte content, use the files.export method with the ID of the file to export and
the correct MIME type. Exported content is limited to 10 MB.
The following code samples show how to use the files.export method to export a
Google Workspace document in PDF format:
Apps Script
/**
* Exports a Google Workspace document.
* @param {string} fileId The ID of the file to export.
* @param {string} mimeType The MIME type to export to.
* @return {Blob} The exported content as a Blob.
*/
function exportPdf(fileId, mimeType) {
var url = 'https://www.googleapis.com/drive/v3/files/' + fileId + '/export?mimeType=' + encodeURIComponent(mimeType);
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
}
});
return response.getBlob();
}
Java
Python
Node.js
PHP
.NET
curl
curl -L "https://www.googleapis.com/drive/v3/files/FILE_ID/export?mimeType=application/pdf" \
--header "Authorization: Bearer ACCESS_TOKEN" \
--output "FILE_NAME.pdf"
Replace the following:
- FILE_ID: the ID of the file to download.
- ACCESS_TOKEN: the access token that grants access to the API.
- FILE_NAME: the name of the output file.
The client library code samples use the restricted drive scope that allows
users to view and manage all of your Drive files. To learn more
about Drive scopes, refer to Choose Google Drive API scopes.
The code samples also declare the export MIME type as application/pdf. For a
complete list of all export MIME types supported for each Google Workspace
document, refer to Export MIME types for Google Workspace documents.
Export Google Workspace document content in a browser
To export Google Workspace document content within a browser, use the
exportLinks field of the
files resource. Depending on the document type, a
link to download the file and its contents is returned for every MIME type
available. You can either redirect a user to a URL, or offer it as a clickable
link.
curl
curl "https://www.googleapis.com/drive/v3/files/FILE_ID?fields=id,name,exportLinks" \
--header "Authorization: Bearer ACCESS_TOKEN" \
--header "Accept: application/json"
Replace the following:
- FILE_ID: the ID of the file to get the download link for.
- ACCESS_TOKEN: the access token that grants access to the API.
Export Google Workspace document content at an earlier version in a browser
To export Google Workspace document content at an earlier version within a
browser, use the revisions.get method with
the ID of the file to download and the ID of the revision to generate an export
link from which you can perform the download. If the user has download access to
the file, a link for downloading the file and its contents is returned. You can
either redirect a user to this URL, or offer it as a clickable link.
curl
curl "https://www.googleapis.com/drive/v3/files/FILE_ID/revisions/REVISION_ID?fields=id,name,exportLinks" \
--header "Authorization: Bearer ACCESS_TOKEN" \
--header "Accept: application/json"
Replace the following:
- FILE_ID: the ID of the file to download.
- REVISION_ID: the ID of the revision to download.
- ACCESS_TOKEN: the access token that grants access to the API.
Export Google Workspace document content using long-running operations
To export Google Workspace document content using long-running operations
(LRO), use the files.download method with
the ID of the file to download and the ID of the revision. For more information,
see Manage long-running operations.
curl
The following curl command initiates a LRO and returns a JSON response. To either download the file or poll this LRO you must make another request using the returned ID to obtain the content URL. Then, you can make a final curl request to that URL to download the file. For more information, see Manage long-running operations.
curl --request POST "https://www.googleapis.com/drive/v3/files/FILE_ID/download?mimeType=MIME_TYPE&revisionId=REVISION_ID" \
--header "Authorization: Bearer ACCESS_TOKEN" \
--header "Content-Length: 0" \
--header "Accept: application/json"
Replace the following:
- FILE_ID: the ID of the file to download.
- MIME_TYPE: the MIME type to export to.
- REVISION_ID: the ID of the revision to download.
- ACCESS_TOKEN: the access token that grants access to the API.