Advertisement
stephondoestech

Reddit Upload Script

Jun 16th, 2025 (edited)
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.49 KB | Source Code | 0 0
  1. <?php
  2. require('aws-autoloader.php');
  3.  
  4. define('AccessKey', '[REDACTED]');
  5. define('SecretKey', '[REDACTED]');
  6. define('HOST', '[REDACTED]');
  7. define('REGION', '[REDACTED]');
  8.  
  9. use Aws\S3\S3Client;
  10. use Aws\Exception\AwsException;
  11. use Aws\S3\MultipartUploader;
  12. use Aws\S3\Exception\MultipartUploadException;
  13.  
  14. // Establish connection with an S3 client.
  15. $client = new S3Client([
  16.     'endpoint' => HOST,
  17.     'region' => REGION,
  18.     'version' => 'latest',
  19.     'credentials' => [
  20.         'key' => AccessKey,
  21.         'secret' => SecretKey,
  22.     ],
  23. ]);
  24.  
  25. class FlxZipArchive extends ZipArchive
  26. {
  27.     public function addDir($location, $name)
  28.     {
  29.         $this->addEmptyDir($name);
  30.         $this->addDirDo($location, $name);
  31.     }
  32.  
  33.     private function addDirDo($location, $name)
  34.     {
  35.         $name .= '/';
  36.         $location .= '/';
  37.         $dir = opendir($location);
  38.  
  39.         while (($file = readdir($dir)) !== false)
  40.         {
  41.             if ($file === '.' || $file === '..') continue;
  42.  
  43.             $fullPath = $location . $file;
  44.  
  45.             if (is_link($fullPath)) {
  46.                 // Skip symlinks
  47.                 echo "Skipping symlink: $fullPath\n";
  48.                 continue;
  49.             }
  50.  
  51.             if (is_dir($fullPath)) {
  52.                 $this->addDir($fullPath, $name . $file);
  53.             } else {
  54.                 $this->addFile($fullPath, $name . $file);
  55.             }
  56.         }
  57.  
  58.         closedir($dir);
  59.     }
  60. }
  61.  
  62. // Create a date time to use for a filename
  63. $date = new DateTime('now');
  64. $filetime = $date->format('Y-m-d-H:i:s');
  65.  
  66. $the_folder = '/home/my_folder';
  67. $zip_file_name = '/home/my_folder/aws/zipped-files-' . $filetime . '.zip';
  68.  
  69. ini_set('memory_limit', '2048M');
  70. echo "Memory limit: " . ini_get('memory_limit') . "\n";
  71.  
  72. $za = new FlxZipArchive;
  73. $res = $za->open($zip_file_name, ZipArchive::CREATE);
  74. if ($res === TRUE) {
  75.     $za->addDir($the_folder, basename($the_folder));
  76.     echo 'Successfully created a zip folder' . PHP_EOL;
  77.     $za->close();
  78. } else {
  79.     echo 'Could not create a zip archive' . PHP_EOL;
  80. }
  81.  
  82. // Push it to the cloud
  83. $key = 'filesbackups/mysite-files-' . $filetime . '.zip';
  84. $source_file = $zip_file_name;
  85. $bucket = 'backupbucket';
  86.  
  87. $uploader = new MultipartUploader($client, $source_file, [
  88.     'bucket' => $bucket,
  89.     'key' => $key
  90. ]);
  91.  
  92. try {
  93.     $result = $uploader->upload();
  94.     echo "Upload complete: {$result['ObjectURL']}" . PHP_EOL;
  95. } catch (MultipartUploadException $e) {
  96.     echo $e->getMessage() . PHP_EOL;
  97. }
  98. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement