Creates a copy of the specified file. Folders cannot be copied. Try it now or see an example.
Request
HTTP request
POST https://www.googleapis.com/drive/v2/files/fileId/copy
Parameters
Parameter name | Value | Description |
---|---|---|
Path parameters | ||
fileId |
string |
The ID of the file to copy. |
Optional query parameters | ||
convert |
boolean |
Whether to convert this file to the corresponding Docs Editors format.
(Default: false )
|
enforceSingleParent |
boolean |
Deprecated. Copying files into multiple folders is no longer supported. Use shortcuts instead.
(Default: false )
|
includeLabels |
string |
A comma-separated list of IDs of labels to include in the labelInfo part of the response.
|
includePermissionsForView |
string |
Specifies which additional view's permissions to include in the response. Only 'published' is supported. |
ocr |
boolean |
Whether to attempt OCR on .jpg, .png, .gif, or .pdf uploads.
(Default: false )
|
ocrLanguage |
string |
If ocr is true, hints at the language to use. Valid values are BCP 47 codes. |
pinned |
boolean |
Whether to pin the head revision of the new copy. A file can have a maximum of 200 pinned revisions.
(Default: false )
|
supportsAllDrives |
boolean |
Whether the requesting application supports both My Drives and shared drives.
(Default: false )
|
supportsTeamDrives |
boolean |
Deprecated use supportsAllDrives instead.
(Default: false )
|
timedTextLanguage |
string |
The language of the timed text. |
timedTextTrackName |
string |
The timed text track name. |
visibility |
string |
The visibility of the new file. This parameter is only relevant when the source is not a native Google Doc and convert=false.
Acceptable values are:
|
Authorization
This request requires authorization with at least one of the following scopes:
Scope |
---|
https://www.googleapis.com/auth/drive |
https://www.googleapis.com/auth/drive.file |
https://www.googleapis.com/auth/drive.appdata |
https://www.googleapis.com/auth/drive.apps.readonly |
https://www.googleapis.com/auth/drive.photos.readonly |
Some scopes are restricted and require a security assessment for your app to use them. For more information, see the authentication and authorization page.
Request body
In the request body, supply a Files resource with the following properties:
Property name | Value | Description | Notes |
---|---|---|---|
Optional Properties | |||
contentRestrictions[].readOnly |
boolean |
Whether the content of the file is read-only. If a file is read-only, a new revision of the file may not be added, comments may not be added or modified, and the title of the file may not be modified. | writable |
contentRestrictions[].reason |
string |
Reason for why the content of the file is restricted. This is only mutable on requests that also set readOnly=true . |
writable |
copyRequiresWriterPermission |
boolean |
Whether the options to copy, print, or download this file, should be disabled for readers and commenters. | writable |
description |
string |
A short description of the file. | writable |
id |
string |
The ID of the file. | writable |
indexableText.text |
string |
The text to be indexed for this file. | writable |
labels.starred |
boolean |
Whether this file is starred by the user. | writable |
labels.trashed |
boolean |
Whether the file has been trashed, either explicitly or from a trashed parent folder. Only the owner may trash a file. The trashed item is excluded from all files.list responses returned for any user who does not own the file. However, all users with access to the file can see the trashed item metadata in an API response. All users with access can copy, download, export, and share the file. | writable |
labels.viewed |
boolean |
Whether this file has been viewed by this user. | writable |
lastViewedByMeDate |
datetime |
Last time this file was viewed by the user (formatted RFC 3339 timestamp). | writable |
modifiedDate |
datetime |
Last time this file was modified by anyone (formatted RFC 3339 timestamp). This is only mutable on update when the setModifiedDate parameter is set. | writable |
parents[] |
list |
Collection of parent folders which contain this file. If not specified as part of an insert request, the file will be placed directly in the user's My Drive folder. If not specified as part of a copy request, the file will inherit any discoverable parents of the source file. Update requests can also use the |
writable |
properties[] |
list |
The list of properties. | writable |
title |
string |
The title of this file. Note that for immutable items such as the top level folders of shared drives, My Drive root folder, and Application Data folder the title is constant. | writable |
writersCanShare |
boolean |
Whether writers can share the document with other users. Not populated for items in shared drives. | writable |
Response
If successful, this method returns a Files resource in the response body.
Examples
Note: The code examples available for this method do not represent all supported programming languages (see the client libraries page for a list of supported languages).
Java
Uses the Java client library.
import com.google.api.services.drive.Drive; import com.google.api.services.drive.model.File; import java.io.IOException; // ... public class MyClass { // ... /** * Copy an existing file. * * @param service Drive API service instance. * @param originFileId ID of the origin file to copy. * @param copyTitle Title of the copy. * @return The copied file if successful, {@code null} otherwise. */ private static File copyFile(Drive service, String originFileId, String copyTitle) { File copiedFile = new File(); copiedFile.setTitle(copyTitle); try { return service.files().copy(originFileId, copiedFile).execute(); } catch (IOException e) { System.out.println("An error occurred: " + e); } return null; } // ... }
.NET
Uses the .NET client library.
using Google.Apis.Drive.v2; using Google.Apis.Drive.v2.Data; using System.Net; // ... public class MyClass { // ... /// <summary> /// Copy an existing file. /// </summary> /// <param name="service">Drive API service instance.</param> /// <param name="originFileId">ID of the origin file to copy.</param> /// <param name="copyTitle">Title of the copy.</param> /// <returns>The copied file, null is returned if an API error occurred</returns> public static File CopyFile(DriveService service, String originFileId, String copyTitle) { File copiedFile = new File(); copiedFile.Title = copyTitle; try { return service.Files.Copy(copiedFile, originFileId).Execute(); } catch (Exception e) { Console.WriteLine("An error occurred: " + e.Message); } return null; } // ... }
PHP
Uses the PHP client library.
/** * Copy an existing file. * * @param Google_Service_Drive $service Drive API service instance. * @param String $originFileId ID of the origin file to copy. * @param String $copyTitle Title of the copy. * @return DriveFile The copied file. NULL is returned if an API error occurred. */ function copyFile($service, $originFileId, $copyTitle) { $copiedFile = new Google_Service_Drive_DriveFile(); $copiedFile->setTitle($copyTitle); try { return $service->files->copy($originFileId, $copiedFile); } catch (Exception $e) { print "An error occurred: " . $e->getMessage(); } return NULL; }
Python
Uses the Python client library.
from apiclient import errors # ... def copy_file(service, origin_file_id, copy_title): """Copy an existing file. Args: service: Drive API service instance. origin_file_id: ID of the origin file to copy. copy_title: Title of the copy. Returns: The copied file if successful, None otherwise. """ copied_file = {'title': copy_title} try: return service.files().copy( fileId=origin_file_id, body=copied_file).execute() except errors.HttpError, error: print 'An error occurred: %s' % error return None
JavaScript
Uses the JavaScript client library.
/** * Copy an existing file. * * @param {String} originFileId ID of the origin file to copy. * @param {String} copyTitle Title of the copy. */ function copyFile(originFileId, copyTitle) { var body = {'title': copyTitle}; var request = gapi.client.drive.files.copy({ 'fileId': originFileId, 'resource': body }); request.execute(function(resp) { console.log('Copy ID: ' + resp.id); }); }
Go
Uses the Go client library.
import ( "google.golang.org/drive/v2" "fmt" ) // CopyFile copies a file setting a new title on the copy func CopyFile(d *drive.Service, fileId string, title string) (*drive.File, error) { f := &drive.File{Title: title} r, err := d.Files.Copy(fileId, f).Do() if err != nil { fmt.Printf("An error occurred: %v\n", err) return nil, err } return r, nil }
Objective-C
Uses the Objective-C client library.
#import "GTLDrive.h" // ... + (void)copyFileWithService:(GTLServiceDrive *)service originFileId:(NSString *)originFileId copyTitle:(NSString *)copyTitle completionBlock:(void (^)(GTLDriveFile *, NSError *))completionBlock { GTLDriveFile *copiedFile = [GTLDriveFile object]; copiedFile.title = copyTitle; GTLQueryDrive *query = [GTLQueryDrive queryForFilesCopyWithObject:copiedFile fileId:originFileId]; // queryTicket can be used to track the status of the request. GTLServiceTicket *queryTicket = [service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, GTLDriveFile *file, NSError *error) { if (error == nil) { completionBlock(file, nil); } else { NSLog(@"An error occurred: %@", error); completionBlock(nil, error); } }]; } // ...
Try it!
Use the APIs Explorer below to call this method on live data and see the response.