Multipart upload splits a large file into parts, uploads them in sequence, and combines them into a complete object with CompleteMultipartUpload.
Versioning behavior: OSS generates a unique version ID for the object when CompleteMultipartUpload is called successfully. The version ID is returned in the x-oss-version-id response header.
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Describe command-line parameters.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"bucket" => ['help' => 'The name of the bucket', 'required' => True],
"key" => ['help' => 'The name of the object', 'required' => True],
];
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
$options = getopt("", $longopts);
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
exit(1);
}
}
$region = $options["region"];
$bucket = $options["bucket"];
$key = $options["key"];
// Load credentials from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
$client = new Oss\Client($cfg);
// Step 1: Initiate the multipart upload and get the upload ID.
$request = new Oss\Models\InitiateMultipartUploadRequest(bucket: $bucket, key: $key);
$result = $client->initiateMultipartUpload($request);
$uploadId = $result->uploadId;
// Step 2: Upload parts. Part numbers start from 1.
$bigFileName = "/Users/localpath/yourfilename"; // Replace with the path of your local file.
$partSize = 1 * 1024 * 1024; // Part size in bytes. This example uses 1 MB.
$file = fopen($bigFileName, 'r');
$parts = [];
if ($file) {
$i = 1;
while (!feof($file)) {
$chunk = fread($file, $partSize);
$partResult = $client->uploadPart(
new Oss\Models\UploadPartRequest(
bucket: $bucket,
key: $key,
partNumber: $i,
uploadId: $uploadId,
contentLength: null,
contentMd5: null,
trafficLimit: null,
requestPayer: null,
body: Oss\Utils::streamFor(resource: $chunk)
)
);
$parts[] = new Oss\Models\UploadPart(
partNumber: $i,
etag: $partResult->etag,
);
$i++;
}
fclose($file);
}
// Step 3: Complete the multipart upload.
// OSS assigns a version ID to the object at this point.
$comResult = $client->completeMultipartUpload(
new Oss\Models\CompleteMultipartUploadRequest(
bucket: $bucket,
key: $key,
uploadId: $uploadId,
acl: null,
completeMultipartUpload: new Oss\Models\CompleteMultipartUpload(
parts: $parts
),
)
);
printf(
'status code: ' . $comResult->statusCode . PHP_EOL .
'request ID: ' . $comResult->requestId . PHP_EOL .
'complete multipart upload result: ' . var_export($comResult, true) . PHP_EOL
);