Created
October 1, 2022 09:20
-
-
Save necmettin/dc85605a12164783dd8c44de8b29a5a1 to your computer and use it in GitHub Desktop.
Downloads cloudinary files, 300 at a time
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 | |
use Cloudinary\Configuration\Configuration; | |
use Cloudinary\Api\Admin\AdminApi; | |
function download_cloudinary_files(string $cloud_name, string $api_key, string $api_secret): void { | |
global $App; | |
$instance = Configuration::instance(['cloud'=>[ | |
'cloud_name'=>$cloud_name, | |
'api_key'=>$api_key, | |
'api_secret'=>$api_secret, | |
]]); | |
$api = new AdminApi(); | |
$list = $api->assets(['max_results'=>300]); | |
foreach ($list as $elements) { | |
foreach ($elements as $elem) { | |
$fn = $elem['public_id'] . "." . $elem['format']; | |
file_put_contents($fn, file_get_contents($elem['url'])); | |
if (filesize($fn) !== $elem['bytes']) { | |
echo "incomplete download: $fn"; | |
} else { | |
echo "done: $fn"; | |
try { | |
$api->deleteAssets($elem['public_id']); | |
} catch (Exception $exception) { | |
var_dump($exception); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment