Last active
February 23, 2018 15:04
-
-
Save peinwag/73c08c23dadabfdb3fbb14e0e000c924 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__ . '/vendor/autoload.php'; | |
$type = 'pdf'; | |
if (isset($argv[1]) && $argv[1] == 'html') { | |
$type = 'html'; | |
} | |
// Step1: https://developers.google.com/identity/protocols/OAuth2ServiceAccount => create service account | |
// Step2: https://console.developers.google.com/apis/api/drive.googleapis.com/overview?project=801063020741 => enable drive api | |
// Step3: Share folder with service account | |
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/secret/key.json'); | |
define( | |
'SCOPES', | |
implode( | |
' ', | |
[ | |
Google_Service_Drive::DRIVE | |
] | |
) | |
); | |
$client = new Google_Client(); | |
$client->useApplicationDefaultCredentials(); | |
$client->setScopes(SCOPES); | |
$service = new Google_Service_Drive($client); | |
$optParams = [ | |
'pageSize' => 10, | |
'fields' => 'nextPageToken, files(id, name)', | |
'q' => 'name ="AGB_TEST_SPIKE_DE"' | |
]; | |
$results = $service->files->listFiles($optParams); | |
if (count($results->getFiles()) == 0) { | |
print "No files found.\n"; | |
} else { | |
foreach ($results->getFiles() as $file) { | |
/* @var $file Google_Service_Drive_DriveFile */ | |
if ($type == 'html') { | |
$response = $service->files->export( | |
$file->getId(), | |
'text/html', | |
[ | |
'alt' => 'media' | |
] | |
); | |
$content = $response->getBody()->getContents(); | |
echo $content; | |
} else { | |
$response = $service->files->export( | |
$file->getId(), | |
'application/pdf', | |
[ | |
'alt' => 'media' | |
] | |
); | |
$content = $response->getBody()->getContents(); | |
// move to s3 | |
$s3 = new Aws\S3\S3Client( | |
[ | |
'version' => 'latest', | |
'region' => 'eu-central-1', | |
] | |
); | |
$result = $s3->putObject( | |
[ | |
'ACL' => 'public-read', | |
'ContentType' => 'application/pdf', | |
'Bucket' => 'spike-pe', | |
'Key' => 'AGB_TEST_SPIKE_DE.pdf', | |
'Body' => $content | |
] | |
); | |
echo $result['ObjectURL']; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment