Skip to content

Commit 1ca4b08

Browse files
backups: add S3 part size configuration (pterodactyl#4382)
1 parent 68e9100 commit 1ca4b08

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

app/Http/Controllers/Api/Remote/Backups/BackupRemoteUploadController.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class BackupRemoteUploadController extends Controller
1717
{
18-
public const PART_SIZE = 5 * 1024 * 1024 * 1024;
18+
public const DEFAULT_MAX_PART_SIZE = 5 * 1024 * 1024 * 1024;
1919

2020
/**
2121
* @var \Pterodactyl\Repositories\Eloquent\BackupRepository
@@ -89,9 +89,12 @@ public function __invoke(Request $request, string $backup)
8989
// the other presigned urls.
9090
$params['UploadId'] = $result->get('UploadId');
9191

92+
// Retrieve configured part size
93+
$maxPartSize = $this->getConfiguredMaxPartSize();
94+
9295
// Create as many UploadPart presigned urls as needed
9396
$parts = [];
94-
for ($i = 0; $i < ($size / self::PART_SIZE); ++$i) {
97+
for ($i = 0; $i < ($size / $maxPartSize); ++$i) {
9598
$parts[] = $client->createPresignedRequest(
9699
$client->getCommand('UploadPart', array_merge($params, ['PartNumber' => $i + 1])),
97100
$expires
@@ -103,7 +106,30 @@ public function __invoke(Request $request, string $backup)
103106

104107
return new JsonResponse([
105108
'parts' => $parts,
106-
'part_size' => self::PART_SIZE,
109+
'part_size' => $maxPartSize,
107110
]);
108111
}
112+
113+
/**
114+
* Get the configured maximum size of a single part in the multipart uplaod.
115+
*
116+
* The function tries to retrieve a configured value from the configuration.
117+
* If no value is specified, a fallback value will be used.
118+
*
119+
* Note if the received config cannot be converted to int (0), is zero or is negative,
120+
* the fallback value will be used too.
121+
*
122+
* The fallback value is {@see BackupRemoteUploadController::DEFAULT_MAX_PART_SIZE}.
123+
*
124+
* @return int
125+
*/
126+
private function getConfiguredMaxPartSize()
127+
{
128+
$maxPartSize = (int) config('backups.max_part_size', self::DEFAULT_MAX_PART_SIZE);
129+
if ($maxPartSize <= 0) {
130+
$maxPartSize = self::DEFAULT_MAX_PART_SIZE;
131+
}
132+
133+
return $maxPartSize;
134+
}
109135
}

config/backups.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
// uses to upload backups to S3 storage. Value is in minutes, so this would default to an hour.
1313
'presigned_url_lifespan' => env('BACKUP_PRESIGNED_URL_LIFESPAN', 60),
1414

15+
// This value defines the maximal size of a single part for the S3 multipart upload during backups
16+
// The maximal part size must be given in bytes. The default value is 5GB.
17+
// Note that 5GB is the maximum for a single part when using AWS S3.
18+
'max_part_size' => env('BACKUP_MAX_PART_SIZE', 5 * 1024 * 1024 * 1024),
19+
1520
// The time to wait before automatically failing a backup, time is in minutes and defaults
1621
// to 6 hours. To disable this feature, set the value to `0`.
1722
'prune_age' => env('BACKUP_PRUNE_AGE', 360),

0 commit comments

Comments
 (0)