Symbolic links work like file shortcuts on Windows and allow you to quickly access associated objects in Object Storage Service (OSS).
Prerequisites
Before you begin, ensure that you have:
An OSS bucket
The
oss:PutObjectpermission to create a symbolic linkThe
oss:GetObjectpermission to retrieve a symbolic link
For details on granting permissions, see Grant custom permissions to a RAM user.
Usage notes
The sample code in this topic uses the China (Hangzhou) region and its public endpoint. To access OSS from other Alibaba Cloud services in the same region, use the internal endpoint. For more information, see Regions and endpoints.
The OSSClient instance in this topic is initialized using an OSS endpoint. To initialize using a custom domain name or Security Token Service (STS), see Create an OSSClient instance.
Create a symbolic link
Method signature
putSymlink(string $bucket, string $symlink, string $object, array $options = null): voidParameters
| Parameter | Type | Required | Description |
|---|---|---|---|
$bucket | string | Yes | The name of the bucket. |
$symlink | string | Yes | The name of the symbolic link. |
$object | string | Yes | The name of the target object that the symbolic link points to. |
$options | array | No | Optional headers. Supported headers: x-oss-forbid-overwrite, x-oss-object-acl, and x-oss-storage-class. |
Sample code
<?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 ACL. This example sets the ACL to private.
'x-oss-object-acl' =>"private",
// Specify the storage class. 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);
print("Symbolic link created successfully." . PHP_EOL);
} catch (OssException $e) {
printf($e->getMessage() . "\n");
return;
}Get the target object of a symbolic link
Method signature
getSymlink(string $bucket, string $symlink): arrayParameters
| Parameter | Type | Required | Description |
|---|---|---|---|
$bucket | string | Yes | The name of the bucket. |
$symlink | string | Yes | The name of the symbolic link. |
Return value
An array containing the symbolic link metadata and the name of the target object.
You must have read permissions on the symbolic link to call this method.
Sample code
<?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);
print("Symbolic link retrieved successfully." . PHP_EOL);
var_dump($Symlinks);
} catch (OssException $e) {
printf("Failed to get symbolic link: " . $e->getMessage() . "\n");
return;
}References
For the complete sample code for symbolic links, see GitHub example.
For the API operation to create a symbolic link, see PutSymlink.
For the API operation to get a symbolic link, see GetSymlink.