Symbolic links can be used to access objects that are frequently used in buckets. After you create a symbolic link for an object, you can use the symbolic link to access the object.

Create a symbolic link

The following code provides an example on 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\OssClient;
use OSS\Core\OssException;

// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
$accessKeyId = "yourAccessKeyId";
$accessKeySecret = "yourAccessKeySecret";
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the name of the bucket. Example: examplebucket. 
$bucket= "examplebucket";
// Specify the name of the object to which the symbolic link points. Example: exampleobject.txt. 
$object = "exampleobject.txt";
// Specify the name of the symbolic link. Example: examplesymlink.txt. 
$symlink = "examplesymlink.txt";

$options[OssClient::OSS_HEADERS] = array(
    // Specify whether to overwrite the object that has the same name when the symbolic link is created. In this example, x-oss-forbid-overwrite is set to true. This indicates that the object that has the same name cannot be overwritten. 
    'x-oss-forbid-overwrite'=>"true",
    // Specify the access control list (ACL) of the object. In this example, the ACL is set to private. Only the owner of the object and authorized users have read and write permissions on the object. 
    'x-oss-object-acl' =>"private",
    // Specify the storage class of the object. In this example, the storage class is set to Standard. 
    'x-oss-storage-class' =>"Standard"
);
try {
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    $ossClient->putSymlink($bucket, $symlink, $object,$options);
} catch (OssException $e) {
    printf($e->getMessage() . "\n");
    return;
}

Query the name of the object to which a symbolic link points

To query a symbolic link, you must have read permissions on the symbolic link. The following code provides an example on how to query a symbolic link and the name of the object to which the symbolic link points:

<?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\OssClient;
use OSS\Core\OssException;

// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation.We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Specify the name of the bucket. 
$bucket= "examplebucket";
// Specify the name of the symbolic link that you want to query. 
$symlink = "examplesymlink.txt";
try {
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    // Query the symbolic link and the name of the object to which the symbolic link points. 
    $Symlinks = $ossClient->getSymlink($bucket, $symlink);
} catch (OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}
print(__FUNCTION__ . ": OK" . "\n");
var_dump($Symlinks);            

References

  • For the complete sample code of symbolic links, visit GitHub.
  • For more information about the API operation that you can call to create a symbolic link, see PutSymlink.
  • For more information about the API operation that you can call to query a symbolic link, see GetSymlink.