您的位置:首页 > 移动开发 > IOS开发

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;

}];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: