Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require('aws-autoloader.php');
- define('AccessKey', '[REDACTED]');
- define('SecretKey', '[REDACTED]');
- define('HOST', '[REDACTED]');
- define('REGION', '[REDACTED]');
- use Aws\S3\S3Client;
- use Aws\Exception\AwsException;
- use Aws\S3\MultipartUploader;
- use Aws\S3\Exception\MultipartUploadException;
- // Establish connection with an S3 client.
- $client = new S3Client([
- 'endpoint' => HOST,
- 'region' => REGION,
- 'version' => 'latest',
- 'credentials' => [
- 'key' => AccessKey,
- 'secret' => SecretKey,
- ],
- ]);
- class FlxZipArchive extends ZipArchive
- {
- public function addDir($location, $name)
- {
- $this->addEmptyDir($name);
- $this->addDirDo($location, $name);
- }
- private function addDirDo($location, $name)
- {
- $name .= '/';
- $location .= '/';
- $dir = opendir($location);
- while (($file = readdir($dir)) !== false)
- {
- if ($file === '.' || $file === '..') continue;
- $fullPath = $location . $file;
- if (is_link($fullPath)) {
- // Skip symlinks
- echo "Skipping symlink: $fullPath\n";
- continue;
- }
- if (is_dir($fullPath)) {
- $this->addDir($fullPath, $name . $file);
- } else {
- $this->addFile($fullPath, $name . $file);
- }
- }
- closedir($dir);
- }
- }
- // Create a date time to use for a filename
- $date = new DateTime('now');
- $filetime = $date->format('Y-m-d-H:i:s');
- $the_folder = '/home/my_folder';
- $zip_file_name = '/home/my_folder/aws/zipped-files-' . $filetime . '.zip';
- ini_set('memory_limit', '2048M');
- echo "Memory limit: " . ini_get('memory_limit') . "\n";
- $za = new FlxZipArchive;
- $res = $za->open($zip_file_name, ZipArchive::CREATE);
- if ($res === TRUE) {
- $za->addDir($the_folder, basename($the_folder));
- echo 'Successfully created a zip folder' . PHP_EOL;
- $za->close();
- } else {
- echo 'Could not create a zip archive' . PHP_EOL;
- }
- // Push it to the cloud
- $key = 'filesbackups/mysite-files-' . $filetime . '.zip';
- $source_file = $zip_file_name;
- $bucket = 'backupbucket';
- $uploader = new MultipartUploader($client, $source_file, [
- 'bucket' => $bucket,
- 'key' => $key
- ]);
- try {
- $result = $uploader->upload();
- echo "Upload complete: {$result['ObjectURL']}" . PHP_EOL;
- } catch (MultipartUploadException $e) {
- echo $e->getMessage() . PHP_EOL;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement