Get Object
Simple object get
You can load the object into memory using the provided code.
Basic workflow
- Instantiate the BOSClient class.
- Executing the BOSClient getObject method will return a BOSGetObjectResponse instance.
- Access the
objectContent.objectData.dataproperty of theBOSGetObjectResponseinstance to retrieve the data.
Example code
1__block BOSGetObjectResponse* getObjResponse = nil;
2BOSGetObjectRequest* getObjRequest = [[BOSGetObjectRequest alloc] init];
3getObjRequest.bucket = @"<bucketname>";
4getObjRequest.key = @"<objectname>";
5BCETask* task = [client getObject:getObjRequest];
6task.then(^(BCEOutput* output) {
7 if (output.response) {
8 getObjResponse = (BOSGetObjectResponse*)output.response;
9 NSLog(@"get object success!");
10 }
11 if (output.error) {
12 NSLog(@"get object failure with %@", outpu****t.error);
13 }
14 if (output.progress) {
15 NSLog(@"the get object progress is %@", output.progress);
16 }
17});
18[task waitUtilFinished];
19// Retrieve data from memory
20NSData* data = getObjResponse.objectContent.objectData.data;
Note:
BOSObjectContentcontains detailed information about the object, including its bucket location, object name, metadata, and data storage.BOSObjectMetadataincludes the ETag set during the object upload, HTTP headers, and custom metadata.- Retrieve the object's data through the
objectDataproperty ofBOSObjectContent.
Complete example
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 __block BOSGetObjectResponse* getObjResponse = nil;
12 BOSGetObjectRequest* getObjRequest = [[BOSGetObjectRequest alloc] init];
13 getObjRequest.bucket = @"<bucketname>";
14 getObjRequest.key = @"<objectname>";
15 BCETask* task = [client getObject:getObjRequest];
16 task.then(^(BCEOutput* output) {
17 if (output.response) {
18 getObjResponse = (BOSGetObjectResponse*)output.response;
19 NSLog(@"get object success!");
20 }
21 if (output.error) {
22 NSLog(@"get object failure with %@", output.error);
23 }
24 if (output.progress) {
25 NSLog(@"the get object progress is %@", output.progress);
26 }
27 });
28 [task waitUtilFinished];
29 NSData* data = getObjResponse.objectContent.objectData.data;
30}
Download part of the content of an object
Basic workflow
- Instantiate the
BOSGetObjectRequestclass. - Specify the
rangeStartand/orrangeEndfields in theBOSGetObjectRequestinstance. - Perform the
client.getObjectoperation.
Example code
1__block BOSGetObjectResponse* getObjResponse = nil;
2BOSGetObjectRequest* getObjRequest = [[BOSGetObjectRequest alloc] init];
3getObjRequest.bucket = @"<bucketname>";
4getObjRequest.key = @"<objectname>";
5// Retrieve the first 100 bytes
6getObjRequest.rangeStart = @"0";
7getObjRequest.rangeEnd = @"99";
8BCETask* task = [client getObject:getObjRequest];
9task.then(^(BCEOutput* output) {
10 if (output.response) {
11 getObjResponse = (BOSGetObjectResponse*)output.response;
12 NSLog(@"get object success!");
13 }
14 if (output.error) {
15 NSLog(@"get object failure with %@", output.error);
16 }
17 if (output.progress) {
18 NSLog(@"the get object progress is %@", output.progress);
19 }
20});
21[task waitUtilFinished];
22NSData* data = getObjResponse.objectContent.objectData.data;
Note: Users can leverage this function for segmented downloads and resumable file uploads.
Download object to a specified path
Users can use the following code to directly download an object to a specified path.
Basic workflow
- Instantiate the
BOSGetObjectRequestclass. - Specify the filename to be saved in the
filefield of theBOSGetObjectRequestinstance. - Perform the
client.getObjectoperation. - Download an object directly to a specified path.
Example code
1__block BOSGetObjectResponse* getObjResponse = nil;
2BOSGetObjectRequest* getObjRequest = [[BOSGetObjectRequest alloc] init];
3getObjRequest.bucket = @"<bucketname>";
4getObjRequest.key = @"<objectname>";
5// Set the file path for saving
6getObjRequest.file = @"<file>";
7BCETask* task = [client getObject:getObjRequest];
8task.then(^(BCEOutput* output) {
9 if (output.response) {
10 getObjResponse = (BOSGetObjectResponse*)output.response;
11 NSLog(@"get object success!");
12 }
13 if (output.error) {
14 NSLog(@"get object failure with %@", output.error);
15 }
16 if (output.progress) {
17 NSLog(@"the get object progress is %@", output.progress);
18 }
19});
20[task waitUtilFinished];
Obtain the storageClass of an object
The storage class of an object is categorized into STANDARD (standard storage), STANDARD_IA (infrequent access storage), and COLD (cold storage).
Example code:
1__block BOSGetObjectMetadataResponse* getObjMetaResponse = nil;
2BCETask* task = [client getObjectMetadata:@"<bucketname>" objectKey:@"<objectname>"];
3task.then(^(BCEOutput* output) {
4 if (output.response) {
5 getObjMetaResponse = (BOSGetObjectMetadataResponse*)output.response;
6 NSString storageClass = getObjMetaResponse.storageClass;
7 NSLog(@"get object storageClass success!");
8 }
9 if (output.error) {
10 NSLog(@"get object storageClass failure");
11 }
12});
13[task waitUtilFinished];
Obtain only ObjectMetadata
Use the getObjectMetadata method to retrieve only the object's metadata without including its content.
Example code:
1__block BOSGetObjectMetadataResponse* getObjMetaResponse = nil;
2BCETask* task = [client getObjectMetadata:@"<bucketname>" objectKey:@"<objectname>"];
3task.then(^(BCEOutput* output) {
4 if (output.response) {
5 getObjMetaResponse = (BOSGetObjectMetadataResponse*)output.response;
6 NSLog(@"get object metadata success!");
7 }
8 if (output.error) {
9 NSLog(@"get object metadata failure");
10 }
11});
12[task waitUtilFinished];
Get object URL
The following code allows you to get the URL of a specific object, which is commonly used for temporarily sharing the object's URL with others.
Basic workflow
- Instantiate the BOSClient class.
- Execute the
BOSClient.generatePresignedUrlmethod. - The method returns the URL of the object.
Example code
1__block BOSGeneratePresignedUrlResponse *generateObjetUrlRes = nil;
2 BCEOutput_ output = [client generatePresignedUrl:@"<bucketname>" objectKey:@"<objectname>" expirationInSeconds:<ExpirationInSeconds>];
3 if (output.response) {
4 generateObjetUrlRes = (BOSGeneratePresignedUrlResponse_)output.response;
5 NSLog(@"get url success, the usrlstting is : %@", [generateObjetUrlRes.objectUrl absoluteString]);
6 }
7 if (output.error) {
8 NSLog(@"get url failure, error : %@:", output.error);
9 }
Note: ExpirationInSeconds is the specified URL validity period, calculated from the current time. It is an optional parameter, and the system default value is 1,800 seconds if not configured. To set a permanent non-expiration time, the ExpirationInSeconds parameter can be set to -1. You cannot set it to any other negative value.
Complete example
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 __block BOSGeneratePresignedUrlResponse *generateObjetUrlRes = nil;
12 BCEOutput* output = [client generatePresignedUrl:@"<bucketname>" objectKey:@"<objectname>" expirationInSeconds:<ExpirationInSeconds>];
13 if (output.response) {
14 generateObjetUrlRes = (BOSGeneratePresignedUrlResponse*)output.response;
15 NSLog(@"get url success, the usrlstting is : %@", [generateObjetUrlRes.objectUrl absoluteString]);
16 }
17 if (output.error) {
18 NSLog(@"get url failure, error : %@:", output.error);
19 }
20}
