Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zuoqin committed Nov 17, 2017
2 parents 9e56473 + b303eb3 commit a006e05
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 21 deletions.
6 changes: 6 additions & 0 deletions AliyunOSSiOS/OSSClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ - (OSSTask *)putObject:(OSSPutObjectRequest *)request {
if (request.uploadProgress) {
requestDelegate.uploadProgress = request.uploadProgress;
}
if (request.uploadRetryCallback) {
requestDelegate.retryCallback = request.uploadRetryCallback;
}
if (request.contentDisposition) {
[headerParams setObject:request.contentDisposition forKey:OSSHttpHeaderContentDisposition];
}
Expand Down Expand Up @@ -305,6 +308,9 @@ - (OSSTask *)putObject:(OSSPutObjectRequest *)request {

- (OSSTask *)putObjectACL:(OSSPutObjectACLRequest *)request {
OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
if (request.uploadRetryCallback) {
requestDelegate.retryCallback = request.uploadRetryCallback;
}
NSMutableDictionary * headerParams = [NSMutableDictionary dictionary];
if (request.acl) {
headerParams[@"x-oss-object-acl"] = request.acl;
Expand Down
39 changes: 19 additions & 20 deletions AliyunOSSiOS/OSSModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ typedef NS_ENUM(NSInteger, OSSClientErrorCODE) {

typedef void (^OSSNetworkingUploadProgressBlock) (int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend);
typedef void (^OSSNetworkingDownloadProgressBlock) (int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite);
typedef void (^OSSNetworkingRetryBlock) (void);
typedef void (^OSSNetworkingCompletionHandlerBlock) (id responseObject, NSError *error);
typedef void (^OSSNetworkingOnRecieveDataBlock) (NSData * data);

Expand Down Expand Up @@ -648,26 +649,6 @@ It's a unique Id represents this request. This is used for troubleshooting when
@property (nonatomic, strong) NSDictionary * objectMeta;
@end

/**
The request class to update the object ACL.
*/
@interface OSSPutObjectACLRequest : OSSRequest

/**
Bucket name
*/
@property (nonatomic, strong) NSString * bucketName;

/**
Object name
*/
@property (nonatomic, strong) NSString * objectKey;

/**
*/
@property (nonatomic, strong) NSString * acl;

@end

/**
The response class to update the object ACL.
Expand Down Expand Up @@ -756,6 +737,24 @@ It's a unique Id represents this request. This is used for troubleshooting when
It runs in background thread (not UI thread).
*/
@property (nonatomic, copy) OSSNetworkingUploadProgressBlock uploadProgress;

/**
The upload retry callback.
It runs in background thread (not UI thread).
*/
@property (nonatomic, copy) OSSNetworkingRetryBlock uploadRetryCallback;

@end

/**
The request class to update the object ACL.
*/
@interface OSSPutObjectACLRequest : OSSPutObjectRequest

/**
*/
@property (nonatomic, strong) NSString * acl;

@end

/**
Expand Down
1 change: 1 addition & 0 deletions AliyunOSSiOS/OSSNetworking.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ typedef NS_ENUM(NSInteger, OSSNetworkingRetryType) {

@property (nonatomic, copy) OSSNetworkingUploadProgressBlock uploadProgress;
@property (nonatomic, copy) OSSNetworkingDownloadProgressBlock downloadProgress;
@property (nonatomic, copy) OSSNetworkingRetryBlock retryCallback;
@property (nonatomic, copy) OSSNetworkingCompletionHandlerBlock completionHandler;
@property (nonatomic, copy) OSSNetworkingOnRecieveDataBlock onRecieveData;

Expand Down
8 changes: 8 additions & 0 deletions AliyunOSSiOS/OSSNetworking.m
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)sessionTask
return task;
}] continueWithBlock:^id(OSSTask *task) {
if (task.error) {


OSSNetworkingRetryType retryType = [delegate.retryHandler shouldRetry:delegate.currentRetryCount
requestDelegate:delegate
response:httpResponse
Expand Down Expand Up @@ -531,9 +533,15 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)sessionTask
NSTimeInterval suspendTime = [delegate.retryHandler timeIntervalForRetry:delegate.currentRetryCount retryType:retryType];
delegate.currentRetryCount++;
[NSThread sleepForTimeInterval:suspendTime];

if(delegate.retryCallback){
delegate.retryCallback();
}


/* retry recursively */
[delegate reset];

[self dataTaskWithDelegate:delegate];
} else {
delegate.completionHandler([delegate.responseParser constructResultObject], nil);
Expand Down
45 changes: 44 additions & 1 deletion AliyunOSSiOSTests/AliyunOSSiOSTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ @interface oss_ios_sdk_newTests : XCTestCase

@end

NSString * const TEST_BUCKET = @"sdk-demo001";
NSString * const TEST_BUCKET = @"testbucket";

NSString * const PUBLIC_BUCKET = @"public-read-write-android1";
NSString * const ENDPOINT = @"https://oss-cn-qingdao.aliyuncs.com";
Expand Down Expand Up @@ -421,6 +421,43 @@ - (void)testA_putObjectWithContentType {
XCTAssertTrue(isEqual);
}

- (void)testA_PutObjectWithErrorRetry {
OSSPutObjectRequest * request = [OSSPutObjectRequest new];
request.bucketName = TEST_BUCKET;
request.objectKey = @"file1m";

[NSDate oss_setClockSkew: 30 * 60];

request.uploadRetryCallback = ^{
NSLog(@"put object call retry");
};

NSString * docDir = [self getDocumentDirectory];
NSURL * fileURL = [NSURL fileURLWithPath:[docDir stringByAppendingPathComponent:@"file1m"]];
NSFileHandle * readFile = [NSFileHandle fileHandleForReadingFromURL:fileURL error:nil];

request.uploadingData = [readFile readDataToEndOfFile];
request.contentMd5 = [OSSUtil base64Md5ForData:request.uploadingData];
request.objectMeta = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"value1", @"x-oss-meta-name1", nil];
request.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
};

OSSTask * task = [client putObject:request];
[[task continueWithBlock:^id(OSSTask *task) {
XCTAssertNil(task.error);
if (task.error) {
OSSLogError(@"%@", task.error);
}
OSSPutObjectResult * result = task.result;
XCTAssertEqual(200, result.httpResponseCode);
NSLog(@"Result - requestId: %@, headerFields: %@",
result.requestId,
result.httpResponseHeaderFields);
return nil;
}] waitUntilFinished];
}

- (void)testA_putObjectFromNSData {
OSSPutObjectRequest * request = [OSSPutObjectRequest new];
request.bucketName = TEST_BUCKET;
Expand All @@ -434,6 +471,7 @@ - (void)testA_putObjectFromNSData {
request.objectMeta = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"value1", @"x-oss-meta-name1", nil];
request.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);

};

OSSTask * task = [client putObject:request];
Expand Down Expand Up @@ -1217,10 +1255,14 @@ - (void)testTimeSkewedButAutoRetry {
}] waitUntilFinished];
}




- (void)testPutObjectWithCheckingDataMd5 {
OSSPutObjectRequest * request = [OSSPutObjectRequest new];
request.bucketName = TEST_BUCKET;
request.objectKey = @"file1m";


NSString * docDir = [self getDocumentDirectory];
NSURL * fileURL = [NSURL fileURLWithPath:[docDir stringByAppendingPathComponent:@"file1m"]];
Expand Down Expand Up @@ -1263,6 +1305,7 @@ - (void)testPutObjectWithCheckingFileMd5 {
request.objectMeta = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"value1", @"x-oss-meta-name1", nil];
request.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);

};

OSSTask * task = [client putObject:request];
Expand Down

0 comments on commit a006e05

Please sign in to comment.