Cette rubrique explique comment créer un inventaire pour un compartiment, ainsi que comment consulter, répertorier et supprimer les inventaires configurés pour ce compartiment.
Notes d'utilisation
Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un point de terminaison interne. Pour plus d'informations sur les régions et les points de terminaison OSS, consultez Régions et points de terminaison.
Cette rubrique illustre la création d'une instance OSSClient avec un point de terminaison OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Initialisation (kit SDK C# V1).
Assurez-vous de disposer des autorisations nécessaires pour créer, consulter, répertorier et supprimer les inventaires d'un compartiment. Par défaut, le propriétaire du compartiment dispose des autorisations requises pour effectuer ces opérations. Si vous ne possédez pas ces autorisations, demandez au propriétaire du compartiment de vous les accorder.
Vous pouvez configurer jusqu'à 1 000 inventaires pour un compartiment.
Le compartiment source pour lequel vous souhaitez configurer un inventaire doit se trouver dans la même région que le compartiment de destination où la liste d'inventaire est stockée.
Créer un inventaire pour un compartiment
L'exemple de code suivant montre comment créer un inventaire pour un compartiment :
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// 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.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket.
var bucketName = "examplebucket";
// Specify the account ID granted by the bucket owner.
var accountId ="yourDestinationBucketAccountId";
// Specify the name of the RAM role that is granted the permissions to read all objects in the bucket for which you want to configure the inventory and the permissions to write data to the bucket in which you want to store the generated inventory lists.
var roleArn ="yourDestinationBucketRoleArn";
// Specify the name of the bucket in which you want to store the generated inventory lists.
var destBucketName ="yourDestinationBucketName";
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters based on your requirements.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Create an inventory for the bucket.
var config = new InventoryConfiguration();
// Specify the name of the inventory.
config.Id = "report1";
// Specify whether to enable the inventory for the bucket. Valid values: true and false. If this parameter is set to true, the inventory is enabled.
config.IsEnabled = true;
// Specify the rule that is used to filter the objects included in inventory lists. The following code provides an example on how to filter the objects by prefix.
config.Filter = new InventoryFilter("filterPrefix");
// Configure the bucket in which you want to store the generated inventory lists.
config.Destination = new InventoryDestination();
config.Destination.OSSBucketDestination = new InventoryOSSBucketDestination();
// Specify the format of the inventory lists.
config.Destination.OSSBucketDestination.Format = InventoryFormat.CSV;
// Specify the ID of the account to which the destination bucket belongs.
config.Destination.OSSBucketDestination.AccountId = accountId;
// Specify the Alibaba Cloud Resource Name (ARN) of the RAM role that is used to access the destination bucket.
config.Destination.OSSBucketDestination.RoleArn = roleArn;
// Specify the name of the bucket in which you want to store the generated inventory lists.
config.Destination.OSSBucketDestination.Bucket = destBucketName;
// Specify the prefix of the path in which you want to store the generated inventory lists.
config.Destination.OSSBucketDestination.Prefix = "prefix1";
// Specify whether to generate the inventory lists on a daily or weekly basis. The following code provides an example on how to generate the inventory lists on a weekly basis. A value of Weekly indicates that the inventory lists are generated on a weekly basis. A value of Daily indicates that the inventory lists are generated on a daily basis.
config.Schedule = new InventorySchedule(InventoryFrequency.Daily);
// Specify that the inventory lists include only the current versions of objects. If you set the InventoryIncludedObjectVersions parameter to All, all versions of objects are included in the inventory lists. This configuration takes effect only when versioning is enabled for the bucket.
config.IncludedObjectVersions = InventoryIncludedObjectVersions.All;
// Specify the object attributes that are included in the inventory lists.
config.OptionalFields.Add(InventoryOptionalField.Size);
config.OptionalFields.Add(InventoryOptionalField.LastModifiedDate);
config.OptionalFields.Add(InventoryOptionalField.StorageClass);
config.OptionalFields.Add(InventoryOptionalField.IsMultipartUploaded);
config.OptionalFields.Add(InventoryOptionalField.EncryptionStatus);
config.OptionalFields.Add(InventoryOptionalField.ETag);
var req = new SetBucketInventoryConfigurationRequest(bucketName, config);
client.SetBucketInventoryConfiguration(req);
Console.WriteLine("Set bucket:{0} InventoryConfiguration succeeded", bucketName);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
Consulter un inventaire configuré pour un compartiment
L'exemple de code suivant montre comment consulter un inventaire configuré pour un compartiment :
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// 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.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket.
var bucketName = "examplebucket";
// Specify the name of the inventory.
var id = "BucketInventoryConfigurationId";
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters based on your requirements.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Query the inventory that has a specified ID.
var request = new GetBucketInventoryConfigurationRequest(bucketName, id);
// Query the inventory.
var result = client.GetBucketInventoryConfiguration(request);
Console.WriteLine("Get bucket:{0} BucketInventoryConfiguration succeeded ", bucketName);
// Display information about the inventory.
Console.WriteLine("bucket InventoryConfiguration id: {0}; bucket InventoryConfiguration IsEnabled: {1}", result.Configuration.Id, result.Configuration.IsEnabled);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
Répertorier les inventaires configurés pour un compartiment
Vous pouvez consulter jusqu'à 100 inventaires en une seule requête. Pour consulter plus de 100 inventaires, envoyez plusieurs requêtes et utilisez le jeton renvoyé par chaque requête comme paramètre pour la requête suivante.
L'exemple de code suivant montre comment répertorier les inventaires configurés pour un compartiment :
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// 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.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket.
var bucketName = "examplebucket";
// Specify the names of the inventories.
var id = "BucketInventoryConfigurationId";
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters based on your requirements.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// List the inventories. By default, a maximum of 100 inventories are listed. If more than 100 inventories are configured, the inventories are listed in pages. You can use the continuationToken parameter to list the inventories on the subsequent page.
string continuationToken = null;
bool isTruncated = false;
do {
var request = new ListBucketInventoryConfigurationRequest(bucketName, continuationToken)
var result = client.ListBucketInventoryConfiguration(request);
Console.WriteLine("List bucket:{0} BucketInventoryConfiguration succeeded ", bucketName);
// Display information about the inventories.
for (var i = 0; i < result.Configurations.Count; i++) {
Console.WriteLine("bucket InventoryConfiguration id: {0}; bucket InventoryConfiguration IsEnabled: {1}", result.Configurations[i].Id, result.Configurations[i].IsEnabled);
}
continuationToken = result.NextContinuationToken;
isTruncated = result.IsTruncated;
} while (isTruncated)
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
Supprimer un inventaire configuré pour un compartiment
L'exemple de code suivant montre comment supprimer un inventaire configuré pour un compartiment :
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// 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.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket.
var bucketName = "examplebucket";
// Specify the name of the inventory.
var id = "BucketInventoryConfigurationId";
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters based on your requirements.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Delete the inventory with the specified ID.
var request = new DeleteBucketInventoryConfigurationRequest(bucketName, id);
var result = client.DeleteBucketInventoryConfiguration(request);
Console.WriteLine("delete bucket:{0} BucketInventoryConfiguration succeeded ", bucketName);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
Références
Pour plus d'informations sur l'opération API permettant de créer un inventaire pour un compartiment, consultez PutBucketInventory.
Pour plus d'informations sur l'opération API permettant de consulter un inventaire configuré pour un compartiment, consultez GetBucketInventory.
Pour plus d'informations sur l'opération API permettant de répertorier les inventaires configurés pour un compartiment, consultez ListBucketInventory.
Pour plus d'informations sur l'opération API permettant de supprimer les inventaires configurés pour un compartiment, consultez DeleteBucketInventory.