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

// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access Object Storage Service (OSS) because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. 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 the endpoint based on your business requirements. 
$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 with the same name when the symbolic link is created. In this example, this parameter is set to true, which indicates that the object with the same name cannot be overwritten. 
    'x-oss-forbid-overwrite'=>"true", 
    // Specify the access control list (ACL) of the object. In this example, this parameter is set to private, which indicates that 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, this parameter is set to Standard. 
    'x-oss-storage-class' =>"Standard"
);
try {
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint,$options);
    $ossClient->putSymlink($bucket, $symlink, $object);
} 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 the 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;

// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. 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 the endpoint based on your business requirements. 
$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 more information about 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.