METHODS

new
my $gd_v2 = Net::Google::Drive::Simple::V2->new();

# same as:
my $gd_v2 = Net::Google::Drive::Simple->new( 'version' => 2 );

# same as:
my $gd_v2 = Net::Google::Drive::Simple->new();
my $children = $gd->children( "/path/to" )

Return the entries under a given path on the Google Drive as a reference to an array. Each entry is an object composed of the JSON data returned by the Google Drive API. Each object offers methods named like the fields in the JSON data, e.g. originalFilename(), downloadUrl, etc.

Will return all entries found unless maxResults is set:

my $children = $gd->children( "/path/to", { maxResults => 3 } )

Due to the somewhat capricious ways Google Drive handles its directory structures, the method needs to traverse the path component by component and determine the ID of each directory to get to the next level. To speed up subsequent lookups, it also returns the ID of the last component to the caller:

my( $children, $parent ) = $gd->children( "/path/to" );

If the caller now wants to e.g. insert a file into the directory, its ID is available in $parent.

Each child comes back as a files#resource type and gets mapped into an object that offers access to the various fields via methods:

for my $child ( @$children ) {
    print $child->kind(), " ", $child->title(), "\n";
}

Please refer to

https://developers.google.com/drive/v2/reference/files#resource

for details on which fields are available.

my $files = $gd->files( )

Return all files on the drive as a reference to an array. Will return all entries found unless maxResults is set:

my $files = $gd->files( { maxResults => 3 } )

Note that Google limits the number of entries returned by default to 100, and seems to restrict the maximum number of files returned by a single query to 3,500, even if you specify higher values for maxResults.

Each file comes back as an object that offers access to the Google Drive item's fields, according to the API (see children()).

my $id = $gd->folder_create( "folder-name", $parent_id )

Create a new folder as a child of the folder with the id $parent_id. Returns the ID of the new folder or undef in case of an error.

my $id = $gd->file_create( "folder-name", "mime-type", $parent_id )

Create a new file with the given mime type as a child of the folder with the id $parent_id. Returns the ID of the new file or undef in case of an error.

Example to create an empty google spreadsheet:

my $id = $gd->file_create( "Quarter Results", "application/vnd.google-apps.spreadsheet", "root" );
$gd->file_upload( $file, $dir_id )

Uploads the content of the file $file into the directory with the ID $dir_id on Google Drive. Uses $file as the file name.

To overwrite an existing file on Google Drive, specify the file's ID as an optional parameter:

$gd->file_upload( $file, $dir_id, $file_id );
$gd->rename( $file_id, $name )

Renames the file or folder with $file_id to the specified $name.

$gd->download( $item, [$local_filename] )

Downloads an item found via files() or children(). Also accepts the downloadUrl of an item. If $local_filename is not specified, download() will return the data downloaded (this might be undesirable for large files). If $local_filename is specified, download() will store the downloaded data under the given file name.

my $gd = Net::Google::Drive::Simple->new();
my $files = $gd->files( { maxResults => 20 }, { page => 0 } );
for my $file ( @$files ) {
    my $name = $file->originalFilename();
    print "Downloading $name\n";
    $gd->download( $file, $name ) or die "failed: $!";
}

Be aware that only documents like PDF or png can be downloaded directly. Google Drive Documents like spreadsheets or (text) documents need to be exported into one of the available formats. Check for "exportLinks" on a file given. In case of a document that can be exported you will receive a hash in the form:

{
    'format_1' => 'download_link_1',
    'format_2' => 'download_link_2',
    ...
}

Choose your download link and use it as an argument to the download() function which can also take urls directly.

my $gd = Net::Google::Drive::Simple->new();
my $children = $gd->children( '/path/to/folder/on/google/drive' );
for my $child ( @$children ) {
    if ($child->can( 'exportLinks' )){
        my $type_chosen;
        foreach my $type (keys %{$child->exportLinks()}){
            # Take any type you can get..
            $type_chosen = $type;
            # ..but choose your preferred format, opendocument here:
            last if $type =~/oasis\.opendocument/;
        }
        my $url = $child->exportLinks()->{$type_chosen};

        $gd->download($url, 'my/local/file');

    }
}
my $files = $gd->search( )
my $children= $gd->search({ maxResults => 20 },{ page => 0 },
                          "title contains 'Futurama'");

Search files for attributes. See https://developers.google.com/drive/web/search-parameters for a definition of the attributes.

To list all available files, those on the drive, those directly shared with the user, and those generally available to the user, use an empty search:

my $children= $gd->search({},{ page => 0 },"");
$gd->file_delete( file_id )

Delete the file with the specified ID from Google Drive.

$gd->drive_mvdir( "/gdrive/path/to/file", "/path/to/new/folder" )

Move an existing file to a new folder. Removes the file's "parent" setting (pointing to the old folder) and then adds the new folder as a new parent.

children_by_folder_id
file_mvdir
http_delete
http_put

LEGALESE

Copyright 2012-2019 by Mike Schilli, all rights reserved. This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

2019, Nicolas R. <cpan@atoomic.org> 2012-2019, Mike Schilli <cpan@perlmeister.com>