Use the Uploader module in OSS SDK for PHP V2 to split large files into parts, upload them in parallel, and resume interrupted transfers.
Usage notes
-
In this topic, the public endpoint of the China (Hangzhou) region (
cn-hangzhou) is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use the internal endpoint. For more information about the mappings between OSS regions and endpoints, see Regions and Endpoints. -
To upload files, you must have the
oss:PutObjectpermission. For more information, see Grant a custom policy. -
This topic loads access credentials from environment variables. Configure access credentials for OSS SDK for PHP.
Methods
Uploader overview
The Uploader module simplifies file uploads in OSS SDK for PHP V2.
-
Uploader splits large files or streams into parts for parallel upload.
-
Uploader supports resumable uploads. It records part status during transfer, so you can resume from a checkpoint file after failures such as network interruptions or unexpected exits, even after multiple retries.
Uploader provides the following methods:
final class Uploader
{
...
public function __construct($client, array $args = [])
public function uploadFile(Models\PutObjectRequest $request, string $filepath, array $args = []): Models\UploadResult
public function uploadFrom(Models\PutObjectRequest $request, StreamInterface $stream, array $args = []): Models\UploadResult
...
}
Request parameters
|
Parameter |
Type |
Description |
|
request |
PutObjectRequest |
Upload request parameters, same as PutObject parameters. |
|
stream |
StreamInterface |
The stream to be uploaded. |
|
filePath |
string |
The path of the local file. |
|
args |
array |
The optional configuration parameters. |
Common args parameters:
|
Parameter |
Type |
Description |
|
part_size |
int |
The part size. Default value: 6 MiB. |
|
parallel_num |
int |
Number of parallel upload parts. Default: 3. Applies per call. |
|
leave_parts_on_error |
bool |
Whether to retain uploaded parts on failure. Default: false. |
Set parameters when creating an Uploader instance or in each upload call.
-
Set parameters on the Uploader instance
$u = $client->newUploader(['part_size' => 10 * 1024 * 1024]); -
Set parameters per upload request
$u->uploadFile( new Oss\Models\PutObjectRequest( bucket: 'bucket', key: 'key' ), filepath: '/local/dir/example', args: [ 'part_size' => 10 * 1024 * 1024, ] );
Sample code
Upload a local file to a bucket:
<?php
// Import autoload files to load dependent libraries.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Specify descriptions for command-line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // (Required) The region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // (Optional) The endpoint for accessing OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // (Required) The name of the bucket.
"key" => ['help' => 'The name of the object', 'required' => True], // (Required) The name of the object.
];
// Construct an array of long options required by getopt.
// Add a colon (:) to the end of each key to indicate that a value is required.
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
// Parse the command-line arguments.
$options = getopt("", $longopts);
// Check whether all required arguments are provided.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help']; // Obtain the argument help information.
echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
exit(1); // If a required argument is missing, exit the program.
}
}
// Extract values from the parsed arguments.
$region = $options["region"]; // The region where the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.
$key = $options["key"]; // The name of the object.
// Load access credentials from environment variables.
// Use EnvironmentVariableCredentialsProvider to obtain the AccessKey ID and AccessKey secret from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configuration of the SDK.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider); // Specify the credential provider.
$cfg->setRegion($region); // Specify the region where the bucket is located.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]); // Specify the endpoint if provided.
}
// Create an OSSClient instance.
$client = new Oss\Client($cfg);
// Specify the path of the local file to be uploaded.
$filename = "/Users/yourLocalPath/yourFileName"; // The sample file path.
// Create an Uploader instance.
$uploader = $client->newUploader();
// Perform the multipart upload operation.
$result = $uploader->uploadFile(
request: new Oss\Models\PutObjectRequest(bucket: $bucket, key: $key), // Create a PutObjectRequest object to specify the names of the bucket and object.
filepath: $filename, // Specify the path of the local file to be uploaded.
);
// Display the result of the multipart upload.
printf(
'multipart upload status code:' . $result->statusCode . PHP_EOL . // The HTTP status code. The status code 200 indicates that the request was successful.
'multipart upload request id:' . $result->requestId . PHP_EOL . // The request ID used for debugging or tracking the request.
'multipart upload result:' . var_export($result, true) . PHP_EOL // The detailed results of the multipart upload.
);
Common scenarios
References
-
Uploader details: OSS SDK for PHP - Developer Guide.
-
Complete sample code: Uploader.php.