1.创建一个存储账户
2.开发文档 https://docs.microsoft.com/zh-cn/azure/storage/blobs/storage-ios-how-to-use-blob-storage
存储之间的关系是 :在存储账户中创建容器,在容器中创建blob。
账户包含容器,容器中包含blob,blob中包含 上传的内容
创建容器时候容器名必须小写
配置连接字符串的作用,相当于配置文件:
a.连接到存储模拟器
b.访问存储账户
c.访问指定资源
查看配置文件的网址如下所示(后面的"xufei",是我自己在账户中创建的):
https://renmin.blob.core.windows.net/xufei/
上传的图片无法在浏览器查看,是因为没有读取的权限,修改容器的权限为读取权限就行了
以下是上传图片到微软云的代码:
//注意这里的connectionStr 很重要,在账户中可以找到,
//一定是https,否则会报错。
NSString *connectionStr = @"DefaultEndpointsProtocol=https;AccountName=renmin;AccountKey=n0yVC/XguvDfvE4k3dIFWOnhNH85aEYnEjlrNKPkSoFPj5IOzd0H47yfiRuhM7WMTxF9fD6MMdfrcxoCBXAK+A==";
NSError *error = nil;
AZSCloudStorageAccount *account = [AZSCloudStorageAccount accountFromConnectionString:connectionStr error:&error];
AZSCloudBlobClient *blobClient = [account getBlobClient];
blobClient.defaultRequestOptions.serverTimeout = 30.0;
NSString *containerName = @"xufei";
AZSCloudBlobContainer *blobContainer = [blobClient containerReferenceFromName:containerName];
[blobContainer createContainerIfNotExistsWithCompletionHandler:^(NSError* error,BOOL ret){
NSLog(@"Error in creating container ==== %@",error);
NSDate *date = [NSDate date];
NSInteger timeInterval = (NSInteger)[date timeIntervalSince1970];
NSString *imageName = @"";
if ( isCover == true) {
imageName = [NSString stringWithFormat:@"%ld.small.jpg",(long)timeInterval];
}else{
imageName = [NSString stringWithFormat:@"%ld.jpg",(long)timeInterval];
}
AZSCloudBlockBlob *blockBlob = [blobContainer blockBlobReferenceFromName:imageName];
blockBlob.properties.contentType = @"image/jpeg";
[blockBlob uploadFromData:data completionHandler:^(NSError * error) {
if (error != nil) {
//NSLog(@"error === %@",error);
if (complete) {
complete(@"",error);
}
}else{
//将以下的imageUrl放在浏览器上是可以显示和下载的,下载之后是一张图片,如果不是图片修改存储器的权限为读写权限就好了。
NSString *newImageUrl = [NSString stringWithFormat:@"%@%@",@"https://renmin.blob.core.windows.net/xufei/",imageName];
if (complete) {
complete(newImageUrl,nil);
}
}
}];
}];
网友评论