You can download iOS SDK for Cloud Object Storage (COS) from iOS SDK
For more examples, refer to iOS Demo.
iOS SDK package for COS:
In the package, there is a static library (.a file) and a Headers folder containing a header file, as shown below: The upload package is available in two versions: one supporting bitcode and the other not. You can select based on your business needs.
When dragging the unpacked COSSDK into the project directory, Xcode will automatically add it to the list of link libraries.
Note: You can import upload/download SDK package based on your business needs.
Set up "Other Linker Flags" in "Build Settings" and add the parameter "-ObjC".
Add "App Transport Security Settings" to the "info.plist" file of the project, and then add the "Allow Arbitrary Loads" as "Boolean" in "App Transport Security Settings", and set its value to "YES"
Introduce COSClient .h header file of upload SDK. Directory-related operations need first instantiate COSClient object.
- (instancetype)initWithAppId:(NSString*)appId withRegion:(NSString *)region;
Parameter Name | Type | Required | Description |
---|---|---|---|
appId | NSString * | Yes | Project ID, i.e. APP ID. |
region | NSString * | Yes | The data center region where bucket is created. For example, East China: "sh", South China: "gz", and North China: "tj" |
COSClient *client= [[COSClient alloc] initWithAppId:appId withRegion:[Congfig instance].region];
The following shows the basic process of upload and download. For details, please refer to demo. Before that, please apply for an appid for COS business on the Tencent Cloud console;
COSClient *client= [[COSClient alloc] initWithAppId:appId withRegion:[Congfig instance].region];
Let's assume that you have applied for your own business bucket. All tasks of SDK correspond to appropriate "task". You can complete operations by passing corresponding "task" parameters to the client;
COSObjectPutTask *task = [COSObjectPutTask new];
task.filePath = path;
task.fileName = fileName;
task.bucket = bucket;
task.attrs = @"customAttribute";
task.directory = dir;
task.insertOnly = YES;
task.sign = _sign;
COSClient *client= [[COSClient alloc] initWithAppId:appId withRegion:[Congfig instance].region];
client.completionHandler = ^(COSTaskRsp *resp, NSDictionary *context){
if (resp.retCode == 0) {
//sucess
}else{}
};
client.progressHandler = ^(NSInteger bytesWritten,NSInteger totalBytesWritten,NSInteger totalBytesExpectedToWrite){
//progress
};
[client putObject:task];
COSObjectGetTask *cm = [[COSObjectGetTask alloc] initWithUrl:imgUrl.text];
COSClient *client= [[COSClient alloc] initWithAppId:appId withRegion:[Congfig instance].region];
client.completionHandler = ^(COSTaskRsp *resp, NSDictionary *context){
//
};
client.downloadProgressHandler = ^(int64_t receiveLength,int64_t contentLength){
};
[client getObjectRequest:cm];
Signature Types:
Type | Description |
---|---|
Multiple-time | It can be used multiple times before the expiration time |
One-time | It is bound to the source URL and only be used once |
Acquiring Signature:
This is the signature used in mobile device SDK. To secure the key and other information, it is recommended that users create simple key creation and capture services. Then, the client will first request the valid signature from the server.
You can create a directory under specified bucket via this API. The steps are as follows:
Parameter Name | Type | Required | Description |
---|---|---|---|
dir | NSString * | Yes | Directory path (relative to bucket path) |
bucket | NSString * | Yes | Name of bucket to which a directory belongs |
sign | NSString * | Yes | Signature |
attrs | NSString * | No | Custom attributes |
The result is returned through COSCreatDirTaskRsp object.
Attribute Name | Type | Description |
---|---|---|
retCode | int | Task description code |
descMsg | NSString * | Task description message |
COSCreateDirCommand *cm = [COSCreateDirCommand new];
cm.directory = dir;
cm.bucket = bucket;
cm.sign = _sign;
cm.attrs = @"dirTest";
COSClient *client= [[COSClient alloc] initWithAppId:appId withRegion:[Congfig instance].region];
client.completionHandler = ^(COSTaskRsp *resp, NSDictionary *context){
if (resp.retCode == 0) {
//sucess
}else{
//fail
}
};
[client createDir:cm];
You can update the custom attributes of the directory by calling this API. The steps are as follows:
Parameter Name | Type | Required | Description |
---|---|---|---|
dir | NSString * | Yes | Directory path (relative to bucket path) |
bucket | NSString * | Yes | Name of bucket to which a directory belongs |
sign | NSString * | Yes | Signature |
attrs | NSString * | No | Custom attributes |
The result is returned through COSUpdateDirTaskRsp object.
Attribute Name | Type | Description |
---|---|---|
retCode | int | Task description code |
descMsg | NSString * | Task description message |
COSUpdateDirCommand *cm = [COSUpdateDirCommand new];
cm.directory = dir;
cm.bucket = bucket;
cm.sign = _sign;//One-time signature is adopted for this business
cm.attrs = @"dirTest";
COSClient *client= [[COSClient alloc] initWithAppId:appId withRegion:[Congfig instance].region];
client.completionHandler = ^(COSTaskRsp *resp, NSDictionary *context){
if (resp.retCode == 0) {
//sucess
}else{}
};
[client updateDir:cm];
You can query the detailed attributes of the directory by calling this API. The steps are as follows:
Parameter Name | Type | Required | Description |
---|---|---|---|
dir | NSString * | Yes | Directory path (relative to bucket path) |
bucket | NSString * | Yes | Name of bucket to which a directory belongs |
sign | NSString * | Yes | Signature |
The result is returned through COSDirmMetaCommand object.
Attribute Name | Type | Description |
---|---|---|
retCode | int | If the task is normal, return 0; if an error occurs, return other value and the result description |
descMsg | NSString * | If the task is normal, return OK; if an error occurs, return the error description |
data | NSDictionary * | Return the task result data |
COSDirmMetaCommand *cm = [COSDirmMetaCommand new];
cm.directory = dir;
cm.bucket = bucket;
cm.sign = sign;
COSClient *client= [[COSClient alloc] initWithAppId:appId withRegion:[Congfig instance].region];
client.completionHandler = ^(COSTaskRsp *resp, NSDictionary *context){
if (resp.retCode == 0) {
//sucess
}else{}
};
[client getDirMetaData:cm];
You can delete a directory under specified bucket by calling this API. But the directory with valid files or directories cannot be deleted. The steps are as follows:
Parameter Name | Type | Required | Description |
---|---|---|---|
dir | NSString * | Yes | Directory path (relative to bucket path) |
bucket | NSString * | Yes | Name of bucket to which a directory belongs |
sign | NSString * | Yes | Signature |
The result is returned through COSdeleteDirTaskRsp object.
Attribute Name | Type | Description |
---|---|---|
retCode | int | Task description code |
descMsg | NSString * | Task description message |
COSDeleteDirCommand *cm = [COSDeleteDirCommand new];
cm.directory = dir;
cm.bucket = bucket;
cm.sign = _oneSign;//Delete one-time signature
COSClient *client= [[COSClient alloc] initWithAppId:appId withRegion:[Congfig instance].region];
client.completionHandler = ^(COSTaskRsp *resp, NSDictionary *context){
if (resp.retCode == 0) {
//sucess;
}else{}
};
[client deleteDir:cm];
You can list files and directories of specified directory under the bucket by calling this API. The steps are as follows:
Parameter Name | Type | Required | Description |
---|---|---|---|
path | NSString * | Yes | Directory path (relative to bucket path) |
bucket | NSString * | Yes | Name of bucket to which a directory belongs |
sign | NSString * | Yes | Signature |
num | NSUInteger | Yes | Number of each pull |
pageContext | NSString * | Yes | Transparently transmitted field. If you want to query the first page, an empty string should be passed. To turn pages, please transmit context in the returned values of the previous page to the parameters in a transparent way |
prefix | NSString * | Yes | Prefix query |
The result is returned through TXYListDirCommandRsp object.
Attribute Name | Type | Description |
---|---|---|
context | NSString * | Number of directories |
listover | NSString * | Number of files |
infos | NSArray * | List of file directory attributes |
retCode | int | Task description code |
descMsg | NSString * | Task description message |
COSListDirCommand *cm = [COSListDirCommand new];
cm.directory = dir;
cm.bucket = bucket;
cm.sign = _sign;
cm.number = 100;
cm.pageContext = @"";
cm.prefix = @"xx";
COSClient *client= [[COSClient alloc] initWithAppId:appId withRegion:[Congfig instance].region];
client.completionHandler = ^(COSTaskRsp *resp, NSDictionary *context){
if (resp.retCode == 0) {
//sucess
}else{}
};
[client listDir:cm];
Like directory-related operations, file-related operations need first introduce COSClient .h header file of upload SDK and instantiate COSClient object.
Parameter Name | Type | Required | Description |
---|---|---|---|
appId | NSString * | Yes | Project ID, i.e. APP ID. |
region | NSString * | Yes | The data center region where bucket is created. For example, Shanghai: "sh" and Guangzhou: "gz" |
- (instancetype)initWithAppId:(NSString*)appId withRegion:(NSString *)region;
You can upload local files by calling this API. The steps are as follows:
Parameter Name | Type | Required | Description |
---|---|---|---|
filePath | NSString * | Yes | File path |
sign | NSString * | Yes | Signature |
bucket | NSString * | Yes | Target Bucket name |
fileName | NSString * | Yes | The name shown after uploading cos for target files |
attrs | NSString * | No | Custom file attributes |
directory | NSString * | Yes | File upload directory, relative path, for example "/path" |
insertOnly | BOOL | Yes | Upload will insert and overwrite the existing files. But if you set "YES", it will not overwrite the previous upload file |
Return the result through COSObjectUploadTaskRsp object
Attribute Name | Type | Description |
---|---|---|
retCode | int | Task description code. If retCode >= 0, the task succeeded; if retCode < 0, the task failed |
descMsg | NSString * | Task description message |
accessURL | NSString * | The CDN url of the file returned by the backend when the task succeeded |
sourceURL | NSString * | The origin server URL of the file returned by the backend when the task succeeded |
COSObjectPutTask *task = [COSObjectPutTask new];
task.filePath = path;
task.fileName = fileName;
task.bucket = bucket;
task.attrs = @"customAttribute";
task.directory = dir;
task.insertOnly = YES;
task.sign = _sign;
COSClient *client= [[COSClient alloc] initWithAppId:appId withRegion:[Congfig instance].region];
client.completionHandler = ^(COSTaskRsp *resp, NSDictionary *context){
if (resp.retCode == 0) {
//sucess
}else{}
};
client.progressHandler = ^(NSInteger bytesWritten,NSInteger totalBytesWritten,NSInteger totalBytesExpectedToWrite){
//progress
};
[client putObject:task];
You can update the custom attributes of the file by calling this API. The steps are as follows:
Parameter Name | Type | Required | Description |
---|---|---|---|
fileName | NSString * | Yes | |
bucket | NSString * | Yes | Name of bucket to which a directory belongs |
sign | NSString * | Yes | Signature |
attrs | NSString * | No | Custom attributes |
The result is returned through TXYUpdateCommandRsp object.
Attribute Name | Type | Description |
---|---|---|
retCode | int | Task description code. If retCode >= 0, the task succeeded; if retCode < 0, the task failed |
descMsg | NSString * | Task description message |
COSObjectUpdateCommand *cm = [COSObjectUpdateCommand new]
cm.fileName = file;
cm.bucket = bucket;
cm.sign = _oneSign;//One-time signature
COSClient *client= [[COSClient alloc] initWithAppId:appId withRegion:[Congfig instance].region];
client.completionHandler = ^(COSTaskRsp *resp, NSDictionary *context){
if (resp.retCode == 0) {
//sucess
}else{
};
[client updateObject:cm];
You can query the file attributes by calling this API. The steps are as follows:
Parameter Name | Type | Required | Description |
---|---|---|---|
filename | NSString * | Yes | |
bucket | NSString * | Yes | Name of bucket to which a file belongs |
directory | NSString * | Yes | Directory path (relative to bucket path) |
sign | NSString * | Yes | Signature |
The result is returned through TXYStatCommandRsp object.
Attribute Name | Type | Description |
---|---|---|
retCode | int | Task description code. If retCode >= 0, the task succeeded; if retCode < 0, the task failed |
descMsg | NSString * | Task description message |
data | NSDictionary * | File basic information when the task succeeded |
COSObjectMetaCommand *cm = [COSObjectMetaCommand new] ;
cm.fileName = file;
cm.bucket = bucket;
cm.directory = dir;
cm.sign = _oneSign;//One-time signature
COSClient *client= [[COSClient alloc] initWithAppId:appId withRegion:[Congfig instance].region];
client.completionHandler = ^(COSTaskRsp *resp, NSDictionary *context){
if (resp.retCode == 0) {
//sucess
}else{}
};
[client getObjectMetaData:cm];
You can delete files by calling this API. The steps are as follows:
Parameter Name | Type | Required | Description |
---|---|---|---|
filename | NSString * | Yes | |
bucket | NSString * | Yes | Name of Bucket to which a file belongs |
directory | NSString * | Yes | Directory path (relative to bucket path) |
sign | NSString * | Yes | Signature |
objectType | TXYObjectType | Yes | Content type. Set as TXYObjectFile when deleting files |
Returned result via COSObjectDeleteTaskRsp
Attribute Name | Type | Description |
---|---|---|
retCode | int | Task description code. If retCode >= 0, the task succeeded; if retCode < 0, the task failed |
descMsg | NSString * | Task description message |
COSObjectDeleteCommand *cm = [COSObjectDeleteCommand new];
cm.fileName = file;
cm.bucket = bucket;
cm.directory = dir;
cm.sign = _oneSign;//One-time signature
COSClient *client= [[COSClient alloc] initWithAppId:appId withRegion:[Congfig instance].region];;
client.completionHandler = ^(COSTaskRsp *resp, NSDictionary *context){
if (resp.retCode == 0) {
//sucess
}else{ }
};
[client deleteObject:cm];
You can download files by calling this API. The steps are as follows:
Parameter Name | Type | Required | Description |
---|---|---|---|
filePath | NSString * | Yes | File download path |
Returned result via COSGetObjectTaskRsp object
Attribute Name | Type | Description |
---|---|---|
retCode | int | Task description code. If retCode >= 0, the task succeeded; if retCode < 0, the task failed |
descMsg | NSString * | Task description message |
object | NSMutableData * | The download file |
COSObjectGetTask *cm = [[COSObjectGetTask alloc] initWithUrl:imgUrl.text];
COSClient *client= [[COSClient alloc] initWithAppId:appId withRegion:[Congfig instance].region];
client.completionHandler = ^(COSTaskRsp *resp, NSDictionary *context){
//
};
client.downloadProgressHandler = ^(int64_t receiveLength,int64_t contentLength){
};
[client getObject:cm];
You can download files by calling this API. The steps are as follows:
Parameter Name | Type | Required | Description |
---|---|---|---|
filePath | NSString * | Yes | File path |
multipartUpload | BOOL | No | Indicate whether to use multipart upload for file upload |
sign | NSString * | Yes | Signature |
bucket | NSString * | Yes | Target Bucket name |
fileName | NSString * | Target Bucket name | The name shown after uploading cos for target files |
attrs | NSString * | No | Custom file attributes |
directory | NSString * | Yes | File upload directory, relative path, for example "/path" |
insertOnly | BOOL | Yes | Upload will insert and overwrite the existing files. But if you set "YES", it will not overwrite the previous upload file |
The result is returned through COSObjectUploadTaskRsp object.
Attribute Name | Type | Description |
---|---|---|
retCode | int | Task description code. If retCode >= 0, the task succeeded; if retCode < 0, the task failed |
descMsg | NSString * | Task description message |
COSObjectPutTask *task = [[COSObjectPutTask alloc] init];
task.multipartUpload = YES;//Multipart upload settings parameters
task.filePath = path;
task.fileName = fileName;
task.bucket = bucket;
task.attrs = @"customAttribute";
task.directory = dir;
task.insertOnly = YES;
task.sign = _sign;
COSClient *client= [[COSClient alloc] initWithAppId:appId withRegion:[Congfig instance].region]; client.completionHandler = ^(COSTaskRsp *resp, NSDictionary *context){
if (rsp.retCode == 0) {
//sucess
}else{ }
};
client.progressHandler = ^(NSInteger bytesWritten,NSInteger totalBytesWritten,NSInteger totalBytesExpectedToWrite){
//Progress
};
[client putObject:task];
Was this page helpful?