Storing Application Data

The Google Drive API includes a special hidden folder that your app can use to store application data.

What is the Application Data folder?

The 'Application Data folder' is a special folder that is only accessible by your application. Its content is hidden from the user, and from other apps. Despite being hidden from the user, the Application Data folder is stored on the user's Drive and therefore uses the user's Drive storage quota. The Application Data folder can be used to store configuration files, saved games data, or any other types of files that the user should not directly interact with.

Even though users can't see the Application Data folder's content, they can see the amount of storage used by your app’s Application Data Folder in the Manage Apps dialog:

Manage apps dialog showing Application Data folder size

Your Application Data folder is deleted if users uninstall your app from their Drive. They can also delete your app's data folder manually.

Get authorization to use the Application Data folder

To be able to use your Application Data folder, request access to the following scope:

https://www.googleapis.com/auth/drive.appdata

For more information about scopes and how to request access to them, see Authorizing Your App with Google Drive.

Working with the Application Data folder

The application specific folder is accessible using the alias appDataFolder. This alias can be used like any other folder ID; for instance, when listing the files located in your application's folder or when creating files by listing appDataFolder as a parent.

Much of the information you'll need to get started, including client library examples, is detailed in the Files reference.

Inserting a file into the Application Data folder

Applications can use the following code snippet to store a new file in the Application Data folder:

Java

File fileMetadata = new File();
fileMetadata.setName("config.json");
fileMetadata.setParents(Collections.singletonList("appDataFolder"));
java.io.File filePath = new java.io.File("files/config.json");
FileContent mediaContent = new FileContent("application/json", filePath);
File file = driveService.files().create(fileMetadata, mediaContent)
        .setFields("id")
        .execute();
System.out.println("File ID: " + file.getId());

Python

file_metadata = {
    'name' : 'config.json',
    'parents': [ 'appDataFolder']
}
media = MediaFileUpload('files/config.json',
                        mimetype='application/json',
                        resumable=True)
file = drive_service.files().create(body=file_metadata,
                                    media_body=media,
                                    fields='id').execute()
print 'File ID: %s' % file.get('id')

PHP

$fileMetadata = new Google_Service_Drive_DriveFile(array(
  'name' => 'config.json',
  'parents' => array('appDataFolder')
));
$content = file_get_contents('files/config.json');
$file = $driveService->files->create($fileMetadata, array(
  'data' => $content,
  'mimeType' => 'application/json',
  'uploadType' => 'multipart',
  'fields' => 'id'));
printf("File ID: %s\n", $file->id);

.NET

var fileMetadata = new File();
fileMetadata.Name = "config.json";
fileMetadata.Parents = new List<string>() { "appDataFolder" };
FilesResource.CreateMediaUpload request;
using (var stream = new System.IO.FileStream("files/config.json", 
    System.IO.FileMode.Open))
{
    request = driveService.Files.Create(
        fileMetadata, stream, "application/json");
    request.Fields = "id";
    request.Upload();
}
var file = request.ResponseBody;
Console.WriteLine("File ID: " + file.Id);

Ruby

file_metadata = {
  name: 'config.json',
  parents: [ 'appDataFolder']
}
file = drive_service.create_file(file_metadata,
                                 fields: 'id',
                                 upload_source: 'files/config.json',
                                 content_type: 'application/json')
puts "File Id: #{file.id}"

Node.js

var fileMetadata = {
  'name': 'config.json',
  'parents': [ 'appDataFolder']
};
var media = {
  mimeType: 'application/json',
  body: fs.createReadStream('files/config.json')
};
drive.files.create({
   resource: fileMetadata,
   media: media,
   fields: 'id'
}, function(err, file) {
  if(err) {
    // Handle error
    console.log(err);
  } else {
    console.log("Folder Id: ", file.id);
  }
});

Listing files from the Application Data folder

The following code snippets shows how to programmatically list all files stored in the Application Data folder:

Java

FileList files = driveService.files().list()
        .setSpaces("appDataFolder")
        .setFields("nextPageToken, files(id, name)")
        .setPageSize(10)
        .execute();
for(File file: files.getFiles()) {
    System.out.printf("Found file: %s (%s)\n",
            file.getName(), file.getId());
}

Python

response = drive_service.files().list(spaces='appDataFolder',
                                     fields='nextPageToken, files(id, name)',
                                     pageSize=10).execute()
for file in response.get('files', []):
    # Process change
    print 'Found file: %s (%s)' % (file.get('name'), file.get('id'))

PHP

$response = $driveService->files->listFiles(array(
  'spaces' => 'appDataFolder',
  'fields' => 'nextPageToken, files(id, name)',
  'pageSize' => 10
));
foreach ($response->files as $file) {
    printf("Found file: %s (%s)", $file->name, $file->id);
}

.NET

var request = driveService.Files.List();
request.Spaces = "appDataFolder";
request.Fields = "nextPageToken, files(id, name)";
request.PageSize = 10;
var result = request.Execute();
foreach (var file in result.Files)
{
    Console.WriteLine(String.Format(
        "Found file: %s (%s)", file.Name, file.Id));
}

Ruby

response = drive_service.list_files(spaces: 'appDataFolder',
                                    fields:'nextPageToken, files(id, name)',
                                    page_size: 10)
for file in response.files
  # Process change
  puts "Found file: #{file.name} #{file.id}"
end

Node.js

drive.files.list({
  spaces: 'appDataFolder',
  fields: 'nextPageToken, files(id, name)',
  pageSize: 100
}, function(err, res) {
  if(err) {
    // Handle error
    console.log(err);
  } else {
    res.files.forEach(function(file) {
      console.log('Found file: ', file.name, file.id);
    });
  }
});

Send feedback about...

Drive REST API
Drive REST API