Upload Object
Updated at:2025-11-03
Simplest upload
Basic workflow
- Instantiate the BOSClient class.
- Use the BOSClient putObject method to upload objects either as files or binary strings.
- For the returned instance of type BOSPutObjectResponse, you can retrieve the eTag.
Example code
Swift
1BOSObjectContent* content = [[BOSObjectContent alloc] init];
2// In file mode
3content.objectData.file = @"<file path>";
4// Or in binary data mode
5NSData* data = [[NSData alloc] init];
6content.objectData.data = data;
7BOSPutObjectRequest* request = [[BOSPutObjectRequest alloc] init];
8request.bucket = @"<bucketname>";
9request.key = @"<objectname>";
10request.objectContent = content;
11__block BOSPutObjectResponse* response = nil;
12BCETask* task = [client putObject:request];
13task.then(^(BCEOutput* output) {
14 if (output.progress) {
15 NSLog(@"put object progress is %@", output.progress);
16 }
17 if (output.response) {
18 response = (BOSPutObjectResponse*)output.response;
19 NSLog(@"put object success!");
20 }
21 if (output.error) {
22 NSLog(@"put object failure");
23 }
24});
25[task waitUtilFinished];
Note: Objects are uploaded to BOS as files, and the putObject function supports uploading objects with a size not exceeding 5GB. After the putObject request is processed successfully, BOS will return the ETag of the object in the Header as the file identifier.
Complete example
Swift
1#import <BaiduBCEBasic/BaiduBCEBasic.h>
2#import <BaiduBCEBOS/BaiduBCEBOS.h>
3void example(void) {
4// Initialize
5 BCECredentials* credentials = [[BCECredentials alloc] init];
6 credentials.accessKey = @"<access key>";
7 credentials.secretKey = @"<secret key>";
8 BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init];
9 configuration.credentials = credentials;
10 BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration];
11 BOSObjectContent* content = [[BOSObjectContent alloc] init];
12// In file mode
13 content.objectData.file = @"<file path>";
14// Or in binary data mode
15 NSData* data = [[NSData alloc] init];
16 content.objectData.data = data;
17 BOSPutObjectRequest* request = [[BOSPutObjectRequest alloc] init];
18 request.bucket = @"<bucketname>";
19 request.key = @"<objectname>";
20 request.objectContent = content;
21 __block BOSPutObjectResponse* response = nil;
22 BCETask* task = [client putObject:request];
23 task.then(^(BCEOutput* output) {
24 if (output.progress) {
25 NSLog(@"put object progress is %@", output.progress);
26 }
27 if (output.response) {
28 response = (BOSPutObjectResponse*)output.response;
29 NSLog(@"put object success!");
30 }
31 if (output.error) {
32 NSLog(@"put object failure");
33 }
34 });
35 [task waitUtilFinished];
36}
Set object's HTTP Header
BOS allows setting Http Headers when uploading an object.
Basic workflow
- Create a BOSObjectMetadata class instance.
- Set fields like contentEncoding, contentType, and contentDisposition in the BOSObjectMetadata instance.
- Assign the BOSObjectMetadata instance to the objectContent.metadata field of the BOSPutObjectRequest.
Example code
Swift
1BOSObjectMetadata* metadata = [[BOSObjectMetadata alloc] init];
2metadata.contentEncoding = @"<encoding>";
3metadata.contentDisposition = @"<content disposition>";
4content.metadata = metadata;
Complete example
Swift
1#import <BaiduBCEBasic/BaiduBCEBasic.h>
2#import <BaiduBCEBOS/BaiduBCEBOS.h>
3void example(void) {
4// Initialize
5 BCECredentials* credentials = [[BCECredentials alloc] init];
6 credentials.accessKey = @"<access key>";
7 credentials.secretKey = @"<secret key>";
8 BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init];
9 configuration.credentials = credentials;
10 BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration];
11 BOSObjectContent* content = [[BOSObjectContent alloc] init];
12// In file mode
13 content.objectData.file = @"<file path>";
14// Or in binary data mode
15 NSData* data = [[NSData alloc] init];
16 content.objectData.data = data;
17 BOSObjectMetadata* metadata = [[BOSObjectMetadata alloc] init];
18 metadata.contentEncoding = @"<encoding>";
19 metadata.contentDisposition = @"<content disposition>";
20 content.metadata = metadata;
21 BOSPutObjectRequest* request = [[BOSPutObjectRequest alloc] init];
22 request.bucket = @"<bucketname>";
23 request.key = @"<Objectname>";
24 request.objectContent = content;
25 __block BOSPutObjectResponse* response = nil;
26 BCETask* task = [client putObject:request];
27 task.then(^(BCEOutput* output) {
28 if (output.progress) {
29 NSLog(@"put object progress is %@", output.progress);
30 }
31 if (output.response) {
32 response = (BOSPutObjectResponse*)output.response;
33 NSLog(@"put object success!");
34 }
35 if (output.error) {
36 NSLog(@"put object failure");
37 }
38 });
39 [task waitUtilFinished];
40}
User-defined metadata
BOS supports user-defined metadata for object descriptions.
Basic workflow
- Create a BOSObjectMetadata class instance.
- Assign a custom metadata dictionary to the userMetadata field of the BOSObjectMetadata.
Example code
Swift
1BOSObjectMetadata* metadata = [[BOSObjectMetadata alloc] init];
2metadata.contentEncoding = @"<encoding>";
3metadata.contentDisposition = @"<content disposition>";
4content.metadata = metadata;
5NSDictionary* customMetadata = @{
6 @"name" : @"my-data"
7};
8content.metadata.userMetadata = customMetadata;
Note: In the provided example, a user-defined metadata property is created with the key "name" and the value "my-data." This metadata can be retrieved when the object is downloaded. Though multiple such parameters can be added, the combined size of all User Metadata must not exceed 2KB.
Complete example
Swift
1#import <BaiduBCEBasic/BaiduBCEBasic.h>
2#import <BaiduBCEBOS/BaiduBCEBOS.h>
3void example(void) {
4// Initialize
5 BCECredentials* credentials = [[BCECredentials alloc] init];
6 credentials.accessKey = @"<access key>";
7 credentials.secretKey = @"<secret key>";
8 BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init];
9 configuration.credentials = credentials;
10 BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration];
11 BOSObjectContent* content = [[BOSObjectContent alloc] init];
12// In file mode
13 content.objectData.file = @"<file path>";
14// Or in binary data mode
15 NSData* data = [[NSData alloc] init];
16 content.objectData.data = data;
17 BOSObjectMetadata* metadata = [[BOSObjectMetadata alloc] init];
18 metadata.contentEncoding = @"<encoding>";
19 metadata.contentDisposition = @"<content disposition>";
20 content.metadata = metadata;
21 NSDictionary* customMetadata = @{
22 @"name" : @"my-data"
23 };
24 content.metadata.userMetadata = customMetadata;
25 BOSPutObjectRequest* request = [[BOSPutObjectRequest alloc] init];
26 request.bucket = @"<bucketname>";
27 request.key = @"<objectname>";
28 request.objectContent = content;
29 __block BOSPutObjectResponse* response = nil;
30 BCETask* task = [client putObject:request];
31 task.then(^(BCEOutput* output) {
32 if (output.progress) {
33 NSLog(@"put object progress is %@", output.progress);
34 }
35 if (output.response) {
36 response = (BOSPutObjectResponse*)output.response;
37 NSLog(@"put object success!");
38 }
39 if (output.error) {
40 NSLog(@"put object failure");
41 }
42 });
43 [task waitUtilFinished];
44}
