Last active
December 29, 2020 16:16
-
-
Save dinowang/9cb375ec7c0c6d8d0454e4ba1c4258e9 to your computer and use it in GitHub Desktop.
Objective-C program upload file to Azure Blob Storage with progress indicator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <AZSClient/AZSClient.h> | |
NSString *const AZURE_STORAGE_CONNECTION_STRING = @"DefaultEndpointsProtocol=https;AccountName=(your account name);AccountKey=(your account key);EndpointSuffix=core.windows.net"; | |
NSString *const AZURE_STORAGE_CONTAINER = @"files"; | |
NSString *const LOCAL_FILE_FILEPATH = @"/Users/dino/A.mp4"; | |
NSString *const LOCAL_FILE_FILENAME = @"A.mp4"; | |
NSError *error; | |
AZSCloudStorageAccount *account = [AZSCloudStorageAccount accountFromConnectionString:AZURE_STORAGE_CONNECTION_STRING error:&error]; | |
AZSCloudBlobClient *client = [account getBlobClient]; | |
AZSCloudBlobContainer *container = [client containerReferenceFromName:AZURE_STORAGE_CONTAINER]; | |
[container createContainerIfNotExistsWithCompletionHandler:^(NSError *error, BOOL exists) { | |
@autoreleasepool { | |
AZSCloudBlockBlob *blob = [container blockBlobReferenceFromName:LOCAL_FILE_FILENAME]; | |
NSUInteger size = [[[NSFileManager defaultManager] attributesOfItemAtPath:LOCAL_FILE_FILEPATH error:&error] fileSize]; | |
NSInputStream *stream = [NSInputStream inputStreamWithFileAtPath:LOCAL_FILE_FILEPATH]; | |
AZSOperationContext *operationContext = [[AZSOperationContext alloc] init]; | |
operationContext.sendingRequest = ^(NSMutableURLRequest *request, AZSOperationContext *context) { | |
NSNumber *number = [stream propertyForKey:NSStreamFileCurrentOffsetKey]; | |
if (number != nil) { | |
NSUInteger pos = [number longValue]; | |
float percentage = ((float)pos / size) * 100; | |
NSLog(@"current %ld of %ld, %f%% completed", pos, size, percentage); | |
} | |
}; | |
NSLog(@"upload started."); | |
[blob uploadFromStream:stream accessCondition:nil requestOptions:nil operationContext:operationContext completionHandler:^(NSError *error) { | |
NSLog(@"upload done."); | |
exit(0); | |
}]; | |
} | |
}]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you have a problem with the streamfilecurrentoffset variable? It is returning nil for me.