OSSClient, Object Storage Service (OSS) için iOS istemcisidir. Paketler ve nesneler gibi kaynakları yönetmek için yöntemler sunar. SDK'yı kullanarak OSS'ye istek göndermeden önce bir OSSClient örneğini başlatın ve yapılandırın.
Bir OSSClient örneğinin yaşam döngüsü, uygulamanızın yaşam döngüsüyle eşleşmelidir. Uygulama başladığında genel bir OSSClient örneği oluşturun ve uygulama sonlandığında bu örneği yok edin.
Bir OSSClient başlatın
Mobil cihazlar güvenilmeyen ortamlardır. İstekleri imzalamak için AccessKeyId ve AccessKeySecret değerlerini doğrudan cihazda saklamak yüksek bir güvenlik riski oluşturur. Daha iyi güvenlik için Security Token Service (STS) kimlik doğrulamasını veya kendi kendine imzalama modunu kullanın.
Aşağıdaki yollardan birini kullanarak bir OSSClient örneği oluşturun.
STS kullanarak bir OSSClient oluşturun
Aşağıdaki kod, STS kullanarak bir OSSClient'in nasıl oluşturulacağını gösterir.
// Set yourEndpoint to the endpoint of 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.
NSString *endpoint = @"yourEndpoint";
// The temporary AccessKey pair obtained from STS, which consists of an AccessKey ID and an AccessKey secret.
NSString *accessKeyId = @"yourAccessKeyId";
NSString *accessKeySecret = @"yourAccessKeySecret";
// The security token obtained from STS.
NSString *securityToken = @"yourSecurityToken";
NSString *region = @"yourRegion";
id<OSSCredentialProvider> credentialProvider = [[OSSStsTokenCredentialProvider alloc] initWithAccessKeyId:accessKeyId secretKeyId:accessKeySecret securityToken:securityToken];
OSSClientConfiguration *configuration = [OSSClientConfiguration new];
configuration.signVersion = OSSSignVersionV4;
OSSClient *client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credentialProvider clientConfiguration:configuration];
client.region = region;
Özel bir etki alanı adı kullanarak bir OSSClient oluşturun
Aşağıdaki kod, özel bir etki alanı adı kullanarak bir OSSClient'in nasıl oluşturulacağını gösterir.
// Set yourEndpoint to a custom domain name.
NSString *endpoint = @"yourEndpoint";
// The temporary AccessKey pair obtained from STS, which consists of an AccessKey ID and an AccessKey secret.
NSString *accessKeyId = @"yourAccessKeyId";
NSString *accessKeySecret = @"yourAccessKeySecret";
// The security token obtained from STS.
NSString *securityToken = @"yourSecurityToken";
NSString *region = @"yourRegion";
id<OSSCredentialProvider> credentialProvider = [[OSSStsTokenCredentialProvider alloc] initWithAccessKeyId:accessKeyId secretKeyId:accessKeySecret securityToken:securityToken];
OSSClientConfiguration *configuration = [OSSClientConfiguration new];
configuration.signVersion = OSSSignVersionV4;
OSSClient *client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credentialProvider clientConfiguration:configuration];
client.region = region;
Apsara Stack veya özel etki alanı ortamında bir OSSClient oluşturun
Aşağıdaki kod, Apsara Stack veya özel etki alanı ortamında bir OSSClient'in nasıl oluşturulacağını gösterir.
// Set yourEndpoint to the endpoint of the region where the bucket is located.
NSString *endpoint = @"yourEndpoint";
// The temporary AccessKey pair obtained from STS, which consists of an AccessKey ID and an AccessKey secret.
NSString *accessKeyId = @"yourAccessKeyId";
NSString *accessKeySecret = @"yourAccessKeySecret";
// The security token obtained from STS.
NSString *securityToken = @"yourSecurityToken";
NSString *region = @"yourRegion";
id<OSSCredentialProvider> credentialProvider = [[OSSStsTokenCredentialProvider alloc] initWithAccessKeyId:accessKeyId secretKeyId:accessKeySecret securityToken:securityToken];
OSSClientConfiguration *configuration = [OSSClientConfiguration new];
// Skip CNAME resolution.
configuration.cnameExcludeList = @[endpoint];
configuration.signVersion = OSSSignVersionV4;
OSSClient *client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credentialProvider clientConfiguration:configuration];
client.region = region;
Bir OSSClient yapılandırın
ClientConfiguration, OSSClient için yapılandırma sınıfıdır. Proxy, bağlantı zaman aşımı ve maksimum bağlantı sayısı gibi parametreleri yapılandırmak için bu sınıfı kullanın.
|
Parametre |
Açıklama |
Yöntem |
|
maxRetryCount |
Başarısız bir istek için maksimum yeniden deneme sayısı. Varsayılan değer 3'tür. |
configuration.maxRetryCount |
|
maxConcurrentRequestCount |
Eşzamanlı isteklerin maksimum sayısı. Varsayılan değer 5'tir. |
configuration.maxConcurrentRequestCount |
|
enableBackgroundTransmitService |
Arka plan görevlerinin etkinleştirilip etkinleştirilmeyeceğini belirtir. Bu özellik varsayılan olarak devre dışı bırakılmıştır. |
configuration.enableBackgroundTransmitService |
|
backgroundSesseionIdentifier |
Arka plan oturumu için özel tanımlayıcı. Varsayılan değer |
configuration.backgroundSesseionIdentifier |
|
isHttpdnsEnable |
HttpDNS'nin etkinleştirilip etkinleştirilmeyeceğini belirtir.
|
configuration.isHttpdnsEnable |
|
timeoutIntervalForRequest |
İstek zaman aşımı aralığı. Varsayılan değer 15 saniyedir. |
configuration.timeoutIntervalForRequest |
|
timeoutIntervalForResource |
Kaynak zaman aşımı aralığı. Varsayılan değer 7 gündür. |
configuration.timeoutIntervalForResource |
|
proxyHost |
Proxy sunucusunun ana bilgisayar adresi. |
configuration.proxyHost |
|
proxyPort |
Proxy sunucusunun bağlantı noktası. |
configuration.proxyPort |
|
userAgentMark |
HTTP isteklerindeki User-Agent üst bilgisi. |
configuration.userAgentMark |
|
cnameExcludeList |
Bu listedeki öğeler kanonik ad (CNAME) çözümlemesini atlar. |
configuration.cnameExcludeList |
|
crc64Verifiable |
64 bitlik döngüsel yedeklilik denetimi (CRC64) doğrulamasının etkinleştirilip etkinleştirilmeyeceğini belirtir. Geçerli değerler:
|
configuration.crc64Verifiable |
|
isAllowUACarrySystemInfo |
User-Agent'ın sistem bilgilerini taşımasına izin verilip verilmeyeceğini belirtir. Geçerli değerler:
|
configuration.isAllowUACarrySystemInfo |
|
isFollowRedirectsEnable |
HTTP yönlendirmesinin etkinleştirilip etkinleştirilmeyeceğini belirtir. Geçerli değerler:
|
configuration.isFollowRedirectsEnable |
Aşağıdaki kod, OSSClient parametrelerini yapılandırmak için ClientConfiguration'ın nasıl kullanılacağını gösterir.
// Set yourEndpoint to the endpoint of 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.
NSString *endpoint = @"yourEndpoint";
// The temporary AccessKey pair obtained from STS, which consists of an AccessKey ID and an AccessKey secret.
NSString *accessKeyId = @"yourAccessKeyId";
NSString *accessKeySecret = @"yourAccessKeySecret";
// The security token obtained from STS.
NSString *securityToken = @"yourSecurityToken";
NSString *region = @"yourRegion";
id<OSSCredentialProvider> credentialProvider = [[OSSStsTokenCredentialProvider alloc] initWithAccessKeyId:accessKeyId secretKeyId:accessKeySecret securityToken:securityToken];
OSSClientConfiguration *configuration = [OSSClientConfiguration new];
// Use Signature V4.
configuration.signVersion = OSSSignVersionV4;
// The maximum number of retries for a failed request.
configuration.maxRetryCount = 3;
// The maximum number of concurrent requests.
configuration.maxConcurrentRequestCount = 3;
// Specifies whether to enable background tasks.
configuration.enableBackgroundTransmitService = YES;
// The custom identifier for the background session.
configuration.backgroundSesseionIdentifier = @"yourBackgroundSesseionIdentifier";
// Specifies whether to enable HttpDNS.
configuration.isHttpdnsEnable = YES;
// The request timeout interval.
configuration.timeoutIntervalForRequest = 15;
// The resource timeout interval.
configuration.timeoutIntervalForResource = 24 * 60 * 60;
// The host address of the proxy server.
configuration.proxyHost = @"yourProxyHost";
// The port of the proxy server.
configuration.proxyPort = @8080;
// The User-Agent header in HTTP requests.
configuration.userAgentMark = @"yourUserAgent";
// The elements in this list will skip CNAME resolution.
configuration.cnameExcludeList = @[@"yourCname"];
// Specifies whether to enable CRC64 verification.
configuration.crc64Verifiable = YES;
// Specifies whether to allow the User-Agent to carry system information.
configuration.isAllowUACarrySystemInfo = YES;
// Specifies whether to enable HTTP redirection.
configuration.isFollowRedirectsEnable = NO;
OSSClient *client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credentialProvider clientConfiguration:configuration];
client.region = region;
Günlük kaydı tutmayı etkinleştirin
Mobil cihazlar karmaşık ortamlarda çalışır ve OSS SDK belirli bölgelerde veya zamanlarda kullanılamaz hale gelebilir. Geliştiricilerin sorunları teşhis etmesine yardımcı olmak için, günlük kaydı tutma etkinleştirildiğinde OSS SDK günlük bilgilerini yerel olarak kaydedebilir. OSSClient'ı kullanmadan önce başlatın ve günlük kaydını etkinleştirmek için aşağıdaki yöntemi çağırın.
// Log format.
//2017/10/25 11:05:43:863 [Debug]: 17th time: <NSThread: 0x7f8099108580>{number = 3, name = (null)}
//2017/10/25 11:05:43:863 [Debug]: 15th time: <NSThread: 0x7f80976052c0>
//2017/10/25 11:05:43:863 [Debug]: ----------TestDebug------------
// Enable logging.
[OSSLog enableLog];
Günlük dosyaları korumalı alanın Caches/OSSLogs klasöründe depolanır.
Dosyaları kendi sunucunuza yükleyebilir veya günlük dosyalarını yüklemek için Alibaba Cloud Simple Log Service'i kullanabilirsiniz.
OSSTask
-
Tüm API çağrıları bir OSSTask döndürür.
OSSTask * task = [client getObject:get]; -
OSSTask'ı yapılandırabilirsiniz.
-
Eşzamansız bir geri arama uygulamak için OSSTask'a bir devam işlemi ayarlayın.
[task continueWithBlock: ^(OSSTask *task) { // do something ... return nil; }]; -
Eşzamanlı bir geri arama uygulamak için OSSTask'ın tamamlanmasını bekleyin.
[task waitUntilFinished];
-