Symbolic links let you access frequently used objects in a bucket through shortcuts, similar to shortcuts in Windows. This topic describes how to create and get symbolic links using the OSS SDK for PHP V2.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket in your target region
The
oss:PutObjectpermission to create symbolic linksThe
oss:GetObjectpermission to get symbolic links
For details on granting permissions, see Grant custom permissions to a RAM user.
Usage notes
Note: The sample code uses the China (Hangzhou) region (cn-hangzhou) with a public endpoint. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For more information, see OSS regions and endpoints.Get a symbolic link
Method definition
getSymlink(request: GetSymlinkRequest): GetSymlinkResultParameters
| Parameter | Type | Required | Description |
|---|---|---|---|
bucket | string | Yes | The name of the bucket. |
key | string | Yes | The name of the symbolic link object. |
region | string | Yes | The region where the bucket is located. |
endpoint | string | No | A custom endpoint. If not specified, the default endpoint for the region is used. |
Sample code
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
$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"]; // The name of the symbolic link.
// 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);
// Get the symbolic link and the name of the target object it points to.
$request = new Oss\Models\GetSymlinkRequest(
bucket: $bucket,
key: $key
);
$result = $client->getSymlink($request);
printf(
'status code: %s' . PHP_EOL .
'request ID: %s' . PHP_EOL .
'result: %s' . PHP_EOL,
$result->statusCode,
$result->requestId,
var_export($result, true)
);Result fields
| Field | Description |
|---|---|
statusCode | HTTP status code. 200 indicates the symbolic link was retrieved successfully. |
requestId | Unique request ID for debugging and tracing. |
For the complete sample, see GetSymlink.php on GitHub.