Download an object from a bucket to a local file using the OSS SDK for PHP V2.
Prerequisites
Before you begin, make sure that you have:
The
oss:GetObjectpermission on the target bucket. For details, see Grant custom access policies to RAM users.The
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRETenvironment variables set with valid access credentials. For setup instructions, see Configure access credentials for PHP.
Usage notes
The sample code uses the China (Hangzhou) region (
cn-hangzhou) with an Internet endpoint. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For supported regions and endpoints, see OSS regions and endpoints.
Download an object to a local file
Call GetObject to download an object and write its content to a local file.
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Load access credentials from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion('<region>'); // e.g., cn-hangzhou
// $cfg->setEndpoint('<endpoint>'); // Optional: set a custom endpoint
$client = new Oss\Client($cfg);
$request = new Oss\Models\GetObjectRequest(
bucket: '<bucket-name>',
key: '<object-key>'
);
$result = $client->getObject($request);
// Write the object content to a local file.
$localFilePath = '<path/to/local/file.txt>';
file_put_contents($localFilePath, $result->body->getContents());
printf(
'status code: %s' . PHP_EOL .
'request ID: %s' . PHP_EOL,
$result->statusCode,
$result->requestId
);Replace the following placeholders:
| Placeholder | Description | Example |
|---|---|---|
<region> | Region ID of the bucket | cn-hangzhou |
<bucket-name> | Name of the bucket | my-bucket |
<object-key> | Key of the object to download | logs/app.log |
<path/to/local/file.txt> | Local path to save the file | downloads/app.log |
For the complete runnable sample, see the GitHub sample.
Use cases
Track download progress
Attach a progress callback to the request by setting $request->progressFn. The callback receives three arguments on each invocation:
| Argument | Type | Description |
|---|---|---|
$increment | int | Bytes downloaded in the current callback invocation |
$transferred | int | Total bytes downloaded so far |
$total | int | Total size of the object in bytes |
$request = new Oss\Models\GetObjectRequest(
bucket: '<bucket-name>',
key: '<object-key>'
);
$request->progressFn = static function (int $increment, int $transferred, int $total) {
echo sprintf("Downloaded so far: %d bytes" . PHP_EOL, $transferred);
echo sprintf("This chunk: %d bytes" . PHP_EOL, $increment);
echo sprintf("Total size: %d bytes" . PHP_EOL, $total);
echo '---' . PHP_EOL;
};
$result = $client->getObject($request);
printf(
'status code: %s' . PHP_EOL .
'request ID: %s' . PHP_EOL .
'object content: %s' . PHP_EOL,
$result->statusCode,
$result->requestId,
$result->body->getContents()
);The code above assumes the client is already initialized. See Download an object to a local file for client setup.