Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Simple upload (iOS SDK)

Son Güncelleme:Oct 01, 2026

Bu konuda, bellekten veya yerel bir dosyadan nasıl dosya yükleneceği anlatılmaktadır. Yükleme sırasında veri bütünlüğünü sağlamak için MD5 doğrulamasını da kullanabilirsiniz.

Notlar

Bu konudaki örnek kodu kullanmadan önce özel bir alan adı veya Security Token Service (STS) kullanarak bir OSSClient örneği oluşturun. Daha fazla bilgi için bkz. Başlatma (iOS SDK).

Not

Paketin bölgesi, başlatma sırasında belirtilen uç noktaya bağlıdır.

İzinler

Bir Alibaba Cloud hesabı varsayılan olarak tam izinlere sahiptir. Bir Alibaba Cloud hesabı altındaki RAM kullanıcıları veya RAM rolleri ise varsayılan olarak herhangi bir izne sahip değildir. Alibaba Cloud hesabı sahibi veya hesap yöneticisi, işlem izinlerini RAM ilkeleri veya Paket İlkesi aracılığıyla vermelidir.

API

Eylem

Açıklama

PutObject

oss:PutObject

Bir nesne yükler.

oss:PutObjectTagging

Bir nesne yüklerken x-oss-tagging üstbilgisini kullanarak nesne etiketleri belirtiyorsanız gereklidir.

kms:GenerateDataKey

Bir nesne yüklerken X-Oss-Server-Side-Encryption: KMS üstbilgisi KMS olarak ayarlanmışsa gereklidir.

kms:Decrypt

Bellekten veya yerel bir dosyadan dosya yükleme

Dosya yüklerken doğrudan OSSData yükleyebilir veya bir NSURL kullanarak dosyayı yükleyebilirsiniz.

OSSPutObjectRequest * put = [OSSPutObjectRequest new];

// Specify the bucket name. For example, examplebucket.
put.bucketName = @"examplebucket";
// Specify the full path of the object. For example, exampledir/exampleobject.txt. The full path cannot contain the bucket name.
put.objectKey = @"exampledir/exampleobject.txt";
put.uploadingFileURL = [NSURL fileURLWithPath:@"<filePath>"];
// put.uploadingData = <NSData *>; // Directly upload NSData.

// (Optional) Set the upload progress.
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
    // The current upload length, the total uploaded length, and the total length to upload.
    NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
};
// Configure optional fields.
// put.contentType = @"application/octet-stream";
// Set Content-MD5.
// put.contentMd5 = @"eB5eJF1ptWaXm4bijSPyxw==";
// Set the codec of the object.
// put.contentEncoding = @"identity";
// Set the display format of the object.
// put.contentDisposition = @"attachment";
// You can set object metadata or HTTP headers when you upload a file.
// NSMutableDictionary *meta = [NSMutableDictionary dictionary];
// Set the file metadata.
// [meta setObject:@"value" forKey:@"x-oss-meta-name1"];
// Set the access permissions of the object to private.
// [meta setObject:@"private" forKey:@"x-oss-object-acl"];
// Set the storage class of the object to Standard.
// [meta setObject:@"Standard" forKey:@"x-oss-storage-class"];
// Overwrite the destination object that has the same name.
// [meta setObject:@"true" forKey:@"x-oss-forbid-overwrite"];
// Specify object tags. You can specify multiple tags.
// [meta setObject:@"a:1" forKey:@"x-oss-tagging"];
// Specify the server-side encryption algorithm that OSS uses to create the destination object.
// [meta setObject:@"AES256" forKey:@"x-oss-server-side-encryption"];
// The customer master key (CMK) that is managed by KMS. This parameter is valid only when x-oss-server-side-encryption is set to KMS.
// [meta setObject:@"9468da86-3509-4f8d-a61e-6eab1eac****" forKey:@"x-oss-server-side-encryption-key-id"];
// put.objectMeta = meta;
OSSTask * putTask = [client putObject:put];

[putTask continueWithBlock:^id(OSSTask *task) {
    if (!task.error) {
        NSLog(@"upload object success!");
    } else {
        NSLog(@"upload object failed, error: %@" , task.error);
    }
    return nil;
}];
// waitUntilFinished blocks the current thread, but does not block the upload process.
// [putTask waitUntilFinished];
// [put cancel];

Dosyayı bir dizine yükleme

OSS'te klasör kavramı bulunmaz. Tüm öğeler dosya olarak depolanır. OSS, klasörleri simüle etmek için bir yöntem sunar. Bir klasörü simüle etmek için adı eğik çizgi (/) ile biten bir dosya oluşturun. Bu sayede dosyaları bu dizine yükleyebilirsiniz. Konsol, adı eğik çizgi (/) ile biten dosyaları klasör olarak görüntüler.

Örneğin, bir dosya yüklerken objectKey değerini folder/subfolder/file olarak ayarlarsanız dosya folder/subfolder/ dizinine yüklenir.

Not

Varsayılan yol kök dizindir. Yolun eğik çizgi (/) ile başlaması gerekmez.

Yükleme sırasında contentType ayarlama ve MD5 doğrulaması kullanma

İstemci tarafından gönderilen verilerin OSS sunucusu tarafından alınan verilerle aynı olduğundan emin olmak için dosya yüklerken bir contentMd5 değeri ekleyebilirsiniz. OSS sunucusu bu MD5 değerini doğrulama için kullanır. Dosya yüklerken contentType değerini açıkça belirtebilirsiniz. contentType belirtmezseniz SDK, içerik türünü yükleme dosyasının adına veya objectKey değerine göre otomatik olarak belirler.

Önemli

MD5 doğrulaması performansı düşürebilir.

SDK, Base64 ve contentMd5 değerlerini hesaplamak için kullanışlı bir yöntem sağlar.

OSSPutObjectRequest * put = [OSSPutObjectRequest new];
// Specify the bucket name. For example, examplebucket.
put.bucketName = @"examplebucket";
// Specify the full path of the object. For example, exampledir/exampleobject.txt. The full path cannot contain the bucket name.
put.objectKey = @"exampledir/exampleobject.txt";
put.uploadingFileURL = [NSURL fileURLWithPath:@"<filePath>"];
// Directly upload NSData.
// put.uploadingData = <NSData *>; 
// (Optional) Set contentType.
put.contentType = @"application/octet-stream";
// (Optional) Set contentMd5 validation.
// Set contentMd5 validation for the file path.
put.contentMd5 = [OSSUtil base64Md5ForFilePath:@"<filePath>"]; 
// Set contentMd5 validation for the binary data.
// put.contentMd5 = [OSSUtil base64Md5ForData:<NSData *>];
// (Optional) Set the upload progress.
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
    // The current upload length, the total uploaded length, and the total length to upload.
    NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
};
OSSTask * putTask = [client putObject:put];
[putTask continueWithBlock:^id(OSSTask *task) {
    if (!task.error) {
        NSLog(@"upload object success!");
    } else {
        NSLog(@"upload object failed, error: %@" , task.error);
    }
    return nil;
}];
// waitUntilFinished blocks the current thread, but does not block the upload process.
// [putTask waitUntilFinished];
// [put cancel];

İlgili Konular