OssClient, Object Storage Service (OSS) için C# istemcisidir. Paketler ve nesneler gibi OSS kaynaklarını yönetmek için bu istemciyi kullanabilirsiniz.
Bir OssClient oluşturun
Aşağıdaki yöntemlerden birini kullanarak bir OssClient örneği oluşturabilirsiniz.
V4 imzası (Önerilir)
V4 imza algoritması daha güvenlidir. İstemciyi V4 imzasıyla başlatmak için Uç Nokta'yı ve genel amaçlı Alibaba Cloud Bölge Kimliğini belirtin. Bölge Kimliği, isteğin bölgesini tanımlar. Örneğin, cn-hangzhou. Ayrıca SignVersion.V4 bildiriminde bulunmanız gerekir. V4 imzalar, OSS .NET SDK 2.14.0 ve sonraki sürümlerinde desteklenir.
Aşağıdaki örnek, bir V4 imzası ve bir OSS alan adı ile bir OssClient örneğinin nasıl oluşturulacağını göstermektedir. Bu örneği özel bir alan adı veya Security Token Service (STS) kullanımı gibi diğer senaryolar için uyarlayabilirsiniz.
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);
V1 imzası (Önerilmez)
1 Mart 2025 tarihinden itibaren Alibaba Cloud Object Storage Service (OSS), yeni kullanıcılar (yani yeni UID'ler) için V1 imzasını sunmamaktadır. 1 Eylül 2025 tarihinden itibaren OSS, V1 imzasını güncellemeyi ve sürdürmeyi durduracak ve yeni nesne depolama paketleri için V1 imzasını sunmayacaktır. Hizmet kesintilerini önlemek için mümkün olan en kısa sürede V4 imzasına geçiş yapın. Daha fazla bilgi için bkz. Duyuru.
Bir OssClient örneği oluşturmak için bir OSS uç noktası kullanın
Aşağıdaki kod, bir OSS alan adı kullanarak bir OssClient örneğinin nasıl oluşturulacağını göstermektedir.
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);
Bir OSSClient örneği oluşturmak için özel bir alan adı kullanın
Aşağıdaki kod, özel bir alan adı kullanarak bir OssClient örneğinin nasıl oluşturulacağını göstermektedir.
Özel bir alan adı kullandığınızda ossClient.listBuckets yöntemi desteklenmez.
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);
STS ile bir OssClient oluşturun
Aşağıdaki kod, Security Token Service (STS) kullanarak bir OssClient örneğinin nasıl oluşturulacağını göstermektedir.
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);
OssClient'ı yapılandırın
ClientConfiguration, OssClient için yapılandırma sınıfıdır. Bu sınıfı proxy, bağlantı zaman aşımı ve maksimum bağlantı sayısı gibi parametreleri yapılandırmak için kullanabilirsiniz. Aşağıdaki parametreler yapılandırılabilir:
|
Parametre |
Açıklama |
Varsayılan değer |
|
ConnectionLimit |
Eşzamanlı bağlantıların maksimum sayısı. |
512 |
|
MaxErrorRetry |
Başarısız bir istek için yapılacak maksimum yeniden deneme sayısı. |
3 |
|
ConnectionTimeout |
Milisaniye cinsinden bağlantı zaman aşımı süresi. |
-1 (zaman aşımı yok) |
|
EnableMD5Check |
Veri yükleme veya indirme işlemleri için MD5 doğrulamasının etkinleştirilip etkinleştirilmeyeceğini belirtir.
Önemli
MD5 doğrulaması bazı performans ek yüklerine neden olur. |
false |
|
IsCname |
Uç Nokta'nın CNAME'i destekleyip desteklemediğini belirtir. CNAME, bir pakete özel bir alan adı eklemek için kullanılır. |
false |
|
ProgressUpdateInterval |
Bayt cinsinden ilerleme çubuğu grafiğinin güncelleme aralığı. |
8096 |
|
ProxyHost |
Proxy sunucusu. Örneğin, |
Yok |
|
ProxyPort |
Proxy bağlantı noktası. Örneğin, |
Yok |
|
ProxyUserName |
Proxy hizmeti için kullanıcı adı. Bu parametre isteğe bağlıdır. |
Yok |
|
ProxyPassword |
Proxy hizmeti için parola. Bu parametre isteğe bağlıdır. |
Yok |
Aşağıdaki kod, OssClient'ın nasıl yapılandırılacağını göstermektedir.
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);