Adds a property to a file, or updates it if it already exists. Try it now or see an example.
Request
HTTP request
POST https://www.googleapis.com/drive/v2/files/fileId/properties
Parameters
Parameter name | Value | Description |
---|---|---|
Path parameters | ||
fileId |
string |
The ID of the file. |
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.metadata |
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 Properties resource with the following properties:
Property name | Value | Description | Notes |
---|---|---|---|
Required Properties | |||
key |
string |
The key of this property. | writable |
Optional Properties | |||
value |
string |
The value of this property. | writable |
visibility |
string |
The visibility of this property. Allowed values are PRIVATE and PUBLIC. (Default: PRIVATE). Private properties can only be retrieved using an authenticated request. An authenticated request uses an access token obtained with a OAuth 2 client ID. You cannot use an API key to retrieve private properties. | writable |
Response
If successful, this method returns a Properties 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.Property; import java.io.IOException; // ... public class MyClass { // ... /** * Insert a new custom file property. * * @param service Drive API service instance. * @param fileId ID of the file to insert property for. * @param key ID of the property. * @param value Property value. * @param visibility 'PUBLIC' to make the property visible by all apps, * or 'PRIVATE' to make it only available to the app that created it. * @return The inserted custom file property if successful, {@code null} otherwise. */ private static Property insertProperty( Drive service, String fileId, String key, String value, String visibility) { Property newProperty = new Property(); newProperty.setKey(key); newProperty.setValue(value); newProperty.setVisibility(visibility); try { return service.properties().insert(fileId, newProperty).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> /// Insert a new custom file property. /// </summary> /// <param name="service">Drive API service instance.</param> /// <param name="fileId">ID of the file to insert property for.</param> /// <param name="key">ID of the property.</param> /// <param name="value">Property value.</param> /// <param name="visibility">'PUBLIC' to make the property visible by all apps, /// or 'PRIVATE' to make it only available to the app that created it. /// </param> /// <returns>The inserted custom file property, null is returned if an API error occurred</returns> public static Property InsertProperty(DriveService service, String fileId, String key, String value, String visibility) { Property newProperty = new Property(); newProperty.Key = key; newProperty.Value = value; newProperty.Visibility = visibility; try { return service.Properties.Insert(newProperty, fileId).Execute(); } catch (Exception e) { Console.WriteLine("An error occurred: " + e.Message); } return null; } // ... }
PHP
Uses the PHP client library.
/** * Insert a new custom file property. * * @param Google_Service_Drive $service Drive API service instance. * @param String $fileId ID of the file to insert property for. * @param String $key ID of the property. * @param String $value Property value. * @param String $visibility 'PUBLIC' to make the property visible by all apps, * or 'PRIVATE' to make it only available to the app that created it. * @return Google_Servie_Drive_Property The inserted property. NULL is returned * if an API error occurred. */ function insertProperty($service, $fileId, $key, $value, $visibility) { $newProperty = new Google_Service_Drive_Property(); $newProperty->setKey($key); $newProperty->setValue($value); $newProperty->setVisibility($visibility); try { return $service->properties->insert($fileId, $newProperty); } catch (Exception $e) { print "An error occurred: " . $e->getMessage(); } return NULL; }
Python
Uses the Python client library.
def insert_property(service, file_id, key, value, visibility): """Insert new custom file property. Args: service: Drive API service instance. file_id: ID of the file to insert property for. key: ID of the property. value: Property value. visibility: 'PUBLIC' to make the property visible by all apps, or 'PRIVATE' to make it only available to the app that created it. Returns: Inserted custom file property if successful, None otherwise. """ body = { 'key': key, 'value': value, 'visibility': visibility } try: p = service.properties().insert( fileId=file_id, body=body).execute() return p except errors.HttpError, error: print 'An error occurred: %s' % error return None
JavaScript
Uses the JavaScript client library.
/** * Insert a new custom file property. * * @param {String} fileId ID of the file to insert property for. * @param {String} key ID of the property. * @param {String} value Property value. * @param {String} visibility 'PUBLIC' to make the property visible by all apps, * or 'PRIVATE' to make it only available to the app that created it. */ function insertProperty(fileId, key, value, visibility) { var body = { 'key': key, 'value': value, 'visibility': visibility } var request = gapi.client.drive.properties.insert({ 'fileId': fileId, 'resource': body }); request.execute(function(resp) { }); }
Try it!
Use the APIs Explorer below to call this method on live data and see the response.