Use the OSS PHP SDK V1 to download an object from a bucket and save it as a local file.
Usage notes
The examples in this topic use the public endpoint for China (Hangzhou). To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For a full list of regions and endpoints, see Regions and endpoints.
The examples create an OSSClient instance using an OSS endpoint. To create an OSSClient instance using a custom domain name or Security Token Service (STS), see Create an OSSClient instance.
Before running the sample code, set the
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables with valid AccessKey credentials.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default — grant access through RAM Policy or Bucket policies.
| API | Action | When required |
|---|---|---|
| GetObject | oss:GetObject | Required to download an object |
| GetObject | oss:GetObjectVersion | Required when specifying an object version via versionId |
| GetObject | kms:Decrypt | Required when the object metadata contains X-Oss-Server-Side-Encryption: KMS |
Sample code
The following example downloads testfolder/exampleobject.txt from examplebucket and saves it as examplefile.txt in D:\localpath.
If the local file already exists, it is overwritten. If it does not exist, it is created. To save the file to a different location or under a different name, update the $localfile value.
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
// Load credentials from environment variables (OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET).
$provider = new EnvironmentVariableCredentialsProvider();
// Replace with the endpoint for your bucket's region.
// Example: https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou).
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name.
$bucket = "examplebucket";
// Specify the full object path within the bucket. Do not include the bucket name.
$object = "testfolder/exampleobject.txt";
// Specify the local destination path and filename.
// If no path is specified, the file is saved to the project's working directory.
$localfile = "D:\\localpath\\examplefile.txt";
$options = array(
OssClient::OSS_FILE_DOWNLOAD => $localfile
);
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
$ossClient->getObject($bucket, $object, $options);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK, please check localfile: 'examplefile.txt'" . "\n");FAQ
How do I rename a file when downloading it?
Set $localfile to the destination path and filename you want. For example, $localfile = "D:\\localpath\\examplefile.txt" saves the object as examplefile.txt regardless of its original name in the bucket.
References
For the complete sample code, see the example on GitHub.
For API details, see GetObject.