Bu konuda, PHP için OSS SDK V2 kullanılarak Object Storage Service (OSS) verilerinin yaşam döngüsünün nasıl yönetileceği açıklanmaktadır.
Genel bilgiler
OSS'de depolanan tüm verilere sık sık erişilmez. Ancak veri düzenlemelerine veya arşivleme gereksinimlerine uyum sağlamak için nadiren erişilen verilerin saklanması gerekir. OSS, verilerinizin yaşam döngüsü yönetimini otomatikleştiren yaşam döngüsü kuralları yapılandırmanıza olanak tanır. İki tür yaşam döngüsü kuralı yapılandırabilirsiniz:
Son değiştirilme zamanına dayalı yaşam döngüsü kuralı. Bu kural türü, uzun süredir değiştirilmeyen verileri silmek veya soğuk veriler için daha maliyet etkin bir depolama sınıfına taşımak için kullanılabilir.
Son erişim zamanına dayalı yaşam döngüsü kuralı. Bu kural türü, veri erişim kalıplarını belirlemek ve soğuk verileri otomatik olarak maliyet etkin bir depolama sınıfına taşımak için kullanılabilir. Bu yaklaşım, sıcak ve soğuk veriler için katmanlı depolama uygulanmasını sağlayarak depolama maliyetlerini azaltır.
Notlar
Son değiştirilme zamanına veya son erişim zamanına dayalı bir yaşam döngüsü kuralı yapılandırmadan önce yaşam döngüsü yönetimi özelliğine aşina olduğunuzdan emin olun. Daha fazla bilgi için bkz. Son değiştirilme zamanına dayalı yaşam döngüsü kuralları ve Son erişim zamanına dayalı yaşam döngüsü kuralları.
Bu konudaki örnek kod, Çin (Hangzhou) bölgesinin bölge kimliği olan
cn-hangzhoudeğerini kullanır. Bir paketteki kaynaklara erişmek için varsayılan olarak genel uç nokta kullanılır. Paketin bulunduğu aynı bölgedeki diğer Alibaba Cloud hizmetlerinden paketteki kaynaklara erişmek istiyorsanız dahili uç noktayı kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.Yaşam döngüsü kurallarını yapılandırmak için
oss:PutBucketLifecycleiznine sahip olmanız gerekir. Yaşam döngüsü kurallarını sorgulamak içinoss:GetBucketLifecycleiznine sahip olmanız gerekir. Yaşam döngüsü kurallarını temizlemek içinoss:DeleteBucketLifecycleiznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Özel ilke atama.
Yaşam döngüsü kurallarını yapılandırma
Bu bölümde, bir paket için son değiştirilme zamanına veya son erişim zamanına dayalı yaşam döngüsü kurallarını yapılandıran kod örnekleri sunulmaktadır. Bir paket için yapılandırılan yaşam döngüsü kurallarından birini veya daha fazlasını değiştirebilirsiniz.
Belirtilen ad önekine sahip nesnelerin depolama sınıfını değiştirmek için son değiştirilme zamanına dayalı bir yaşam döngüsü kuralı yapılandırma
Aşağıdaki örnek kod, belirtilen ad önekine sahip nesnelerin depolama sınıfını değiştirmek üzere nesnelerin son değiştirilme zamanına dayalı bir yaşam döngüsü kuralı yapılandırır:
<?php
// Include the autoload file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
use AlibabaCloud\Oss\V2\Models\LifecycleConfiguration;
// Describe command-line parameters.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located', 'required' => True], // (Required) Specify the region in which the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS', 'required' => False], // (Optional) Specify the OSS endpoint.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // (Required) Specify the name of the bucket.
];
// Generate a long options list to parse the command-line parameters.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon (:) to the end of each parameter to indicate that a value is required.
}, array_keys($optsdesc));
// Parse the command-line parameters.
$options = getopt("", $longopts);
// Check whether the required parameters are missing.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help"; // Display the required but missing parameters.
exit(1);
}
}
// Obtain the values of the command-line parameters and assign the values to variables.
$region = $options["region"]; // The region in which the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.
// Load the AccessKey ID and AccessKey secret from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configuration of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Specify the region.
$cfg->setRegion($region);
// If an endpoint is provided, specify the endpoint.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSS client instance.
$client = new Oss\Client($cfg);
// Define a lifecycle rule that moves objects whose names are prefixed with "log/" to IA 30 days after they were last modified.
$lifecycleRule = new Oss\Models\LifecycleRule(
prefix: 'log/', // The prefix of object names.
transitions: array(
new Oss\Models\LifecycleRuleTransition(
days: 30, // Specify the days.
storageClass: 'IA' // Change to IA.
)
),
id: 'rule', // The rule ID.
status: 'Enabled' // Enable the rule.
);
// Create a lifecycle configuration and add lifecycle rules to the configuration.
$lifecycleConfiguration = new LifecycleConfiguration(
rules: array($lifecycleRule)
);
// Create a request to set a lifecycle configuration and pass the lifecycle configuration.
$request = new Oss\Models\PutBucketLifecycleRequest(
bucket: $bucket,
lifecycleConfiguration: $lifecycleConfiguration
);
// Call the putBucketLifecycle method to configure the lifecycle rules.
$result = $client->putBucketLifecycle($request);
// Display the result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP response status code.
'request id:' . $result->requestId . PHP_EOL // The request ID.
);
Belirtilen ad öneki veya etiketlere sahip olmayan nesnelerin depolama sınıfını değiştirmek için son değiştirilme zamanına dayalı bir yaşam döngüsü kuralı yapılandırma
Aşağıdaki örnek kod, "log" ad öneki olmayan veya "key1:value1" etiketi olmayan nesneleri son değiştirilmelerinden 30 gün sonra Sık Erişilen Depolama (IA) sınıfına taşıyan ve son değiştirilme zamanından 100 gün sonra süresi dolan bir yaşam döngüsü kuralı yapılandırır:
<?php
// Include the autoload file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
use AlibabaCloud\Oss\V2\Models\LifecycleConfiguration;
// Describe command-line parameters.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located', 'required' => True], // (Required) Specify the region in which the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS', 'required' => False], // (Optional) Specify the OSS endpoint.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // (Required) Specify the name of the bucket.
];
// Generate a long options list to parse the command-line parameters.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon (:) to the end of each parameter to indicate that a value is required.
}, array_keys($optsdesc));
// Parse the command-line parameters.
$options = getopt("", $longopts);
// Check whether the required parameters are missing.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help"; // Display the required but missing parameters.
exit(1);
}
}
// Obtain the values of the command-line parameters and assign the values to variables.
$region = $options["region"]; // The region in which the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.
// Load the AccessKey ID and AccessKey secret from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configuration of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Specify the region.
$cfg->setRegion($region);
// If an endpoint is provided, specify the endpoint.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSS client instance.
$client = new Oss\Client($cfg);
$lifecycleRule = new Oss\Models\LifecycleRule(
id: 'rule', // The rule ID.
status: 'Enabled' // Enable the rule.
prefix: 'log/', // The prefix of object names.
transitions: array(
new Oss\Models\LifecycleRuleTransition(
days: 30, // Specify the days.
storageClass: 'Archive' // Change the storage class to Archive.
)
),
filter: new Oss\Models\LifecycleRuleFilter( // Specify filters.
not: new Oss\Models\LifecycleRuleNot( // Specify exclusion filters.
prefix: 'log', // Exclude objects whose names contain the "log" prefix.
tag: new Oss\Models\Tag( // Specify tag filters.
key: 'key1',
value: 'value1'
)
)
),
expiration: new Oss\Models\LifecycleRuleExpiration(
days: 100 // Set the object expiration time to 100 days.
)
);
// Create a lifecycle configuration and add lifecycle rules to the configuration.
$lifecycleConfiguration = new LifecycleConfiguration(
rules: array($lifecycleRule)
);
// Create a request to set a lifecycle configuration and pass the lifecycle configuration.
$request = new Oss\Models\PutBucketLifecycleRequest(
bucket: $bucket,
lifecycleConfiguration: $lifecycleConfiguration
);
// Call the putBucketLifecycle method to configure the lifecycle rules.
$result = $client->putBucketLifecycle($request);
// Display the result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP response status code.
'request id:' . $result->requestId . PHP_EOL // The request ID.
);
Depolama sınıfını değiştirmek için son erişim zamanına dayalı bir yaşam döngüsü kuralı yapılandırma
Aşağıdaki örnek kod, depolama sınıfını değiştirmek üzere son erişim zamanına dayalı bir yaşam döngüsü kuralı yapılandırır:
<?php
// Include the autoload file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
use AlibabaCloud\Oss\V2\Models\LifecycleConfiguration;
// Describe command-line parameters.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located', 'required' => True], // (Required) Specify the region in which the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS', 'required' => False], // (Optional) Specify the OSS endpoint.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // (Required) Specify the name of the bucket.
];
// Generate a long options list to parse the command-line parameters.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon (:) to the end of each parameter to indicate that a value is required.
}, array_keys($optsdesc));
// Parse the command-line parameters.
$options = getopt("", $longopts);
// Check whether the required parameters are missing.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help"; // Display the required but missing parameters.
exit(1);
}
}
// Obtain the values of the command-line parameters and assign the values to variables.
$region = $options["region"]; // The region in which the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.
// Load the AccessKey ID and AccessKey secret from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configuration of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Specify the region.
$cfg->setRegion($region);
// If an endpoint is provided, specify the endpoint.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSS client instance.
$client = new Oss\Client($cfg);
{}
$lifecycleRule = new Oss\Models\LifecycleRule(
prefix: 'log/', // The prefix of object names.
transitions: array(
new Oss\Models\LifecycleRuleTransition(
days: 30, // Specify the days.
storageClass: 'IA', // Change to IA.
isAccessTime: 'true', // Trigger the change based on the last access time.
returnToStdWhenVisit: 'true' // Return to the Standard storage class after they are accessed.
)
),
id: 'rule', // The rule ID.
status: 'Enabled' // Enable the rule.
);
// Create a lifecycle configuration and add lifecycle rules to the configuration.
$lifecycleConfiguration = new LifecycleConfiguration(
rules: array($lifecycleRule)
);
// Create a request to set a lifecycle configuration and pass the lifecycle configuration.
$request = new Oss\Models\PutBucketLifecycleRequest(
bucket: $bucket,
lifecycleConfiguration: $lifecycleConfiguration
);
// Call the putBucketLifecycle method to configure the lifecycle rules.
$result = $client->putBucketLifecycle($request);
// Display the result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP response status code.
'request id:' . $result->requestId . PHP_EOL // The request ID.
);
Yaşam döngüsü kurallarını sorgulama
Aşağıdaki kod, bir paket için yapılandırılan yaşam döngüsü kurallarını sorgular:
<?php
// Include the autoload file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Describe command-line parameters.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located', 'required' => True], // (Required) Specify the region in which the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS', 'required' => False], // (Optional) Specify the OSS endpoint.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // (Required) Specify the name of the bucket.
];
// Generate a long options list to parse the command-line parameters.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon (:) to the end of each parameter to indicate that a value is required.
}, array_keys($optsdesc));
// Parse the command-line parameters.
$options = getopt("", $longopts);
// Check whether the required parameters are missing.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help"; // Display the required but missing parameters.
exit(1);
}
}
// Obtain the values of the command-line parameters and assign the values to variables.
$region = $options["region"]; // The region in which the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.
// Load the AccessKey ID and AccessKey secret from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configuration of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Specify the region.
$cfg->setRegion($region);
// If an endpoint is provided, specify the endpoint.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSS client instance.
$client = new Oss\Client($cfg);
// Create a request to query lifecycle rules.
$request = new Oss\Models\GetBucketLifecycleRequest(bucket: $bucket);
// Call the getBucketLifecycle method to query lifecycle rules.
$result = $client->getBucketLifecycle($request);
// Display the result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP response status code.
'request id:' . $result->requestId . PHP_EOL . // The request ID.
'lifecycle:' . var_export($result->lifecycleConfiguration, true) . PHP_EOL // The lifecycle rules.
);
Yaşam döngüsü kurallarını temizleme
Aşağıdaki örnek kod, bir paketin yaşam döngüsü kurallarını temizler. Bir veya daha fazla yaşam döngüsü kuralını silmek için Yaşam döngüsü kurallarını silme bölümündeki talimatları uygulayın.
<?php
// Include the autoload file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Describe command-line parameters.
$optsdesc = [
"region" => ['help' => The region in which the bucket is located.', 'required' => True], // (Required) Specify the region in which the bucket is located.
"endpoint" => ['help' => The domain names that other services can use to access OSS.', 'required' => False], // (Optional) Specify the endpoint that can be used by other services to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // (Required) Specify the name of the bucket.
];
// Generate a long options list to parse the command-line parameters.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon (:) to the end of each parameter to indicate that a value is required.
}, array_keys($optsdesc));
// Parse the command-line parameters.
$options = getopt("", $longopts);
// Check whether the required parameters are missing.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help"; // Display the required but missing parameters.
exit(1);
}
}
// Obtain the values of the command-line parameters and assign the values to variables.
$region = $options["region"]; // The region in which the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.
// Load the AccessKey ID and AccessKey secret from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configuration of the SDK.
$cfg = Oss\Config::loadDefault();
// Specify the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Specify the region.
$cfg->setRegion($region);
// If an endpoint is provided, specify the endpoint.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSS client instance.
$client = new Oss\Client($cfg);
// Create a request to delete lifecycle rules of a bucket.
$request = new Oss\Models\DeleteBucketLifecycleRequest(bucket: $bucket);
// Call the deleteBucketLifecycle method to delete lifecycle rules.
$result = $client->deleteBucketLifecycle($request);
// Display the result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP response status code.
'request id:' . $result->requestId . PHP_EOL // The request ID.
);
Başvurular
Bir yaşam döngüsü kuralı yapılandıran API işlemi hakkında daha fazla bilgi için bkz. PutBucketLifecycle.
Yaşam döngüsü kurallarını sorgulayan API işlemi hakkında daha fazla bilgi için bkz. GetBucketLifecycle.
Yaşam döngüsü kurallarını silen API işlemi hakkında daha fazla bilgi için bkz. DeleteBucketLifecycle.