iOS 利用阿里云上传及下载图片
2016-04-28 10:26
477 查看
因为本地服务器不想把头像的文件服务器放在那里,所以要app端直接上传头像至阿里云,需要用的时候则从阿里云下载,因为阿里云的SDK和文档比较清晰,所以整个过程比较顺利,很快就完成了,我这里只用到了异步文件上传与下载。
详细的sdk和文档可以去官网看:https://help.aliyun.com/document_detail/oss/sdk/ios-sdk/preface.html?spm=5176.product8314910_oss.6.319.ZvoCgV
代码中的xxxxx都是自己需要填充的
//自己这边的配置
NSString * const AccessKey = @"xxxxxx";
NSString * const SecretKey = @"xxxxxx";
NSString * const endPoint = @"xxxxxx";
//初始化
- (void)initOSSClient
{
[OSSLog enableLog];
id credential = [[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey:AccessKey
secretKey:SecretKey];
// 自实现签名,可以用本地签名也可以远程加签
id credential1 = [[OSSCustomSignerCredentialProvider alloc] initWithImplementedSigner:^NSString *(NSString *contentToSign, NSError *__autoreleasing *error) {
NSString *signature = [OSSUtil calBase64Sha1WithData:contentToSign withSecret:@""];
if (signature != nil) {
*error = nil;
} else {
// construct error object
*error = [NSError errorWithDomain:@"" code:OSSClientErrorCodeSignFailed userInfo:nil];
return nil;
}
return [NSString stringWithFormat:@"OSS %@:%@", @"", signature];
}];
id credential2 = [[OSSFederationCredentialProvider alloc] initWithFederationTokenGetter:^OSSFederationToken * {
OSSFederationToken * token = [OSSFederationToken new];
token.tAccessKey = AccessKey;
token.tSecretKey = SecretKey;
token.tToken = @"";
token.expirationTimeInGMTFormat = @"";
NSLog(@"get token: %@", token);
return token;
}];
OSSClientConfiguration * conf = [OSSClientConfiguration new];
conf.maxRetryCount = 2;
conf.timeoutIntervalForRequest = 30;
conf.timeoutIntervalForResource = 24 * 60 * 60;
client = [[OSSClient alloc] initWithEndpoint:endPoint credentialProvider:credential2 clientConfiguration:conf];
}
//异步上传
- (void)uploadObjectAsync:(NSString *)str
{
OSSPutObjectRequest * put = [OSSPutObjectRequest new];
put.bucketName = @"xxxxxxx";
put.objectKey = @"xxxxxxx";
// NSString * docDir = [self getDocumentDirectory];
put.uploadingFileURL = [NSURL fileURLWithPath:str];
// optional fields
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
};
put.contentType = @"";
put.contentMd5 = @"";
put.contentEncoding = @"";
put.contentDisposition = @"";
OSSTask * putTask = [client putObject:put];
[putTask continueWithBlock:^id(OSSTask *task) {
NSLog(@"objectKey: %@", put.objectKey);
if (!task.error) {
//上传成功后作出一些自己的动作
} else {
//上传失败
NSLog(@"upload object failed, error: %@" , task.error);
}
return nil;
}];
}
//异步下载
- (void)downloadObjectAsync:(NSString *)str
{
OSSGetObjectRequest * request = [OSSGetObjectRequest new];
request.bucketName = @"xxxxxxx";
request.objectKey = @"xxxxxxx";
//optional
request.downloadProgress = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
NSLog(@"%lld, %lld, %lld", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
};
// NSString * docDir = [self getDocumentDirectory];
// request.downloadToFileURL = [NSURL fileURLWithPath:[docDir stringByAppendingPathComponent:@"downloadfile"]];
OSSTask * getTask = [client getObject:request];
[getTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
//下载成功后作一些处理
NSLog(@"download object success!");
} else {
//下载失败作一些处理
NSLog(@"download object failed, error: %@" ,task.error);
}
return nil;
}];
}
详细的sdk和文档可以去官网看:https://help.aliyun.com/document_detail/oss/sdk/ios-sdk/preface.html?spm=5176.product8314910_oss.6.319.ZvoCgV
代码中的xxxxx都是自己需要填充的
//自己这边的配置
NSString * const AccessKey = @"xxxxxx";
NSString * const SecretKey = @"xxxxxx";
NSString * const endPoint = @"xxxxxx";
//初始化
- (void)initOSSClient
{
[OSSLog enableLog];
id credential = [[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey:AccessKey
secretKey:SecretKey];
// 自实现签名,可以用本地签名也可以远程加签
id credential1 = [[OSSCustomSignerCredentialProvider alloc] initWithImplementedSigner:^NSString *(NSString *contentToSign, NSError *__autoreleasing *error) {
NSString *signature = [OSSUtil calBase64Sha1WithData:contentToSign withSecret:@""];
if (signature != nil) {
*error = nil;
} else {
// construct error object
*error = [NSError errorWithDomain:@"" code:OSSClientErrorCodeSignFailed userInfo:nil];
return nil;
}
return [NSString stringWithFormat:@"OSS %@:%@", @"", signature];
}];
id credential2 = [[OSSFederationCredentialProvider alloc] initWithFederationTokenGetter:^OSSFederationToken * {
OSSFederationToken * token = [OSSFederationToken new];
token.tAccessKey = AccessKey;
token.tSecretKey = SecretKey;
token.tToken = @"";
token.expirationTimeInGMTFormat = @"";
NSLog(@"get token: %@", token);
return token;
}];
OSSClientConfiguration * conf = [OSSClientConfiguration new];
conf.maxRetryCount = 2;
conf.timeoutIntervalForRequest = 30;
conf.timeoutIntervalForResource = 24 * 60 * 60;
client = [[OSSClient alloc] initWithEndpoint:endPoint credentialProvider:credential2 clientConfiguration:conf];
}
//异步上传
- (void)uploadObjectAsync:(NSString *)str
{
OSSPutObjectRequest * put = [OSSPutObjectRequest new];
put.bucketName = @"xxxxxxx";
put.objectKey = @"xxxxxxx";
// NSString * docDir = [self getDocumentDirectory];
put.uploadingFileURL = [NSURL fileURLWithPath:str];
// optional fields
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
};
put.contentType = @"";
put.contentMd5 = @"";
put.contentEncoding = @"";
put.contentDisposition = @"";
OSSTask * putTask = [client putObject:put];
[putTask continueWithBlock:^id(OSSTask *task) {
NSLog(@"objectKey: %@", put.objectKey);
if (!task.error) {
//上传成功后作出一些自己的动作
} else {
//上传失败
NSLog(@"upload object failed, error: %@" , task.error);
}
return nil;
}];
}
//异步下载
- (void)downloadObjectAsync:(NSString *)str
{
OSSGetObjectRequest * request = [OSSGetObjectRequest new];
request.bucketName = @"xxxxxxx";
request.objectKey = @"xxxxxxx";
//optional
request.downloadProgress = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
NSLog(@"%lld, %lld, %lld", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
};
// NSString * docDir = [self getDocumentDirectory];
// request.downloadToFileURL = [NSURL fileURLWithPath:[docDir stringByAppendingPathComponent:@"downloadfile"]];
OSSTask * getTask = [client getObject:request];
[getTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
//下载成功后作一些处理
NSLog(@"download object success!");
} else {
//下载失败作一些处理
NSLog(@"download object failed, error: %@" ,task.error);
}
return nil;
}];
}
相关文章推荐
- ios-NSString总结
- IOS动画总结-Core Animation
- miss @end
- 浅谈iOS中的蓝牙技术(一) --GameKit.framework
- Ios中常见的问题
- iOS 调用地图导航
- iOS开发 ☞ 应用间跳转
- sdwebimage 图片大小 处理
- iOS开发——NSString小结
- IOS常用方法封装(1)
- iOS开发——瀑布流
- iOS开发——蓝牙小结
- 手把手教你如何搭建iOS项目基本框架
- iOS开发——观察者模式
- iOS开发——打电话
- iOS开发——工厂模式
- iOS开发——单例模式
- iOS 导航栏Title不居中
- iOS中使用脚本打包项目
- iOS开发之运动事件和远程控制