All Products
Search
Document Center

Object Storage Service:Manage symbolic links in PHP

Last Updated:Mar 08, 2024

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. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS 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 need to have the oss:PutObject permission. To query a symbolic link, you need to have the oss:GetObject permission. For more information, see Attach a custom policy 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();
// 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 you want the symbolic link to point. 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 as the symbolic link. 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 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, the storage class is set to Standard. 
    'x-oss-storage-class' =>"Standard"
);
try {
    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
    );
    $ossClient = new OssClient($config);
    $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 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();
// 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. 
$symlink = "examplesymlink.txt";
try {
    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
    );
    $ossClient = new OssClient($config);
    // 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 that is used to manage 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.