如果仅需要文件中的部分数据,您可以使用范围下载,下载指定范围内的数据。
范围下载适用于下载较大的 Object 。如果在请求头中使用 Range 参数,则返回消息中会包含整个文件的长度和此次返回的范围。
以下代码用于范围下载:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | OSSGetObjectRequest * request = [OSSGetObjectRequest new ]; request.bucketName = @"<bucketName>; request.objectKey = @ "<objectKey>" ; request.range = [[OSSRange alloc] initWithStart: 1 withEnd: 99 ]; // bytes=1-99 // request.range = [[OSSRange alloc] initWithStart:-1 withEnd:99]; // bytes=-99 // request.range = [[OSSRange alloc] initWithStart:10 withEnd:-1]; // bytes=10- request.downloadProgress = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) { NSLog(@ "%lld, %lld, %lld" , bytesWritten, totalBytesWritten, totalBytesExpectedToWrite); }; OSSTask * getTask = [client getObject:request]; [getTask continueWithBlock:^id(OSSTask *task) { if (!task.error) { NSLog(@ "download object success!" ); OSSGetObjectResult * getResult = task.result; NSLog(@ "download result: %@" , getResult.dowloadedData); } else { NSLog(@ "download object failed, error: %@" ,task.error); } return nil; }]; // [getTask waitUntilFinished]; // [request cancel]; |