Agentic Bucket (OSS SDK for PHP 2.0)
This topic describes how to use the OSS SDK for PHP 2.0 to create and manage Agentic Buckets, bind and unbind wildcard CNAMEs, and create Bucket Spaces and upload objects.
Usage notes
To use Agentic Bucket features, you must install
alibabacloud/oss-v2(OSS SDK for PHP 2.0).The sample code in this topic uses the region ID
cn-hangzhouof the China (Hangzhou) region as an example and uses the public endpoint by default. To access OSS from other Alibaba Cloud services in the same region as OSS, use the internal endpoint. For more information about the mappings between the regions and endpoints supported by OSS, see Regions and endpoints.When you use the
AlibabaCloud\Oss\V2\Agenticnamespace clients (AgenticBucketClient,BucketSpaceClient), you must set both of the following in the configuration:regionandaccountId. In the request, thebucketparameter is the bucket name prefix, and the SDK automatically resolves it to the full name. The full name of an Agentic Bucket is{prefix}-{accountId}-{region}-ab-apsr, and the full name of a Bucket Space is{prefix}-{accountId}-{region}-bs-apsr.You can access a Bucket Space in either of the following ways: Use
BucketSpaceClient. In the request, thebucketparameter is set to the Bucket Space name prefix, and the SDK automatically resolves it to the full name. Or, useBucketSpaceHelperand the standard clientOss\Client. First, call itstoBucketNamemethod to convert the name prefix to the full name, and then access the Bucket Space through the standard client. In this case, thebucketparameter in the request must be set to the full name. When you useBucketSpaceHelper, you must also set both of the following in the configuration:regionandaccountId.CNAME-related operations (creating a CnameToken, getting a CnameToken, binding a CNAME, listing CNAMEs, and deleting a CNAME) are provided by the standard client
Oss\Client. In the request, thebucketparameter must be set to the full name of the Agentic Bucket, which is{prefix}-{accountId}-{region}-ab-apsr.In wildcard CNAME requests, the
domainorcnameparameter must be set to the base domain name (without the*.prefix). The base domain name must contain at least three segments (for example,example.comis invalid, andfoo.example.comis valid). Before you bind a wildcard CNAME, you must apply for a CnameToken for the base domain name and complete domain ownership verification. If you bind a certificate, the certificate must be a wildcard certificate that matches*.base-domain. For more information about the complete rules for wildcard CNAMEs, see PutCname.
Sample code
Create an Agentic Bucket
The following code creates an Agentic Bucket.
<?php
// Import the autoloader to load the dependencies
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
use AlibabaCloud\Oss\V2\Agentic\AgenticBucketClient;
use AlibabaCloud\Oss\V2\Agentic\Models;
// Define the command-line argument descriptions, including the required region, Agentic Bucket name prefix, and account ID, and the optional endpoint
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"account-id" => ['help' => 'The ID of the Alibaba Cloud account that owns the bucket.', 'required' => True],
"bucket" => ['help' => 'The name of the agentic bucket.', 'required' => True],
];
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
$options = getopt("", $longopts);
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
$region = $options["region"];
$accountId = $options["account-id"];
$bucket = $options["bucket"];
// Load access credentials from environment variables for authentication
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Create a configuration object with the SDK default configuration. AgenticBucketClient resolves
// the bucket name prefix to the full name automatically: {prefix}-{accountId}-{region}-ab-apsr
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
$cfg->setAccountId($accountId);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Initialize the AgenticBucketClient with the preceding configuration
$client = new AgenticBucketClient($cfg);
try {
// Create an Agentic Bucket with the storage class set to Standard and the data redundancy type set to LRS
$request = new Models\CreateAgenticBucketRequest(
$bucket,
new Models\CreateAgenticBucketConfiguration('Standard', 'LRS')
);
$result = $client->createAgenticBucket($request);
printf(
'status code: %d' . PHP_EOL . 'request id: %s' . PHP_EOL,
$result->statusCode,
$result->requestId
);
} catch (\Throwable $e) {
printf("error: %s" . PHP_EOL, $e->getMessage());
exit(1);
}Query Agentic Bucket information
The following code queries the information about a specified Agentic Bucket.
<?php
// Import the autoloader to load the dependencies
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
use AlibabaCloud\Oss\V2\Agentic\AgenticBucketClient;
use AlibabaCloud\Oss\V2\Agentic\Models;
// Define the command-line argument descriptions, including the required region, Agentic Bucket name prefix, and account ID, and the optional endpoint
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"account-id" => ['help' => 'The ID of the Alibaba Cloud account that owns the bucket.', 'required' => True],
"bucket" => ['help' => 'The name of the agentic bucket.', 'required' => True],
];
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
$options = getopt("", $longopts);
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
$region = $options["region"];
$accountId = $options["account-id"];
$bucket = $options["bucket"];
// Load access credentials from environment variables for authentication
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Create a configuration object with the SDK default configuration and set the region and account ID
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
$cfg->setAccountId($accountId);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Initialize the AgenticBucketClient with the preceding configuration
$client = new AgenticBucketClient($cfg);
try {
// Query the information about a specified Agentic Bucket
$request = new Models\GetAgenticBucketRequest($bucket);
$result = $client->getAgenticBucket($request);
printf(
'status code: %d' . PHP_EOL . 'request id: %s' . PHP_EOL,
$result->statusCode,
$result->requestId
);
if ($result->agenticBucketInfo !== null) {
printf('bucket name: %s' . PHP_EOL, $result->agenticBucketInfo->name);
}
} catch (\Throwable $e) {
printf("error: %s" . PHP_EOL, $e->getMessage());
exit(1);
}List Agentic Buckets
The following code lists all Agentic Buckets of the current account in a specified region.
<?php
// Import the autoloader to load the dependencies
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
use AlibabaCloud\Oss\V2\Agentic\AgenticBucketClient;
use AlibabaCloud\Oss\V2\Agentic\ListAgenticBucketsPaginator;
use AlibabaCloud\Oss\V2\Agentic\Models;
// Define the command-line argument descriptions, including the required region and account ID, and the optional endpoint
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"account-id" => ['help' => 'The ID of the Alibaba Cloud account that owns the bucket.', 'required' => True],
];
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
$options = getopt("", $longopts);
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
$region = $options["region"];
$accountId = $options["account-id"];
// Load access credentials from environment variables for authentication
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Create a configuration object with the SDK default configuration and set the region and account ID
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
$cfg->setAccountId($accountId);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Initialize the AgenticBucketClient with the preceding configuration
$client = new AgenticBucketClient($cfg);
try {
// Create a ListAgenticBuckets paginator iterator to list all Agentic Buckets of the current account in the specified region
$paginator = new ListAgenticBucketsPaginator($client);
foreach ($paginator->iterPage(new Models\ListAgenticBucketsRequest()) as $page) {
foreach ($page->agenticBuckets ?? [] as $summary) {
printf('agentic bucket: name: %s, storage class: %s, data redundancy type: %s' . PHP_EOL,
$summary->name, $summary->storageClass, $summary->dataRedundancyType);
}
}
} catch (\Throwable $e) {
printf("error: %s" . PHP_EOL, $e->getMessage());
exit(1);
}Delete an Agentic Bucket
The following code deletes a specified Agentic Bucket.
<?php
// Import the autoloader to load the dependencies
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
use AlibabaCloud\Oss\V2\Agentic\AgenticBucketClient;
use AlibabaCloud\Oss\V2\Agentic\Models;
// Define the command-line argument descriptions, including the required region, Agentic Bucket name prefix, and account ID, and the optional endpoint
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"account-id" => ['help' => 'The ID of the Alibaba Cloud account that owns the bucket.', 'required' => True],
"bucket" => ['help' => 'The name of the agentic bucket.', 'required' => True],
];
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
$options = getopt("", $longopts);
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
$region = $options["region"];
$accountId = $options["account-id"];
$bucket = $options["bucket"];
// Load access credentials from environment variables for authentication
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Create a configuration object with the SDK default configuration and set the region and account ID
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
$cfg->setAccountId($accountId);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Initialize the AgenticBucketClient with the preceding configuration
$client = new AgenticBucketClient($cfg);
try {
// Delete a specified Agentic Bucket
$request = new Models\DeleteAgenticBucketRequest($bucket);
$result = $client->deleteAgenticBucket($request);
printf(
'status code: %d' . PHP_EOL . 'request id: %s' . PHP_EOL,
$result->statusCode,
$result->requestId
);
} catch (\Throwable $e) {
printf("error: %s" . PHP_EOL, $e->getMessage());
exit(1);
}Create a CnameToken (wildcard domain name)
The following code applies for a CnameToken for the base domain name of a wildcard CNAME on an Agentic Bucket.
<?php
// Import the autoloader to load the dependencies
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the command-line argument descriptions, including the required region, account ID, and base domain name, and the optional endpoint
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"account-id" => ['help' => 'The ID of the Alibaba Cloud account that owns the bucket.', 'required' => True],
"bucket" => ['help' => 'The prefix of the agentic bucket name.', 'required' => True],
"domain" => ['help' => 'The base domain name of the wildcard domain, without the "*." prefix.', 'required' => True],
];
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
$options = getopt("", $longopts);
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
$region = $options["region"];
$accountId = $options["account-id"];
$bucketPrefix = $options["bucket"];
$domain = $options["domain"];
// Load access credentials from environment variables for authentication
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Create a configuration object with the SDK default configuration. CNAME-related operations are provided by the standard client Oss\Client,
// and the bucket parameter in the request must be set to the full name of the Agentic Bucket
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Initialize the standard OSS client with the preceding configuration
$client = new Oss\Client($cfg);
// The full name of the Agentic Bucket is {prefix}-{accountId}-{region}-ab-apsr
$agenticBucket = sprintf('%s-%s-%s-ab-apsr', $bucketPrefix, $accountId, $region);
try {
// Apply for a CnameToken for the base domain name. Set domain to the base domain name without the *. prefix,
// Set isWildCard to true to apply for a CnameToken for a wildcard CNAME
$request = new Oss\Models\CreateCnameTokenRequest(
$agenticBucket,
new Oss\Models\BucketCnameConfiguration(
new Oss\Models\Cname($domain, null, true)
)
);
$result = $client->createCnameToken($request);
printf(
'status code: %d' . PHP_EOL . 'request id: %s' . PHP_EOL,
$result->statusCode,
$result->requestId
);
if ($result->cnameToken !== null) {
printf('cname: %s' . PHP_EOL, $result->cnameToken->cname);
printf('token: %s' . PHP_EOL, $result->cnameToken->token);
}
} catch (\Throwable $e) {
printf("error: %s" . PHP_EOL, $e->getMessage());
exit(1);
}Get a CnameToken (wildcard domain name)
The following code gets the CnameToken of a wildcard CNAME on an Agentic Bucket.
<?php
// Import the autoloader to load the dependencies
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the command-line argument descriptions, including the required region, account ID, and base domain name, and the optional endpoint
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"account-id" => ['help' => 'The ID of the Alibaba Cloud account that owns the bucket.', 'required' => True],
"bucket" => ['help' => 'The prefix of the agentic bucket name.', 'required' => True],
"domain" => ['help' => 'The base domain name of the wildcard domain, without the "*." prefix.', 'required' => True],
];
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
$options = getopt("", $longopts);
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
$region = $options["region"];
$accountId = $options["account-id"];
$bucketPrefix = $options["bucket"];
$domain = $options["domain"];
// Load access credentials from environment variables for authentication
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Create a configuration object with the SDK default configuration. CNAME-related operations are provided by the standard client Oss\Client,
// and the bucket parameter in the request must be set to the full name of the Agentic Bucket
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Initialize the standard OSS client with the preceding configuration
$client = new Oss\Client($cfg);
// The full name of the Agentic Bucket is {prefix}-{accountId}-{region}-ab-apsr
$agenticBucket = sprintf('%s-%s-%s-ab-apsr', $bucketPrefix, $accountId, $region);
try {
// Get the CnameToken of the base domain name. Set cname to the base domain name without the *. prefix,
// Set wildcard to true to get the CnameToken of a wildcard CNAME
$request = new Oss\Models\GetCnameTokenRequest($agenticBucket, $domain, null, true);
$result = $client->getCnameToken($request);
printf(
'status code: %d' . PHP_EOL . 'request id: %s' . PHP_EOL,
$result->statusCode,
$result->requestId
);
if ($result->cnameToken !== null) {
printf('cname: %s' . PHP_EOL, $result->cnameToken->cname);
printf('token: %s' . PHP_EOL, $result->cnameToken->token);
}
} catch (\Throwable $e) {
printf("error: %s" . PHP_EOL, $e->getMessage());
exit(1);
}Bind a wildcard CNAME
The following code binds a wildcard CNAME to an Agentic Bucket.
<?php
// Import the autoloader to load the dependencies
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the command-line argument descriptions, including the required region, account ID, and base domain name, and the optional endpoint
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"account-id" => ['help' => 'The ID of the Alibaba Cloud account that owns the bucket.', 'required' => True],
"bucket" => ['help' => 'The prefix of the agentic bucket name.', 'required' => True],
"domain" => ['help' => 'The base domain name of the wildcard domain, without the "*." prefix.', 'required' => True],
];
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
$options = getopt("", $longopts);
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
$region = $options["region"];
$accountId = $options["account-id"];
$bucketPrefix = $options["bucket"];
$domain = $options["domain"];
// Load access credentials from environment variables for authentication
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Create a configuration object with the SDK default configuration. CNAME-related operations are provided by the standard client Oss\Client,
// and the bucket parameter in the request must be set to the full name of the Agentic Bucket
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Initialize the standard OSS client with the preceding configuration
$client = new Oss\Client($cfg);
// The full name of the Agentic Bucket is {prefix}-{accountId}-{region}-ab-apsr
$agenticBucket = sprintf('%s-%s-%s-ab-apsr', $bucketPrefix, $accountId, $region);
try {
// Bind a wildcard CNAME to an Agentic Bucket. Set domain to the base domain name without the *. prefix,
// Set isWildCard to true to bind a wildcard CNAME
$request = new Oss\Models\PutCnameRequest(
$agenticBucket,
new Oss\Models\BucketCnameConfiguration(
new Oss\Models\Cname($domain, null, true)
)
);
$result = $client->putCname($request);
printf(
'status code: %d' . PHP_EOL . 'request id: %s' . PHP_EOL,
$result->statusCode,
$result->requestId
);
} catch (\Throwable $e) {
printf("error: %s" . PHP_EOL, $e->getMessage());
exit(1);
}List CNAME records
The following code lists the CNAME records of an Agentic Bucket.
<?php
// Import the autoloader to load the dependencies
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the command-line argument descriptions, including the required region and account ID, and the optional endpoint
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"account-id" => ['help' => 'The ID of the Alibaba Cloud account that owns the bucket.', 'required' => True],
"bucket" => ['help' => 'The prefix of the agentic bucket name.', 'required' => True],
];
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
$options = getopt("", $longopts);
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
$region = $options["region"];
$accountId = $options["account-id"];
$bucketPrefix = $options["bucket"];
// Load access credentials from environment variables for authentication
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Create a configuration object with the SDK default configuration and initialize the standard OSS client
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
$client = new Oss\Client($cfg);
// The full name of the Agentic Bucket is {prefix}-{accountId}-{region}-ab-apsr
$agenticBucket = sprintf('%s-%s-%s-ab-apsr', $bucketPrefix, $accountId, $region);
try {
// List the CNAME records of an Agentic Bucket
$request = new Oss\Models\ListCnameRequest($agenticBucket);
$result = $client->listCname($request);
printf(
'status code: %d' . PHP_EOL . 'request id: %s' . PHP_EOL,
$result->statusCode,
$result->requestId
);
foreach ($result->cnames ?? [] as $info) {
printf('domain: %s, status: %s' . PHP_EOL, $info->domain, $info->status);
}
} catch (\Throwable $e) {
printf("error: %s" . PHP_EOL, $e->getMessage());
exit(1);
}Delete a wildcard CNAME
The following code deletes a wildcard CNAME from an Agentic Bucket.
<?php
// Import the autoloader to load the dependencies
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the command-line argument descriptions, including the required region, account ID, and base domain name, and the optional endpoint
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"account-id" => ['help' => 'The ID of the Alibaba Cloud account that owns the bucket.', 'required' => True],
"bucket" => ['help' => 'The prefix of the agentic bucket name.', 'required' => True],
"domain" => ['help' => 'The base domain name of the wildcard domain, without the "*." prefix.', 'required' => True],
];
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
$options = getopt("", $longopts);
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
$region = $options["region"];
$accountId = $options["account-id"];
$bucketPrefix = $options["bucket"];
$domain = $options["domain"];
// Load access credentials from environment variables for authentication
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Create a configuration object with the SDK default configuration. CNAME-related operations are provided by the standard client Oss\Client,
// and the bucket parameter in the request must be set to the full name of the Agentic Bucket
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Initialize the standard OSS client with the preceding configuration
$client = new Oss\Client($cfg);
// The full name of the Agentic Bucket is {prefix}-{accountId}-{region}-ab-apsr
$agenticBucket = sprintf('%s-%s-%s-ab-apsr', $bucketPrefix, $accountId, $region);
try {
// Delete a wildcard CNAME from an Agentic Bucket. Set domain to the base domain name without the *. prefix,
// Set isWildCard to true to delete a wildcard CNAME
$request = new Oss\Models\DeleteCnameRequest(
$agenticBucket,
new Oss\Models\BucketCnameConfiguration(
new Oss\Models\Cname($domain, null, true)
)
);
$result = $client->deleteCname($request);
printf(
'status code: %d' . PHP_EOL . 'request id: %s' . PHP_EOL,
$result->statusCode,
$result->requestId
);
} catch (\Throwable $e) {
printf("error: %s" . PHP_EOL, $e->getMessage());
exit(1);
}Create a Bucket Space
The following code creates a Bucket Space under a specified Agentic Bucket. The SDK provides two ways to access a Bucket Space. The following samples demonstrate each way, and you can choose one as needed.
Method 1: Use BucketSpaceClient. In the request, the bucket parameter is set to the Bucket Space name prefix.
<?php
// Import the autoloader to load the dependencies
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
use AlibabaCloud\Oss\V2\Agentic\BucketSpaceClient;
// Define the command-line argument descriptions, including the required region, Bucket Space name prefix, account ID,
// the name prefix of the parent Agentic Bucket, and the optional endpoint
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"account-id" => ['help' => 'The ID of the Alibaba Cloud account that owns the bucket.', 'required' => True],
"bucket" => ['help' => 'The bucket space prefix name.', 'required' => True],
"agentic-bucket" => ['help' => 'The agentic bucket that the bucket space belongs to.', 'required' => True],
];
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
$options = getopt("", $longopts);
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
$region = $options["region"];
$accountId = $options["account-id"];
$bucket = $options["bucket"];
$agenticBucket = $options["agentic-bucket"];
// Load access credentials from environment variables for authentication
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Create a configuration object with the SDK default configuration. BucketSpaceClient resolves
// the Bucket Space name prefix to the full name automatically: {prefix}-{accountId}-{region}-bs-apsr
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
$cfg->setAccountId($accountId);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create a Bucket Space client. This client reuses standard OSS bucket and object operations
$client = BucketSpaceClient::newClient($cfg);
try {
// Create a Bucket Space under a specified Agentic Bucket. The bucket parameter of PutBucketRequest
// is the Bucket Space name prefix, and the agenticBucket parameter is the full name of the parent Agentic Bucket
$request = new Oss\Models\PutBucketRequest($bucket);
$request->agenticBucket = sprintf('%s-%s-%s-ab-apsr', $agenticBucket, $accountId, $region);
$result = $client->putBucket($request);
printf(
'status code: %d' . PHP_EOL . 'request id: %s' . PHP_EOL,
$result->statusCode,
$result->requestId
);
} catch (\Throwable $e) {
printf("error: %s" . PHP_EOL, $e->getMessage());
exit(1);
}Method 2: Use BucketSpaceHelper and the standard client Oss\Client. In the request, the bucket parameter is set to the converted full name of the Bucket Space.
<?php
// Import the autoloader to load the dependencies
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
use AlibabaCloud\Oss\V2\Agentic\BucketSpaceHelper;
// Define the command-line argument descriptions, the same as Method 1
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"account-id" => ['help' => 'The ID of the Alibaba Cloud account that owns the bucket.', 'required' => True],
"bucket" => ['help' => 'The bucket space prefix name.', 'required' => True],
"agentic-bucket" => ['help' => 'The agentic bucket that the bucket space belongs to.', 'required' => True],
];
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
$options = getopt("", $longopts);
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
$region = $options["region"];
$accountId = $options["account-id"];
$bucket = $options["bucket"];
$agenticBucket = $options["agentic-bucket"];
// Load access credentials from environment variables for authentication
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Create a configuration object with the SDK default configuration and initialize the standard OSS client
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
$client = new Oss\Client($cfg);
// Create a BucketSpaceHelper. Based on the account ID and region in the configuration, the helper
// Convert the Bucket Space name prefix to the full name {prefix}-{accountId}-{region}-bs-apsr
$helper = new BucketSpaceHelper($cfg);
try {
// Create a Bucket Space under a specified Agentic Bucket. The bucket parameter of PutBucketRequest
// is the full Bucket Space name converted by helper, and the agenticBucket parameter is the full name of the parent Agentic Bucket
$request = new Oss\Models\PutBucketRequest($helper->toBucketName($bucket));
$request->agenticBucket = sprintf('%s-%s-%s-ab-apsr', $agenticBucket, $accountId, $region);
$result = $client->putBucket($request);
printf(
'status code: %d' . PHP_EOL . 'request id: %s' . PHP_EOL,
$result->statusCode,
$result->requestId
);
} catch (\Throwable $e) {
printf("error: %s" . PHP_EOL, $e->getMessage());
exit(1);
}Upload an object to a Bucket Space
The following code uploads an object to a specified Bucket Space. The following samples demonstrate the two access methods.
Method 1: Use BucketSpaceClient. In the request, the bucket parameter is set to the Bucket Space name prefix.
<?php
// Import the autoloader to load the dependencies
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
use AlibabaCloud\Oss\V2\Agentic\BucketSpaceClient;
// Define the command-line argument descriptions, including the required region, Bucket Space name prefix, account ID,
// the file name, and the optional endpoint
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"account-id" => ['help' => 'The ID of the Alibaba Cloud account that owns the bucket.', 'required' => True],
"bucket" => ['help' => 'The bucket space prefix name.', 'required' => True],
"key" => ['help' => 'The name of the object.', 'required' => True],
];
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
$options = getopt("", $longopts);
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
$region = $options["region"];
$accountId = $options["account-id"];
$bucket = $options["bucket"];
$key = $options["key"];
// Load access credentials from environment variables for authentication
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Create a configuration object with the SDK default configuration. BucketSpaceClient resolves
// the Bucket Space name prefix to the full name automatically: {prefix}-{accountId}-{region}-bs-apsr
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
$cfg->setAccountId($accountId);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create a Bucket Space client. This client reuses standard OSS bucket and object operations
$client = BucketSpaceClient::newClient($cfg);
try {
// Upload an object to a specified Bucket Space
$request = new Oss\Models\PutObjectRequest($bucket, $key);
$request->body = Oss\Utils::streamFor('Hello OSS');
$result = $client->putObject($request);
printf(
'status code: %d' . PHP_EOL . 'request id: %s' . PHP_EOL . 'etag: %s' . PHP_EOL,
$result->statusCode,
$result->requestId,
$result->etag
);
} catch (\Throwable $e) {
printf("error: %s" . PHP_EOL, $e->getMessage());
exit(1);
}Method 2: Use BucketSpaceHelper and the standard client Oss\Client. In the request, the bucket parameter is set to the converted full name of the Bucket Space.
<?php
// Import the autoloader to load the dependencies
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
use AlibabaCloud\Oss\V2\Agentic\BucketSpaceHelper;
// Define the command-line argument descriptions, the same as Method 1
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
"account-id" => ['help' => 'The ID of the Alibaba Cloud account that owns the bucket.', 'required' => True],
"bucket" => ['help' => 'The bucket space prefix name.', 'required' => True],
"key" => ['help' => 'The name of the object.', 'required' => True],
];
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
$options = getopt("", $longopts);
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help";
exit(1);
}
}
$region = $options["region"];
$accountId = $options["account-id"];
$bucket = $options["bucket"];
$key = $options["key"];
// Load access credentials from environment variables for authentication
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Create a configuration object with the SDK default configuration and initialize the standard OSS client
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
$client = new Oss\Client($cfg);
// Create a BucketSpaceHelper to convert the Bucket Space name prefix to the full name
// {prefix}-{accountId}-{region}-bs-apsr
$helper = new BucketSpaceHelper($cfg);
try {
// Upload an object to a specified Bucket Space. The bucket parameter of PutObjectRequest
// is the full Bucket Space name converted by helper
$request = new Oss\Models\PutObjectRequest($helper->toBucketName($bucket), $key);
$request->body = Oss\Utils::streamFor('Hello OSS');
$result = $client->putObject($request);
printf(
'status code: %d' . PHP_EOL . 'request id: %s' . PHP_EOL . 'etag: %s' . PHP_EOL,
$result->statusCode,
$result->requestId,
$result->etag
);
} catch (\Throwable $e) {
printf("error: %s" . PHP_EOL, $e->getMessage());
exit(1);
}References
For the complete sample code for creating an Agentic Bucket, see AgenticCreateBucket.php.
For the complete sample code for querying Agentic Bucket information, see AgenticGetBucket.php.
For the complete sample code for listing Agentic Buckets, see AgenticListBuckets.php.
For the complete sample code for deleting an Agentic Bucket, see AgenticDeleteBucket.php.
For the complete sample code for creating the CnameToken required for domain ownership verification, see CreateCnameToken.php.
For the complete sample code for getting a CnameToken, see GetCnameToken.php.
For the complete sample code for adding a CNAME record, see PutCname.php.
For the complete sample code for viewing CNAME records, see ListCname.php.
For the complete sample code for deleting a CNAME record, see DeleteCname.php.
For the complete sample code for creating a Bucket Space, see BucketSpacePutBucket.php.
For the complete sample code for uploading an object to a Bucket Space, see BucketSpacePutObject.php.
For more information about the parameters of CNAME-related operations, see CreateCnameToken, PutCname, ListCname, GetCnameToken and DeleteCname.