This guide explains how to create and manage files in Google Drive using the Google Drive API.
Create file
To create a file in Drive that contains no metadata or content,
use the create
method on the files
resource with no parameters.
When you create the file, the method returns a files
resource. The file is
given a kind
of drive.file
, an id
, a name
of "Untitled", and a
mimeType
of application/octet-stream
. The
uploadType
is marked as required but defaults to media
, so you don't actually have to
supply it.
For more information about Drive file limits, see File and folder limits.
Use the fields parameter
If you want to specify the fields to return in the response, you can set the
fields
system
parameter
with any method of the files
resource. If you omit the fields
parameter, the
server returns a default set of fields specific to the method. For example, the
list
method returns only the kind
, id
,
name
, mimeType
, and resourceKey
fields for each file. To return different
fields, see Return specific fields.
Generate IDs to use with your files
The generateIds
method on the
files
resource lets you pre-generate unique file
IDs that can be used when creating or copying files and folders in
Drive. This can be useful when you need to control the file IDs
from your app, rather than letting Drive assign them
automatically.
You can set the number of IDs generated using the
count
query parameter.
If count
is not set, 10 are returned by default. The maximum number of IDs you
can request is capped at 1,000.
You can also designate the
space
in which the IDs
can be used and the
type
of items which the
IDs can be used for.
Once an ID is generated, it can be passed to the create
or copy
method
through the id
field. This ensures that the created or copied file uses the
predetermined ID.
If the file is successfully created or copied, subsequent retries return a 409
Conflict
HTTP status code response and duplicate files aren't created.
Note that pre-generated IDs aren't supported for the creation of
Google Workspace files, except for the application/vnd.google-apps.drive-sdk
and application/vnd.google-apps.folder
MIME
types. Similarly, uploads referencing a conversion
to a Google Workspace file format aren't supported.
Create metadata-only files
Metadata-only files contain no content. Metadata is data (such as name
,
mimeType
, and createdTime
) that describes the file. Fields like name
are
user-agnostic and appear the same for each user, whereas fields such as
viewedByMeTime
contain user-specific values.
One example of a metadata-only file is a folder with the MIME type
application/vnd.google-apps.folder
. For more information, see Create and
populate folders. Another example is a shortcut that
points to another file on Drive with the MIME type
application/vnd.google-apps.shortcut
. For more information, see Create a
shortcut to a Drive file.
Manage thumbnail images
Thumbnails help users identify Drive files. Drive can automatically generate thumbnails for common file types or you can provide a thumbnail image generated by your app. For more information, see Upload thumbnails.
Copy an existing file
To copy a file, and apply any requested updates, use the copy
method on the files
resource. To find the
fileId
to copy, use the list
method.
You can apply updates through patch semantics, meaning you can make partial modifications to a resource. You must explicitly set the fields that you intend to modify in your request. Any fields not included in the request retain their existing values. For more information, see Working with partial resources.
You can pre-set the file ID of the copied file using the generateIds
method. For more information, see
Generate IDs to use with your files.
Note that you need to use an appropriate Drive API scope to authorize the call. For more information on Drive scopes, see Choose Google Drive API scopes.
Limits and considerations
As you prepare to copy files, take note of these limits and considerations:
Permissions:
- The
DownloadRestrictionsMetadata
object of thefiles
resource determines who can copy the file. For more information, see Prevent users from downloading, printing, or copying your file. - The
capabilities.canCopy
field resource determines whether the user can copy a file. For more information, see Understand file capabilities. - The user that created the copy owns the copied file. No other sharing settings from the source file are replicated. If the copy is created in a shared folder, it inherits the permissions of that folder.
- A copied file's ownership might change and the copy might not inherit the original file's sharing settings. These settings might need to be reset.
- The
File management:
- Some files, like third-party shortcuts, can never be copied.
- You can only copy a file into one parent folder. Specifying multiple
parents isn't supported. If the
parents
field isn't specified, the file inherits any discoverable parents from the source file. - Even though a folder is a type of file, you can't copy a folder.
Instead, create a destination folder and set the
parents
field of the existing files to the destination folder. You can then delete the original source folder. - Unless a new filename is specified, the
copy
method produces a file with the same name as the original. - Excessive use of
copy
can lead to exceeding your Drive API quota limits. For more information, see Usage limits.
Related topics
Here are a few next steps you might try:
To upload file data when you create or update a file, see Upload file data.
To create a file in a specific folder, see Create a file in a specific folder.
To move files, see Move files between folders.
To work with file metadata, see Manage file metadata.
To delete a file, see Trash or delete files and folders.