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.
-
Hi @Kishan thanks for responding quickly and there are first two lines
require_once 'Google/Client.php'; require_once 'Google/Service/Drive.php';
How to include this things in Magento 2 – gajjala sandeep Feb 13 '19 at 9:22 -
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
.
Or
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
.
https://developers.google.com/drive/api/v3/quickstart/php
Updated:
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);
-
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? – gajjala sandeep Feb 13 '19 at 9:27
-
-
hey, where we have to give client id and client secret key etc., – gajjala sandeep Feb 13 '19 at 9:51
-
Have a look at Step 1 here- developers.google.com/drive/api/v3/quickstart/php – Akhil Gupta 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();
$client->setScopes([
\Google_Service_Sheets::SPREADSHEETS,
]);
//Set Service account key that config in project
//https://console.cloud.google.com/iam-admin/serviceaccounts/details/101147339843886042602;edit=true?previousPage=%2Fapis%2Fcredentials%3Fproject%3Dsuntory-wellness-1594265938326&project=suntory-wellness-1594265938326
$client->setAuthConfig($credential);
$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.
https://khoaln.com/blog/how-to-import-data-from-magento-to-google-spreadsheet