Symbolic links work like file shortcuts on Windows and allow you to quickly access associated objects in Object Storage Service (OSS).
Usage 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.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
To create a symbolic link, you must have the
oss:PutObjectpermission. To retrieve a symbolic link, you must have theoss:GetObjectpermission. For more information, see Grant custom permissions to a RAM user.
Create a symbolic link
The following sample 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\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// This example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the one for your region.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name. For example, examplebucket.
$bucket= "examplebucket";
// Specify the name of the object to which the symbolic link points. For example, exampleobject.txt.
$object = "exampleobject.txt";
// Specify the name of the symbolic link. For example, examplesymlink.txt.
$symlink = "examplesymlink.txt";
$options[OssClient::OSS_HEADERS] = array(
// Specify whether to overwrite an object that has the same name. This example sets the value to true, which prevents overwriting.
'x-oss-forbid-overwrite'=>"true",
// Specify the object's access control list (ACL). This example sets the ACL to private, which means only the object owner and authorized users have read and write permissions.
'x-oss-object-acl' =>"private",
// Specify the storage class of the object. This example sets the storage class to Standard.
'x-oss-storage-class' =>"Standard"
);
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
$ossClient->putSymlink($bucket, $symlink, $object,$options);
} catch (OssException $e) {
printf($e->getMessage() . "\n");
return;
}Get the target object of a symbolic link
To query a symbolic link, you must have read permissions on the symbolic link. The following sample 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\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// This example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the one for your region.
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name.
$bucket= "examplebucket";
// Specify the name of the symbolic link.
$symlink = "examplesymlink.txt";
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
// Get 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 for symbolic links, see GitHub example.
For more information about the API operation to create a symbolic link, see PutSymlink.
For more information about the API operation to retrieve a symbolic link, see GetSymlink.