Skip to content

Instantly share code, notes, and snippets.

@peinwag
Last active February 23, 2018 15:04
Show Gist options
  • Save peinwag/73c08c23dadabfdb3fbb14e0e000c924 to your computer and use it in GitHub Desktop.
Save peinwag/73c08c23dadabfdb3fbb14e0e000c924 to your computer and use it in GitHub Desktop.
<?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