Todos os produtos
Search
Central de documentação

Object Storage Service:Gerenciar links simbólicos (PHP SDK V1)

Última atualização: Sep 25, 2026

Os links simbólicos funcionam como atalhos de arquivos no Windows e permitem o acesso rápido aos objetos associados no Object Storage Service (OSS).

Notas de uso

  • Este tópico usa o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para obter detalhes sobre as regiões e os endpoints compatíveis, consulte Regiões e endpoints.

  • Este tópico mostra como criar uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como usar um domínio personalizado ou autenticar-se com credenciais do Security Token Service (STS), consulte Criar um OssClient.

  • Para criar um link simbólico, você precisa da permissão oss:PutObject. Para obter um link simbólico, você precisa da permissão oss:GetObject. Para saber mais, consulte Conceder permissões personalizadas a um usuário do RAM.

Criar um link simbólico

O código de exemplo a seguir mostra como criar um link simbólico:

<?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;
}

Obter o objeto de destino de um link simbólico

Para consultar um link simbólico, você precisa de permissões de leitura no link simbólico. O exemplo de código a seguir demonstra como consultar um link simbólico e obter o nome do objeto de destino:

<?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);            

Referências

  • Para obter o código de exemplo completo para links simbólicos, consulte Exemplo no GitHub.

  • Para mais detalhes sobre a operação da API para criar um link simbólico, consulte PutSymlink.

  • Para mais informações sobre a operação da API para obter um link simbólico, consulte GetSymlink.