File Uploader (OSS SDK for PHP V2)

更新时间:
复制 MD 格式

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:PutObject permission. 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

Specify the part size and parallel performance

Upload a file with custom part size and parallelism:

<?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.
    "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);

// Configure parameters related to the multipart upload.
$partSize = 100 * 1024; // The part size. Unit: bytes. In this example, the part size is set to 100 KB.

// 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.
    args: [ // Optional arguments for customizing multipart upload behaviors.
        'part_size' => $partSize, // Specify a custom part size.
        'parallel_num' => 1, // The number of parts to be uploaded in parallel.
    ]
);

// 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.
);

Upload a local file stream

Upload a local file as a stream:

<?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.
    "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 argument to specify 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);

// Configure parameters related to the multipart upload.
$partSize = 100 * 1024; // The part size. Unit: bytes. In this example, the part size is set to 100 KB.

// 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.
// Use LazyOpenStream to open the file as a stream to avoid loading the entire file to the memory at a time.
$result = $uploader->uploadFrom(
    request: new Oss\Models\PutObjectRequest(
        bucket: $bucket,
        key: $key
    ),
    stream: new \GuzzleHttp\Psr7\LazyOpenStream($filename, 'rb'), // Open the file stream in read-only mode.
    args: [
        'part_size' => $partSize, // Specify a custom part size.
    ]
);

// Display the result of the multipart upload.
printf(
    'upload from status code:' . $result->statusCode . PHP_EOL . // The HTTP status code. The status code 200 indicates that the request was successful.
    'upload from request id:' . $result->requestId . PHP_EOL .   // The request ID used for debugging or tracking the request.
    'upload from result:' . var_export($result, true) . PHP_EOL  // The detailed results of the multipart upload.
);

Display the upload progress

Track upload progress with a callback function:

<?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.
    "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);

// Configure parameters related to the multipart upload.
$partSize = 100 * 1024; // The part size. Unit: bytes. In this example, the part size is set to 100 KB.

// 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();

$request = new \AlibabaCloud\Oss\V2\Models\PutObjectRequest(bucket: $bucket, key: $key);

# Specify the upload progress callback function to display the upload progress.
$request->progressFn = function (int $increment, int $transferred, int $total) {
    echo sprintf("Uploaded: %d" . PHP_EOL, $transferred);
    echo sprintf("This upload: %d" . PHP_EOL, $increment);
    echo sprintf("Total data: %d" . PHP_EOL, $total);
    echo '-------------------------------------------' . PHP_EOL;
};

// Perform the multipart upload operation.
$result = $uploader->uploadFile(request: $request,filepath: $filename);

// 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.
);

Upload a file with an upload callback configured

Upload a file and notify your application server via callback:

<?php

// Include the autoload file to load dependencies.
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);

// Configure parameters related to the multipart upload.
$partSize = 100 * 1024; // The part size. Unit: bytes. In this example, the part size is set to 100 KB.

// Specify the path of the local file to be uploaded.
$filename = "/Users/yourLocalPath/yourFileName"; // Replace the value with your actual file path.

// Create an Uploader instance.
$uploader = $client->newUploader();

// ADD x-oss-callback AND x-oss-callback-var headers.
// Define the callback URL.
 $call_back_url = "http://www.example.com/callback";

// Define callback settings: Base64-encoded callback URL and callback body.
// Replace ${x:var1} and ${x:var2} with {var1} and {var2}, respectively.
$callback_body_template = "bucket={bucket}&object={object}&my_var_1={var1}&my_var_2={var2}";
$callback_body_replaced = str_replace(
    ['{bucket}', '{object}', '{var1}', '{var2}'],
    [$bucket, $key, 'value1', 'value2'],
    $callback_body_template
);
$callback = base64_encode(json_encode([
    "callbackUrl" => $call_back_url,
    "callbackBody" => $callback_body_replaced
]));

// Construct callback-var that is Base64-encoded.
$callback_var = base64_encode(json_encode([
    "x:var1" => "value1",
    "x:var2" => "value2"
]));

// Perform the multipart upload
$result = $uploader->uploadFile(
    request: new Oss\Models\PutObjectRequest(
                    bucket: $bucket,
                    key: $key,
                    callback: $callback,
                    callbackVar: $callback_var,), // 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.
);

References