Skip to content

Commit

Permalink
Fixed an exception thrown by resumableupload when the number of parts…
Browse files Browse the repository at this point in the history
… exceeds 5000
  • Loading branch information
wushuai1415 authored and huiguangjun committed Dec 30, 2021
1 parent f0a10c7 commit e3a196e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
11 changes: 9 additions & 2 deletions AliyunOSSSDK/OSSClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
static NSString * const kClientRecordNameWithCRC64Suffix = @"-crc64";
static NSString * const kClientRecordNameWithSequentialSuffix = @"-sequential";
static NSUInteger const kClientMaximumOfChunks = 5000; //max part number
static NSUInteger const kPartSizeAlign = 4 * 1024; // part size byte alignment

static NSString * const kClientErrorMessageForEmptyFile = @"the length of file should not be 0!";
static NSString * const kClientErrorMessageForCancelledTask = @"This task has been cancelled!";
Expand Down Expand Up @@ -277,13 +278,19 @@ - (NSUInteger)judgePartSizeForMultipartRequest:(OSSMultipartUploadRequest *)requ

if(partCount > kClientMaximumOfChunks)
{
request.partSize = fileSize / kClientMaximumOfChunks;
partCount = kClientMaximumOfChunks;
NSUInteger partSize = fileSize / (kClientMaximumOfChunks - 1);
request.partSize = [self ceilPartSize:partSize];
partCount = (fileSize / request.partSize) + ((fileSize % request.partSize == 0) ? 0 : 1);
}
return partCount;
#pragma clang diagnostic pop
}

- (NSUInteger)ceilPartSize:(NSUInteger)partSize {
partSize = (((partSize + (kPartSizeAlign - 1)) / kPartSizeAlign) * kPartSizeAlign);
return partSize;
}

- (unsigned long long)getSizeWithFilePath:(nonnull NSString *)filePath error:(NSError **)error
{
NSFileManager *fm = [NSFileManager defaultManager];
Expand Down
86 changes: 86 additions & 0 deletions AliyunOSSiOSTests/OSSObjectTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
#define BUCKET_NAME @"BucketName"
#define OBJECT_KEY @"ObjectKey"


@interface OSSClient(Test)

- (NSUInteger)judgePartSizeForMultipartRequest:(OSSMultipartUploadRequest *)request fileSize:(unsigned long long)fileSize;
- (NSUInteger)ceilPartSize:(NSUInteger)partSize;

@end

@interface OSSObjectTests : XCTestCase
{
OSSClient *_client;
Expand Down Expand Up @@ -1966,4 +1974,82 @@ - (void)testAPI_putObjectWithEmptyFile {
XCTAssertNotNil(task.error);
}

- (void)testAPI_judgePartSize {
NSInteger partSize = 100 * 1024;
NSInteger fileSize = partSize * 5001;
OSSMultipartUploadRequest *multipartUploadRequest = [OSSMultipartUploadRequest new];
multipartUploadRequest.partSize = partSize;

NSInteger partCount = [_client judgePartSizeForMultipartRequest:multipartUploadRequest fileSize:fileSize];
NSInteger expectPartCount = fileSize / multipartUploadRequest.partSize;
expectPartCount += fileSize % multipartUploadRequest.partSize > 0 ? 1 : 0;
XCTAssertEqual(partCount, expectPartCount);
XCTAssertEqual(0, multipartUploadRequest.partSize % (4 * 1024));

fileSize = partSize * 5000;
multipartUploadRequest.partSize = partSize;
partCount = [_client judgePartSizeForMultipartRequest:multipartUploadRequest fileSize:fileSize];
expectPartCount = fileSize / multipartUploadRequest.partSize;
expectPartCount += fileSize % multipartUploadRequest.partSize > 0 ? 1 : 0;
XCTAssertEqual(partCount, expectPartCount);
XCTAssertEqual(5000, expectPartCount);

fileSize = partSize * 4999;
multipartUploadRequest.partSize = partSize;
partCount = [_client judgePartSizeForMultipartRequest:multipartUploadRequest fileSize:fileSize];
expectPartCount = fileSize / multipartUploadRequest.partSize;
expectPartCount += fileSize % multipartUploadRequest.partSize > 0 ? 1 : 0;
XCTAssertEqual(partCount, expectPartCount);
XCTAssertEqual(4999, expectPartCount);

fileSize = partSize * 1 + 1;
multipartUploadRequest.partSize = partSize;
partCount = [_client judgePartSizeForMultipartRequest:multipartUploadRequest fileSize:fileSize];
expectPartCount = fileSize / multipartUploadRequest.partSize;
expectPartCount += fileSize % multipartUploadRequest.partSize > 0 ? 1 : 0;
XCTAssertEqual(partCount, expectPartCount);
XCTAssertEqual(2, expectPartCount);

fileSize = partSize * 1;
multipartUploadRequest.partSize = partSize;
partCount = [_client judgePartSizeForMultipartRequest:multipartUploadRequest fileSize:fileSize];
expectPartCount = fileSize / multipartUploadRequest.partSize;
expectPartCount += fileSize % multipartUploadRequest.partSize > 0 ? 1 : 0;
XCTAssertEqual(partCount, expectPartCount);
XCTAssertEqual(1, expectPartCount);

fileSize = 1;
multipartUploadRequest.partSize = partSize;
partCount = [_client judgePartSizeForMultipartRequest:multipartUploadRequest fileSize:fileSize];
expectPartCount = fileSize / multipartUploadRequest.partSize;
expectPartCount += fileSize % multipartUploadRequest.partSize > 0 ? 1 : 0;
XCTAssertEqual(partCount, expectPartCount);
XCTAssertEqual(1, expectPartCount);


fileSize = 200 * 1024 * 4999;
multipartUploadRequest.partSize = partSize;
partCount = [_client judgePartSizeForMultipartRequest:multipartUploadRequest fileSize:fileSize];
expectPartCount = fileSize / multipartUploadRequest.partSize;
expectPartCount += fileSize % multipartUploadRequest.partSize > 0 ? 1 : 0;
XCTAssertEqual(partCount, expectPartCount);
XCTAssertEqual(4999, expectPartCount);
}

- (void)testAPI_ceilPartSize {

NSUInteger partSizeAlign = 4 * 1024;
NSUInteger partSize = 1;
partSize = [_client ceilPartSize:partSize];
XCTAssertEqual(partSizeAlign, partSize);

partSize = 4 * 1024;
partSize = [_client ceilPartSize:partSize];
XCTAssertEqual(partSizeAlign, partSize);

partSize = 4 * 1024 + 1;
partSize = [_client ceilPartSize:partSize];
XCTAssertEqual(partSizeAlign * 2, partSize);
}

@end

0 comments on commit e3a196e

Please sign in to comment.