This topic describes how to upload a file from memory or a local file. You can also use MD5 validation to ensure data integrity during the upload.
Usage notes
Before you use the sample code in this topic, create an OSSClient instance using a custom domain name or Security Token Service (STS). For more information, see Initialization (iOS SDK).
The region of the bucket depends on the endpoint specified during initialization.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM Policy or Bucket policies.
API | Action | Definition |
PutObject |
| Uploads an object. |
| When uploading an object, if you specify object tags through | |
| When uploading an object, if the object metadata contains | |
|
Upload a file from memory or a local file
When you upload a file, you can directly upload OSSData or upload the file using an NSURL.
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];Upload a file to a directory
OSS does not have the concept of folders. All elements are stored as files. OSS provides a way to simulate folders. To simulate a folder, create a file whose name ends with a forward slash (/). This lets you upload files to that directory. The console displays files that end with a forward slash (/) as folders.
For example, when you upload a file, if you set objectKey to folder/subfolder/file, the file is uploaded to the folder/subfolder/ directory.
The default path is the root directory. The path does not need to start with a forward slash (/).
Set contentType and use MD5 validation during an upload
To ensure that the data sent by the client is the same as the data received by the OSS server, you can add a contentMd5 value when you upload a file. The OSS server uses this MD5 value for validation. You can also explicitly specify the contentType when you upload a file. If you do not specify the contentType, the SDK automatically determines the content type based on the filename or the objectKey of the upload.
MD5 validation may decrease performance.
The SDK provides a convenient method to calculate the Base64 and contentMd5 values.
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];References
For the complete sample code for simple upload, see the GitHub example.
For more information about the API operation for simple upload, see PutObject.
For more information about how to initialize an OSSClient instance, see Initialize an OSSClient instance.