I want to export/transfer files to Google drive using Magento 2. Please help me out.


You can implement following code in your Magento 2 custom extension. Note that you need to convert it according to Magento 2 extension structure and coding standard.



Which file? When you want to upload file on google drive? Is there any specific case ?

So basically you need to use PHP code only to export to the google drive. You need Magento logic just to get the input for google drive API. (File Export)

If there is any specific case like once customer place the order some file has to upload to google drive then you can use Plugins/Observers.


If any some other case you should first find out the place where you can be added the PHP code in the respective controller/Block/Helper.



Assuming you have a CSV file to upload : Reference

$fileMetadata = new Google_Service_Drive_DriveFile(array(
    'name' => 'My Report',
    'mimeType' => 'application/vnd.google-apps.spreadsheet'));
$content = file_get_contents('files/report.csv');
$file = $driveService->files->create($fileMetadata, array(
    'data' => $content,
    'mimeType' => 'text/csv',
    'uploadType' => 'multipart',
    'fields' => 'id'));
printf("File ID: %s\n", $file->id);

About Google_Service_Drive_DriveFile

  • Yes, when the customer places an order an export file should be transferred to Google drive. For file export, I have the code but I need the code to transfer that's it. Could you pls share the test code for files transfer? Feb 13 '19 at 9:27
  • Check my updated answer. Feb 13 '19 at 9:41
  • hey, where we have to give client id and client secret key etc., Feb 13 '19 at 9:51
  • Have a look at Step 1 here- developers.google.com/drive/api/v3/quickstart/php Feb 13 '19 at 10:15

The Class Api I developed:

 public function append( $contact)
    $client = $this->getClient();
    $service = new \Google_Service_Sheets($client);
    $range = date('Y-m', time());
    $spreadsheetId= $this->_dataHelper->getContactFormSheetId();
    try {
        return $this->appendRow($service, $spreadsheetId, $range, $contact);
    } catch (\Exception $ex) {
        $body = new \Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array(
            'requests' => array(
                'addSheet' => array(
                    'properties' => array(
                        'title' => $range
        $service->spreadsheets->batchUpdate($spreadsheetId, $body);
        return $this->appendRow($service, $spreadsheetId, $range, $contact);
private function appendRow($service, $spreadsheetId, $range, $contact)
    // Create the value range Object
    $valueRange = new \Google_Service_Sheets_ValueRange();
    $valueRange->setValues(["values" => $contact]); // Add two values
    $conf = ["valueInputOption" => "RAW"];
    return $service->spreadsheets_values->append($spreadsheetId, $range, $valueRange, $conf);
private function getClient()
    if (!$this->_client) {
        $credential = $this->_dataHelper->getCredential();
        $client = new \Google_Client();
        //Set Service account key that config in project
        $this->_client = $client;
    return $this->_client;

You should install google/apiclient:v2.0.0 library

I found the link that guide you start develop new module with full function you need.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.