Manage symbolic links (PHP SDK V1)

Updated at:

A symbolic link is similar to a Windows shortcut and lets you quickly access frequently used object files. In a versioning-enabled bucket, different versions of a symbolic link can point to different object files.

Notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.

  • This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Create an OssClient.

  • You must have the oss:PutObject permission to create a symbolic link and the oss:GetObject permission to retrieve a symbolic link. For more information, see Grant custom access policies to a RAM user.

Create a symbolic link

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

The following code shows how to create a symbolic link:

<?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;

// Obtain access credentials from environment variables. Before running this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// The China (Hangzhou) Endpoint is used as an example. Specify a different Endpoint as needed.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name.
$bucket= "<yourBucketName>";
// Specify the object name. The object name is the full path of the object that does not include the bucket name, such as example/test.txt.
$object = "<yourObjectName>";
// Specify the name of the symbolic link.
$symlink = "<yourSymLink>";

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

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

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

For more information about creating symbolic links, see PutSymlink.

Get a symbolic link

The getSymlink method retrieves the current version of a symbolic link by default. To retrieve a specific version of the symbolic link, specify its versionId. If the current version of the symbolic link is a delete marker, OSS returns a 404 Not Found error. The response header contains x-oss-delete-marker = true and the version ID in x-oss-version-id.

The following code shows how to retrieve a symbolic link:

<?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;

// Obtain access credentials from environment variables. Before running this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// The China (Hangzhou) Endpoint is used as an example. Specify a different Endpoint as needed.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name.
$bucket= "<yourBucketName>";
// Specify the name of the symbolic link.
$symlink = "<yourSymlink>";
// Specify the version ID of the object.
$versionId = "<yourObjectVersionId>";

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

try {
    // Get the content of 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 more information about retrieving symbolic links, see GetSymlink.