Skip to content

Instantly share code, notes, and snippets.

@hanfengs
Last active July 16, 2019 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanfengs/a21a41c77ae4076a89b939b952da2ab4 to your computer and use it in GitHub Desktop.
Save hanfengs/a21a41c77ae4076a89b939b952da2ab4 to your computer and use it in GitHub Desktop.
[阿里云OSS方式下载文件]
/*
阿里云出于安全考虑,从2018年8月13日起,直接使用OSS访问域名,从互联网访问OSS上的网页类型文件(mimetype为text/html,扩展名包括htm、 html、jsp、plg、htx 和stm)时,Response Header中会自动加上 Content-Disposition:'attachment=filename;'。即从浏览器访问网页类型文件时,会以附件 形式进行下载。
用户使用自有域名访问OSS的请求,Response Header中不会加上此信息。如何使用自有域名访问OSS,请参考OSS帮助文档“绑定自定义域名”。
表现的现象是, web中下载文件时,我们一般设置Content-Disposition告诉浏览器下载文件的名称,是否在浏览器中内嵌显示.
Content-disposition: inline; filename=1502849449726.jpg表示浏览器内嵌显示一个文件
Content-disposition: attachment; filename=1502849449726.xlsx表示会下载文件,如火狐浏览器中
//方法调用示例demo
NSURL *url = [NSURL URLWithString:@"https://quxue-data.oss-cn-beijing.aliyuncs.com/scene_course/reading/test_record/test223.json"];
NSString *host = [url host];
NSString *buckName = [host componentsSeparatedByString:@"."].firstObject;
NSString *path = [url path];
NSString *objectKey = [path substringFromIndex:1];
*/
[[TSAliyunOSSService new] asyncGetBucketName:buckName ObjectKey:objectKey localFilePath:@"" success:^(NSData * _Nonnull data) {
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
TSEvaluationSubjectInfoModel *model = [TSEvaluationSubjectInfoModel yy_modelWithJSON:str];
NSLog(@"%@",model);
} failure:^(NSError * _Nonnull error) {
}];
- (void)asyncGetBucketName:(NSString *)bucketName
ObjectKey:(NSString *)objectKey
localFilePath:(NSString *)filePath
success:(void (^_Nullable)(NSData *))success
failure:(void (^_Nullable)(NSError*))failure{
if (![objectKey oss_isNotEmpty]) {
NSError *error = [NSError errorWithDomain:NSInvalidArgumentException code:0 userInfo:@{NSLocalizedDescriptionKey: @"objectKey should not be nil"}];
failure(error);
return;
}
OSSGetObjectRequest * request = [OSSGetObjectRequest new];
// 必填字段
request.bucketName = bucketName;
request.objectKey = objectKey;
// 可选字段
request.downloadProgress = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
// 当前下载段长度、当前已经下载总长度、一共需要下载的总长度
NSLog(@"%lld, %lld, %lld", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
};
// request.range = [[OSSRange alloc] initWithStart:0 withEnd:99]; // bytes=0-99,指定范围下载
// request.downloadToFileURL = [NSURL fileURLWithPath:@"<filepath>"]; // 如果需要直接下载到文件,需要指明目标文件地址
OSSTask * getTask = [[OSSManager sharedManager].defaultClient getObject:request];
[getTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
NSLog(@"download object success!");
OSSGetObjectResult * getResult = task.result;
NSLog(@"download result: %@", getResult.downloadedData);
success(getResult.downloadedData);
} else {
NSLog(@"download object failed, error: %@" ,task.error);
failure(task.error);
}
return nil;
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment