The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Net::Google::Drive - simple Google drive API module

SYNOPSIS

This module use to upload, download, share file on Google drive use Net::Google::Drive;

#Create disk object. You need send in param 'access_token', 'refresh_token', 'client_id' and 'client_secret'. 
#Values of 'client_id' and 'client_secret' uses to create Net::Google::OAuth object so that update value of 'access_token'.
my $disk = Net::Google::Drive->new(
                                    -client_id          => $client_id,
                                    -client_secret      => $client_secret,
                                    -access_token       => $access_token,
                                    -refresh_token      => $refresh_token,
                                    );

# Search file by name
my $file_name = 'upload.doc';
my $files = $disk->searchFileByName( -filename => $file_name ) or croak "File '$file_name' not found";
my $file_id = $files->[0]->{id};
print "File id: $file_id\n";

#Download file
my $dest_file = '/home/upload.doc';
$disk->downloadFile(
                        -file_id        => $file_id,
                        -dest_file      => $dest_file,
                        );

#Upload file
my $source_file = '/home/upload.doc';
my $res = $disk->uploadFile( -source_file   => $source_file );
print "File: $source_file uploaded. File id: $res->{id}\n";

METHODS

new(%opt)

Create Net::Google::Disk object

%opt:
    -client_id          => Your app client id (Get from google when register your app)
    -client_secret      => Your app client secret (Get from google when register your app)
    -access_token       => Access token value (Get from L<Net::Google::OAuth>)
    -refresh_token      => Refresh token value (Get from L<Net::Google::OAuth>)

searchFileByName(%opt)

Search file on google disk by name. Return arrayref to info with found files. If files not found - return empty arrayref

 %opt:
     -filename           => Name of file to find
 Return:
[
     [0] {
         id         "1f13sLfo6UEyUuFpn-NWPnY",
         kind       "drive#file",
         mimeType   "application/x-perl",
         name       "drive.t"
     }
 ]
 

searchFileByNameContains(%opt)

Search files on google drive by name contains value in param '-filename' Param and return value same as in method searchFileByName

downloadFile(%opt)

Download file from google dist to -dest_file on local system. Return 1 if success, die in otherwise

%opt: 
    -dest_file          => Name of file on disk in which you will download file from google disk
    -file_id            => Id of file on google disk

deleteFile(%opt)

Delete file from google disk. Return 1 if success, die in otherwise

%opt: 
    -file_id            => Id of file on google disk

uploadFile(%opt)

Upload file from local system to google drive. Return file_info hashref if success, die in otherwise

%opt:
    -source_file        => File on local system
    -parents            => Optional arrayref of parent ids
Return:
   {
        id         "1LVAr2PpqX9m314JyZ6YJ4v_KIzG0Gey2",
        kind       "drive#file",
        mimeType   "application/octet-stream",
        name       "gogle_upload_file"
   }

setFilePermission(%opt)

Set permissions for file on google drive. Return permission hashref, die in otherwise

%opt:
    -file_id            => Id of file on google disk
    -type               => The type of the grantee. Valid values are: (user, group, domain, anyone)
    -role               => The role granted by this permission. Valid values are: (owner, organizer, fileOrganizer, writer, commenter, reader)
Return:
    {
        allowFileDiscovery   JSON::PP::Boolean  {
            Parents       Types::Serialiser::BooleanBase
            public methods (0)
            private methods (0)
            internals: 0
        },
        id                   "anyoneWithLink",
        kind                 "drive#permission",
        role                 "reader",
        type                 "anyone"
    }

getFileMetadata(%opt)

Get metadata of file. Return hashref with metadata if success, die in otherwise

%opt: 
    -file_id            => Id of file on google disk
Return:
        {                                                                                                                                                         alternateLink                  "https://drive.google.com/file/d/10Z5YDCHn3gnj0S4_Lf0poc2Lm5so0Sut/view?usp=drivesdk",
        appDataContents                JSON::PP::Boolean  {
            Parents       Types::Serialiser::BooleanBase
            public methods (0)
            private methods (0)
            internals: 0
        },
        capabilities                   {
            canCopy   JSON::PP::Boolean  {
                Parents       Types::Serialiser::BooleanBase
                public methods (0)
                private methods (0)
                internals: 1
            },
            canEdit   var{capabilities}{canCopy}
        },
        copyable                       var{capabilities}{canCopy},
        copyRequiresWriterPermission   var{appDataContents},
        createdDate                    "2018-10-04T12:05:15.896Z",
        downloadUrl                    "https://doc-0g-7o-docs.googleusercontent.com/docs/securesc/ck8i7vfbvef13kb30b8mkrcjv4ihp2uj/3mfn1kbr655euhlo7tctg5mmn8oirg
    gf/1538654400000/10526805100525201667/10526805100525201667/10Z5YDCHn3gnj0S4_Lf0poc2Lm5so0Sut?e=download&gd=true",
        editable                       var{capabilities}{canCopy},
        embedLink                      "https://drive.google.com/file/d/10Z5YDCHn3gnj0S4_Lf0poc2Lm5so0Sut/preview?usp=drivesdk",
        etag                           ""omwGuTP8OdxhZkubyp-j43cFdJQ/MTUzODY1NDcxNTg5Ng"",
        explicitlyTrashed              var{appDataContents},
        fileExtension                  "",
        fileSize                       1000000,
        headRevisionId                 "0B4HgPHxdPy22UmZXSFVRTkRLbXhFakdzZjFSUGkrNWZIVFN3PQ",
        iconLink                       "https://drive-thirdparty.googleusercontent.com/16/type/application/octet-stream",
        id                             "10Z5YDCHn3gnj0S4_Lf0poc2Lm5so0Sut",
        kind                           "drive#file",
        labels                         {
            hidden       var{appDataContents},
            restricted   var{appDataContents},
            starred      var{appDataContents},
            trashed      var{appDataContents},
            viewed       var{appDataContents}
        },
        lastModifyingUser              {
                displayName           "Ларри Уолл",
                emailAddress          "perlgogledrivemodule@gmail.com",
                isAuthenticatedUser   var{capabilities}{canCopy},
                kind                  "drive#user",
                permissionId          10526805100525201667
        },
        lastModifyingUserName          "Ларри Уолл",
        markedViewedByMeDate           "1970-01-01T00:00:00.000Z",
        md5Checksum                    "ded2a2983b3e1743152d8224549510e1",
        mimeType                       "application/octet-stream",
        modifiedByMeDate               "2018-10-04T12:05:15.896Z",
        modifiedDate                   "2018-10-04T12:05:15.896Z",
        originalFilename               "gogle_upload_file",
        ownerNames                     [
            [0] "Ларри Уолл"
        ],
        owners                         [
            [0] {
                displayName           "Ларри Уолл",
                emailAddress          "perlgogledrivemodule@gmail.com",
                isAuthenticatedUser   var{capabilities}{canCopy},
                kind                  "drive#user",
                permissionId          10526805100525201667
            }
        ],
        parents                        [
            [0] {
                id           "0AIHgPHxdPy22Uk9PVA",
                isRoot       var{capabilities}{canCopy},
                kind         "drive#parentReference",
                parentLink   "https://www.googleapis.com/drive/v2/files/0AIHgPHxdPy22Uk9PVA",
                selfLink     "https://www.googleapis.com/drive/v2/files/10Z5YDCHn3gnj0S4_Lf0poc2Lm5so0Sut/parents/0AIHgPHxdPy22Uk9PVA"
            }
        ],
        quotaBytesUsed                 1000000,
        selfLink                       "https://www.googleapis.com/drive/v2/files/10Z5YDCHn3gnj0S4_Lf0poc2Lm5so0Sut",
        shared                         var{appDataContents},
        spaces                         [
            [0] "drive"
        ],
        title                          "gogle_upload_file",
        userPermission                 {
            etag       ""omwGuTP8OdxhZkubyp-j43cFdJQ/N52l-iUAo-dARaTch8nQXOzl348"",
            id         "me",
            kind       "drive#permission",
            role       "owner",
            selfLink   "https://www.googleapis.com/drive/v2/files/10Z5YDCHn3gnj0S4_Lf0poc2Lm5so0Sut/permissions/me",
            type       "user"
        },
        version                        2,
        webContentLink                 "https://drive.google.com/uc?id=10Z5YDCHn3gnj0S4_Lf0poc2Lm5so0Sut&export=download",
        writersCanShare                var{capabilities}{canCopy}
    }

shareFile(%opt)

Share file for download. Return download link if success, die in otherwise

%opt: 
    -file_id            => Id of file on google disk

DEPENDENCE

Net::Google::OAuth, LWP::UserAgent, JSON::XS, URI, HTTP::Request, File::Basename

AUTHORS

  • Pavel Andryushin <vrag867@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2018 by Pavel Andryushin.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.