Manage symbolic links (PHP SDK V1)

Updated at:

A symbolic link works like a Windows shortcut — it gives you a fast path to frequently accessed objects. In versioning-enabled buckets, each version of a symbolic link can point to a different object.

Prerequisites

Before you begin, make sure you have:

  • The oss:PutObject permission to create a symbolic link

  • The oss:GetObject permission to retrieve a symbolic link

To grant these permissions to a RAM user, see Grant custom access policies to a RAM user.

Usage notes

  • The code examples use the public endpoint for the China (Hangzhou) region (oss-cn-hangzhou.aliyuncs.com). To access OSS from other Alibaba Cloud services in the same region, use the internal endpoint instead. For a full list of regions and endpoints, see Regions and endpoints.

  • The examples initialize OssClient with a standard OSS endpoint. For alternative setups — such as a custom domain or Security Token Service (STS) credentials — see Create an OssClient instance.

Create a symbolic link

In a versioning-enabled bucket, a new symbolic link points to the current version of the target object by default. You cannot create a symbolic link for a delete marker.

The following code:

  1. Reads credentials from the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.

  2. Initializes OssClient with Signature Version 4.

  3. Calls putSymlink() to create a symbolic link that points to the specified object.

<?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;
use OSS\CoreOssException;

// Read credentials from environment variables.
// Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this sample.
$provider = new EnvironmentVariableCredentialsProvider();

// Replace with your endpoint. This sample uses China (Hangzhou).
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";

// Replace with your actual values.
$bucket  = "<yourBucketName>";
$object  = "<yourObjectName>";   // Full object path, e.g. example/test.txt
$symlink = "<yourSymLink>";      // Name of the symbolic link to create

$config = array(
    "provider"         => $provider,
    "endpoint"         => $endpoint,
    "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
    "region"           => "cn-hangzhou"
);
$ossClient = new OssClient($config);

try {
    // Create the symbolic link.
    $ossClient->putSymlink($bucket, $symlink, $object);
} catch (OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}

print(__FUNCTION__ . ": OK\n");

For the full API reference, see PutSymlink.

Get a symbolic link

getSymlink() returns the current version of a symbolic link by default. Pass a version ID to retrieve a specific version.

If the current version of the symbolic link is a delete marker, OSS returns 404 Not Found. The response headers include:

  • x-oss-delete-marker: true

  • x-oss-version-id: the version ID of the delete marker

The following code:

  1. Reads credentials from the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.

  2. Initializes OssClient with Signature Version 4.

  3. Calls getSymlink() with a version ID and prints the version ID and the target object name from the response.

<?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;
use OSS\CoreOssException;

// Read credentials from environment variables.
// Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this sample.
$provider = new EnvironmentVariableCredentialsProvider();

// Replace with your endpoint. This sample uses China (Hangzhou).
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";

// Replace with your actual values.
$bucket    = "<yourBucketName>";
$symlink   = "<yourSymlink>";
$versionId = "<yourObjectVersionId>";

$config = array(
    "provider"         => $provider,
    "endpoint"         => $endpoint,
    "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
    "region"           => "cn-hangzhou"
);
$ossClient = new OssClient($config);

try {
    // Retrieve a specific version of the symbolic link.
    $sym = $ossClient->getSymlink($bucket, $symlink, array(OssClient::OSS_VERSION_ID => $versionId));
    printf($sym['x-oss-version-id'] . "\n");
    printf($sym[OssClient::OSS_SYMLINK_TARGET] . "\n");
} catch (OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}

print(__FUNCTION__ . ": OK\n");

For the full API reference, see GetSymlink.