Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Sembolik bağlantıları yönetin (PHP SDK V2)

Son Güncelleme:Oct 02, 2026

Sembolik bağlantı özelliği, bir paket içindeki sık kullanılan dosyalara kolay erişim sağlar. Bir sembolik bağlantı oluşturduktan sonra, Windows kısayollarına benzer şekilde kullanarak dosyalara erişebilirsiniz. Bu konuda, OSS PHP SDK kullanılarak sembolik bağlantıların nasıl oluşturulacağı ve alınacağı açıklanmaktadır.

Dikkat edilmesi gerekenler

  • Bu konudaki örnek kodlar, Çin (Hangzhou) bölgesini (cn-hangzhou) ve bu bölgenin genel uç noktasını örnek olarak kullanır. Aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e erişmek için dahili uç noktayı kullanın. OSS tarafından desteklenen bölgeler ve uç noktalar hakkında daha fazla bilgi için bkz. OSS bölgeleri ve uç noktaları.

  • Sembolik bağlantı oluşturmak için oss:PutObject iznine sahip olmanız gerekir. Sembolik bağlantıyı almak için oss:GetObject iznine sahip olmanız gerekir. Daha fazla bilgi için bkz. RAM kullanıcıya özel izinler atama.

Örnek kod

Sembolik bağlantı oluşturun

Sembolik bağlantı oluşturmak için aşağıdaki kodu kullanabilirsiniz.

<?php

// Import the autoloader file to ensure that dependency libraries are loaded correctly.
require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Define the descriptions for command-line arguments.
$optsdesc = [
    "region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region where the bucket is located. (Required)
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint. (Optional)
    "bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name. (Required)
    "key" => ['help' => 'The name of the object', 'required' => True], // The name of the symbolic link. (Required)
    "symlink" => ['help' => 'The name of the symlink object', 'required' => True], // The name of the target object for the symbolic link. (Required)
];

// Convert the argument descriptions to the long option format required by getopt.
// Append a colon (:) to each argument to indicate that the argument requires a value.
$longopts = \array_map(function ($key) {
    return "$key:";
}, array_keys($optsdesc));

// Parse the command-line arguments.
$options = getopt("", $longopts);

// Verify that all required arguments are provided.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help']; // Obtain the help information for the argument.
        echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
        exit(1); // If a required argument is missing, exit the program.
    }
}

// Extract values from the parsed arguments.
$region = $options["region"]; // The region where the bucket is located.
$bucket = $options["bucket"]; // The bucket name.
$key = $options["key"];       // The name of the symbolic link.
$symlink = $options["symlink"]; // The name of the target object for the symbolic link.

// Load credentials from environment variables.
// Use EnvironmentVariableCredentialsProvider to read the AccessKey ID and AccessKey secret from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider); // Set the credential provider.
$cfg->setRegion($region); // Set the region where the bucket is located.
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]); // If an endpoint is specified, set the endpoint.
}

// Create an OSS client instance.
$client = new Oss\Client($cfg);

// Create a PutSymlinkRequest object to create a symbolic link.
$request = new Oss\Models\PutSymlinkRequest(
    bucket: $bucket,
    key: $key,  // The name of the symbolic link.
    target: $symlink ,// The name of the target object for the symbolic link.
);

// Execute the operation to create the symbolic link.
$result = $client->putSymlink($request);

// Print the result of the symbolic link creation.
printf(
    'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code. For example, 200 indicates that the operation is successful.
    'request id:' . $result->requestId . PHP_EOL .   // The request ID, which is used to debug or track the request.
    'result:' . var_export($result, true) . PHP_EOL  // The detailed result of the symbolic link creation.
);

Sembolik bağlantıyı alın

Aşağıdaki kodu kullanarak sembolik bağlantıyı ve sembolik bağlantının işaret ettiği hedef dosyanın adını alabilirsiniz.

<?php

// Import the autoloader file to ensure that dependency libraries are loaded correctly.
require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Define the descriptions for command-line arguments.
$optsdesc = [
    "region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region where the bucket is located. (Required)
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint. (Optional)
    "bucket" => ['help' => 'The name of the bucket', 'required' => True], // The bucket name. (Required)
    "key" => ['help' => 'The name of the object', 'required' => True], // The name of the symbolic link. (Required)
];

// Convert the parameter descriptions to the long option format required by getopt.
// A colon ":" after each parameter indicates that the parameter requires a value.
$longopts = \array_map(function ($key) {
    return "$key:";
}, array_keys($optsdesc));

// Parse the command-line arguments.
$options = getopt("", $longopts);

// Verify that all required parameters are specified.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help']; // Obtain the help information for the parameter.
        echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
        exit(1); // If a required parameter is not specified, exit the program.
    }
}

// Extract values from the parsed parameters.
$region = $options["region"]; // The region where the bucket is located.
$bucket = $options["bucket"]; // The bucket name.
$key = $options["key"];       // The name of the symbolic link.

// Load credentials from environment variables.
// Use EnvironmentVariableCredentialsProvider to read the Access Key ID and Access Key Secret from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider); // Set the credential provider.
$cfg->setRegion($region); // Set the region where the bucket is located.
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]); // If an endpoint is provided, set the endpoint.
}

// Create an OSS client instance.
$client = new Oss\Client($cfg);

// Create a GetSymlinkRequest object to obtain the target object of the symbolic link.
$request = new Oss\Models\GetSymlinkRequest(
    bucket: $bucket,
    key: $key
);

// Execute the operation to obtain the symbolic link.
$result = $client->getSymlink($request);

// Print the result of obtaining the symbolic link.
printf(
    'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code. For example, 200 indicates a successful request.
    'request id:' . $result->requestId . PHP_EOL .   // The request ID, which is used for debugging or tracing requests.
    'result:' . var_export($result, true) . PHP_EOL  // The detailed result of the obtained symbolic link.
);

İlgili bilgiler

  • Sembolik bağlantı oluşturmaya yönelik örnek kodlar için bkz. Github örneği.

  • Sembolik bağlantıları almaya yönelik örnek kodlar için bkz. Github örneği.