OssClient est le client C# pour Object Storage Service (OSS). Il vous permet de gérer les ressources OSS, telles que les buckets et les objets.
Créer un OssClient
Créez une instance OssClient en utilisant l'une des méthodes suivantes.
Signature V4 (recommandée)
L'algorithme de signature V4 offre une sécurité accrue. Pour initialiser le client avec une signature V4, spécifiez l'endpoint et l'ID de région Alibaba Cloud à usage général. L'ID de région identifie la région de la requête, par exemple cn-hangzhou. Déclarez également SignVersion.V4. Les signatures V4 sont prises en charge par le SDK OSS .NET 2.14.0 et versions ultérieures.
L'exemple suivant montre comment créer une instance OssClient avec une signature V4 et un nom de domaine OSS. Adaptez cet exemple à d'autres scénarios, tels que l'utilisation d'un nom de domaine personnalisé ou du Security Token Service (STS).
using Aliyun.OSS;
using Aliyun.OSS.Common;
// 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 set.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the Endpoint for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
const string endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the Region ID for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region ID to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters as needed.
var conf = new ClientConfiguration();
// Set the signature version to V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
Signature V1 (non recommandée)
Utiliser un endpoint OSS pour créer une instance OssClient
Le code suivant montre comment créer une instance OssClient en utilisant un nom de domaine OSS.
using Aliyun.OSS;
// 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 set.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the Endpoint for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
const string endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Create an OssClient instance.
var ossClient = new OssClient(endpoint, accessKeyId, accessKeySecret);
Utiliser un nom de domaine personnalisé pour créer une instance OSSClient
Le code suivant montre comment créer une instance OssClient en utilisant un nom de domaine personnalisé.
Lorsque vous utilisez un nom de domaine personnalisé, la méthode ossClient.listBuckets n'est pas prise en charge.
using Aliyun.OSS;
using Aliyun.OSS.Common;
// 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 set.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the custom domain name.
const string endpoint = "yourDomain";
// Create a ClientConfiguration instance and modify the default parameters as needed.
var conf = new ClientConfiguration();
// Enable CNAME. CNAME is the procedure to attach a custom domain name to a bucket.
conf.IsCname = true;
// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
Créer un OssClient avec STS
Le code suivant montre comment créer une instance OssClient en utilisant Security Token Service (STS).
using Aliyun.OSS;
// Before you run this sample code, make sure that you have used the temporary AccessKey pair obtained from STS to set the YOUR_ACCESS_KEY_ID and YOUR_ACCESS_KEY_SECRET environment variables.
var accessKeyId = Environment.GetEnvironmentVariable("YOUR_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("YOUR_ACCESS_KEY_SECRET");
// The security token obtained from STS.
const string securityToken = "yourSecurityToken";
// Specify the Endpoint for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
const string endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Create an OssClient instance.
var ossClient = new OssClient(endpoint, accessKeyId, accessKeySecret, securityToken);
Configurer l'OssClient
ClientConfiguration est la classe de configuration d'OssClient. Elle permet de configurer des paramètres tels que le proxy, le délai de connexion et le nombre maximal de connexions. Vous pouvez configurer les paramètres suivants :
|
Paramètre |
Description |
Valeur par défaut |
|
ConnectionLimit |
Nombre maximal de connexions simultanées. |
512 |
|
MaxErrorRetry |
Nombre maximal de tentatives pour une requête ayant échoué. |
3 |
|
ConnectionTimeout |
Délai de connexion, en millisecondes. |
-1 (aucun délai) |
|
EnableMD5Check |
Indique s'il faut activer la validation MD5 pour les téléchargements ou les téléversements de données.
Important
La validation MD5 entraîne une certaine surcharge de performance. |
false |
|
IsCname |
Indique si l'endpoint prend en charge le CNAME. Le CNAME est utilisé pour associer un nom de domaine personnalisé à un bucket. |
false |
|
ProgressUpdateInterval |
Intervalle de mise à jour de la barre de progression, en octets. |
8096 |
|
ProxyHost |
Serveur proxy. Par exemple, |
Aucun |
|
ProxyPort |
Port du proxy. Par exemple, |
Aucun |
|
ProxyUserName |
Nom d'utilisateur pour le service proxy. Ce paramètre est facultatif. |
Aucun |
|
ProxyPassword |
Mot de passe pour le service proxy. Ce paramètre est facultatif. |
Aucun |
Le code suivant montre comment configurer l'OssClient.
using Aliyun.OSS;
using Aliyun.OSS.Common;
var conf = new ClientConfiguration();
// Set the maximum number of concurrent connections.
ClientConfiguration.ConnectionLimit = 512;
// Set the maximum number of retries for a failed request.
conf.MaxErrorRetry = 3;
// Set the connection timeout period.
conf.ConnectionTimeout = 300;
// Enable MD5 validation.
conf.EnableMD5Check = true;
// Set the proxy server.
conf.ProxyHost = "example.aliyundoc.com";
// Set the proxy port.
conf.ProxyPort = 3128;
// Set the username for the proxy.
conf.ProxyUserName = "user";
// Set the password for the proxy.
conf.ProxyPassword = "password";
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);