We’re Still Here to Help (Even Over the Holidays!) - find out more here.
Forum Discussion
C. Logan
9 years agoExplorer | Level 3
Uploading big file using Objective-C API V2 very SLOW...
Hello, recently our app updates the Dropbox related code from API V1 to API V2. Usually we need to transfer files to Dropbox, it is OK for small files, but for video files of size about 1.5 GB th...
Greg-DB
Dropbox Community Moderator
9 years agoThanks! Your definition DB_CHUNK_SIZE of seems like it should be fine, but I don't see exactly where it's actually being used. Can you share the definitions of TGDropboxObject, readDataOfLength, getOffsetFromObj, closeFileFromObj, and anything else necessary so I can try to run this? Thanks in advance!
C. Logan
9 years agoExplorer | Level 3
You cannot run the code without our external device. But the following code does the same thing and produces the the same result.
You need to provide the source file to be uploaded. The destination file is hard coded (/temp/test.mp4), you can modify it to another file path. The unused parameters in the API uploadInternal:files:index:fromDir:toDropboxDir:opType:resArray: can be ignored.
If there is anything unclear, feel free to point out.
const NSUInteger DB_BLOCK_SIZE = 5*1024*1024; // 102400000;
@interface DestVC ()
@property DBUploadTask<DBFILESUploadSessionStartResult *, DBNilObject *> * uploadTask;
@property DBUploadTask<DBNilObject *, DBFILESUploadSessionLookupError *> * continueTask;
@property DBUploadTask<DBFILESFileMetadata *, DBFILESUploadSessionFinishError *> *finishTask;
@property unsigned long long offset;
@property NSFileHandle *file;
@property NSData *data;
@property NSNumber *fileSize;
@property DropboxClient *client;
@property NSString *sessionId;
@end
@implementation DestVC
- (void)uploadInternal:(NSArray *)items files:(NSArray *)files index:(int)index fromDir:(NSString *)fromDir toDropboxDir:(NSString *)toDir opType:(OperationType)opType resArray:(NSMutableArray *)resArray {
NSString *file = [files objectAtIndex:index];
NSString *srcPath = [fromDir stringByAppendingPathComponent:file];// define your own source file to be uploaded
// NSString *dstPath = [toDir stringByAppendingPathComponent:file];// define your own destination file in Dropbox
_client = [DropboxClientsManager authorizedClient];
if (!_client) {
[DropboxClientsManager authorizeFromController:[UIApplication sharedApplication]
controller:self
openURL:^(NSURL *url) {
[[UIApplication sharedApplication] openURL:url];
}
browserAuth:YES];
} else {
_fileSize = [FCFileManager sizeOfFileAtPath:srcPath];// get file size
_file = [NSFileHandle fileHandleForReadingAtPath:srcPath];
_data = [_file readDataOfLength:DB_BLOCK_SIZE];
_uploadTask = [_client.filesRoutes uploadSessionStartData:_data];
[_uploadTask response:^(DBFILESUploadSessionStartResult * _Nullable result, DBNilObject * _Nullable obj, DBRequestError * _Nullable error) {
DLog(@"result: %@", result);
DLog(@"routeError: %@", obj);
DLog(@"error: %@", error);
if (error == nil) {
_sessionId = result.sessionId;
DBFILESUploadSessionCursor *cursor = [[DBFILESUploadSessionCursor alloc] initWithSessionId:result.sessionId offset:[NSNumber numberWithUnsignedLongLong:_file.offsetInFile]];
[self uploadNext:cursor];
} else {
[_file closeFile];
}
}];
[_uploadTask progress:^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
NSLog(@"Start: bytesWritten = %lld", bytesWritten);
}];
}
return;
}
- (void)uploadNext:(DBFILESUploadSessionCursor *)cursor {
if ([_fileSize unsignedLongLongValue] - _file.offsetInFile > DB_BLOCK_SIZE) {
_data = [_file readDataOfLength:DB_BLOCK_SIZE];
} else {
unsigned long long rest = [_fileSize unsignedLongLongValue] - _file.offsetInFile;
_data = [_file readDataOfLength:rest];
}
if (_data.length < DB_BLOCK_SIZE) {
DBFILESCommitInfo *info = [[DBFILESCommitInfo alloc] initWithPath:@"/temp/test.mp4"];// define your destination file
_finishTask = [_client.filesRoutes uploadSessionFinishData:cursor commit:info inputData:_data];
[_finishTask response:^(DBFILESFileMetadata * _Nullable meta, DBFILESUploadSessionFinishError * _Nullable finishError, DBRequestError * _Nullable error) {
DLog(@"meta: %@", meta);
DLog(@"finishError: %@", finishError);
DLog(@"error: %@", error);
}];
return;
}
_continueTask = [_client.filesRoutes uploadSessionAppendV2Data:cursor inputData:_data];
[_continueTask response:^(DBNilObject * _Nullable obj, DBFILESUploadSessionLookupError * _Nullable lookupError, DBRequestError * _Nullable error) {
DLog(@"obj: %@", obj);
DLog(@"lookupError: %@", lookupError);
DLog(@"error: %@", error);
if (error == nil) {
DLog(@"_file.offsetInFile: %lld", _file.offsetInFile);
DBFILESUploadSessionCursor *cursor = [[DBFILESUploadSessionCursor alloc] initWithSessionId:_sessionId offset:[NSNumber numberWithUnsignedLongLong:_file.offsetInFile]];
[self uploadNext:cursor];
} else {
[_file closeFile];
}
}];
[_continueTask progress:^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
NSLog(@"Continue: bytesWritten = %lld", bytesWritten);
}];
}
@end
- C. Logan9 years agoExplorer | Level 3
Remark: File size can also be obtained through following code instead of using FCFileManager:
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:NULL];
fileSize = [attributes fileSize];
- Greg-DB9 years ago
Dropbox Community Moderator
Thanks! That's helpful
A few notes:
- I misunderstood what was being logged in your original post, and testing the code I can confirm the issue isn't the chunk size being 32768. That's the progress callback is showing incremental progress within each single chunk upload call. Your chunk size is working at 5 MB.
- In your latest code though, note that the comment is wrong. The actual code sets a 5 MB chunk size, but the comment indicates 100 MB.
- Testing this code, I have a better idea of where the overhead is. We're looking into it and I'll follow up here once I have more information.
- C. Logan9 years agoExplorer | Level 3
Thanks for the reply!
The comment is just for testing different value of chunk size. I have tested 5 MB and 100 MB, but the result is the same.
If you have tested the upload using the code I provided, did you get the same result? I mean, is the upload blocked/stopped or canceled after a few seconds?
It would be great if you could help us out from the problem described above.
Hope to hear from you soon when you have more information about how to correctly use the API V2 and get rid of the problem.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!