Updated on 2026-09-22 GMT+08:00

Uploading an object

This example uploads a local file to bucket bucketname as object objectname.

Sample code:

NSString *filePath = [[NSBundle mainBundle]pathForResource:@"fileName" ofType:@"Type"];
OBSPutObjectWithFileRequest *request = [[OBSPutObjectWithFileRequest alloc]initWithBucketName:@"bucketname" objectKey:@"objectname" uploadFilePath:filePath];
// Query the upload progress.
request.uploadProgressBlock = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
    NSLog(@"%0.1f%%",(float)floor((totalBytesExpectedToSend > 0) ? (totalBytesSent*10000/totalBytesExpectedToSend) : 0)/100);
};
// Upload a file.
[client putObject:request completionHandler:^(OBSPutObjectResponse *response, NSError *error){
    if (response.etag) {
        NSLog(@"%@",response.etag);
    }
}];

In the current SDK, the uploadFilePath parameter of the file upload APIs (OBSPutObjectWithFileRequest and OBSUploadFileRequest) only accepts file paths of the NSString type. Directly passing an NSURL object created with [NSURL fileURLWithPath:@"xxx"] is not supported. To use NSURL, obtain the file path string through [fileURL path] and then pass it to the uploadFilePath parameter.