This topic describes how to use the OSS SDK for PHP to create and retrieve symbolic links in a versioning-enabled bucket.
Precautions
The sample code in this topic uses a public endpoint in the China (Hangzhou) region (
cn-hangzhou) as an example. If you want to access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the mappings between OSS regions and endpoints, see OSS regions and endpoints.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.
Sample code
Create a symbolic link
A symbolic link can have multiple versions. Each version can point to a different target object. The version ID is automatically generated by OSS and returned in the x-oss-version-id response header.
You cannot create a symbolic link for a delete marker in a versioning-enabled bucket.
You can use the following code to create a symbolic link that points to the current version of the target object.
<?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 description of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region in which the bucket is located. (Required)
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint that can be used by other services to access OSS. (Optional)
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // The name of the bucket. (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 object file targeted by the symbolic link. (Required)
];
// Convert the argument description to the long option format required by getopt.
// Add a colon (:) after 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);
// Check whether required arguments are specified.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help']; // Obtain the help information of the argument.
echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
exit(1); // If a required argument is not specified, exit the program.
}
}
// Extract values from the parsed arguments.
$region = $options["region"]; // The region in which the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.
$key = $options["key"]; // The name of the symbolic link.
$symlink = $options["symlink"]; // The name of the object file targeted by 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 in which 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 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 object file targeted by the symbolic link.
);
// Execute the operation to create the symbolic link.
$result = $client->putSymlink($request);
// Print the result of creating the symbolic link.
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 for debugging or tracking requests.
'result:' . var_export($result, true) . PHP_EOL // The detailed result of creating the symbolic link.
);
Obtain a symbolic link
To retrieve a symbolic link, you must have the read permission on the symbolic link.
By default, the GetSymlink operation retrieves the current version of a symbolic link. You can specify a version ID to retrieve a specific version. If the current version of the symbolic link is a delete marker, OSS returns a 404 Not Found error. In the response, the x-oss-delete-marker header is set to true and the x-oss-version-id header is returned.
You can use the following code to retrieve a symbolic link.
<?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 description of command line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region in which the bucket is located. (Required)
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint that can be used by other services to access OSS. (Optional)
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // The name of the bucket. (Required)
"key" => ['help' => 'The name of the object', 'required' => True], // The name of the symbolic link. (Required)
];
// Convert the argument description to the long option format required by getopt.
// Add a colon (:) after 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);
// Check whether required arguments are specified.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help']; // Obtain the help information of the argument.
echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
exit(1); // If a required argument is not specified, exit the program.
}
}
// Extract values from the parsed arguments.
$region = $options["region"]; // The region in which the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.
$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 in which 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,
versionId: "yourVersionId", // Specify the actual version ID.
);
// 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 that the operation is successful.
'request id:' . $result->requestId . PHP_EOL . // The request ID, which is used for debugging or tracking requests.
'result:' . var_export($result, true) . PHP_EOL // The detailed result of the obtained symbolic link.
);