All Products
Search
Document Center

Object Storage Service:Manage symbolic links (PHP SDK V2)

Last Updated:Mar 20, 2026

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:PutObject permission to create symbolic links

  • The oss:GetObject permission 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.

Create a symbolic link

Method definition

putSymlink(request: PutSymlinkRequest): PutSymlinkResult

Parameters

ParameterTypeRequiredDescription
bucketstringYesThe name of the bucket.
keystringYesThe name of the symbolic link object.
targetstringYesThe name of the target object the symbolic link points to.
regionstringYesThe region where the bucket is located.
endpointstringNoA 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],
    "symlink"  => ['help' => 'The name of the symlink 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.
$symlink = $options["symlink"];   // The name of the target object.

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

// Create the symbolic link.
$request = new Oss\Models\PutSymlinkRequest(
    bucket: $bucket,
    key: $key,
    target: $symlink,
);

$result = $client->putSymlink($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

FieldDescription
statusCodeHTTP status code. 200 indicates the symbolic link was created successfully.
requestIdUnique request ID for debugging and tracing.

For the complete sample, see PutSymlink.php on GitHub.

Get a symbolic link

Method definition

getSymlink(request: GetSymlinkRequest): GetSymlinkResult

Parameters

ParameterTypeRequiredDescription
bucketstringYesThe name of the bucket.
keystringYesThe name of the symbolic link object.
regionstringYesThe region where the bucket is located.
endpointstringNoA 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

FieldDescription
statusCodeHTTP status code. 200 indicates the symbolic link was retrieved successfully.
requestIdUnique request ID for debugging and tracing.

For the complete sample, see GetSymlink.php on GitHub.