相关资料
- 官方文档
- 上传SDK-objc-sdk源码
- 上传SDK-java-sdk源码
iOS使用上传SDK介绍
完整代码请查看SDK源码中的ViewController文件
- 为
ViewController
添加NOSUploadRequestDelegate
代理 - 并实现以下三个代理方法
-(NSString *)NOSUploadAppKey{
return kNOSTestAppKey;
}
-(NSString *)NOSVcloudAppAccid{
return kNOSTestAccid;
}
-(NSString *)NOSVcloudAppToken{
return kNOSTestToken;
}
- 在
viewDidLoad
中实例化上传管理类
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSError *error = nil;
NSString *dir = [NSTemporaryDirectory() stringByAppendingString:@"nos-ios-sdk-test"];
NSLog(@"%@", dir);
//创建断点续传目录并记录
NOSFileRecorder *file = [NOSFileRecorder fileRecorderWithFolder:dir error:&error];
//配置基于位置服务的参数
NOSConfig *conf = [[NOSConfig alloc] initWithLbsHost: @"http://wanproxy.127.net"
withSoTimeout: [_soTimeoutText.text intValue]
//withConnectionTimeout: [_connectTimeoutText.text intValue]
withRefreshInterval: [_monitorInterval.text intValue]
withChunkSize: [_chunkSizeText.text intValue] * 1024
withMoniterInterval: [_monitorInterval.text intValue]
withRetryCount: [_retryCountText.text intValue]];
//将其设置为全局变量
[NOSUploadManager setGlobalConf:conf];
if (error) {
NSLog(@"%@", error);
}
//实例化上传管理类
upManager = [NOSUploadManager sharedInstanceWithRecorder: (id)file
recorderKeyGenerator: nil];
//设置上传管理类的delegate
upManager.delegate = self;
}
- 获取要上传的图片或者视频的本地文件路径并上传
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
NSString *name = @"";
NSString *filepath = @"";
NSInteger key = 0;
//当选择的类型是图片
if ([type isEqualToString:@"public.image"]){
UIImage *image = info[UIImagePickerControllerOriginalImage];
NSData *data = UIImageJPEGRepresentation(image, 1.0);
NSString *dataMd5 = [Md5Utls getMD5WithData:data];
filepath = [NSString stringWithFormat:@"%@%@",NSTemporaryDirectory(),dataMd5];
if (![[NSFileManager defaultManager] fileExistsAtPath:filepath]) {
[data writeToFile:filepath atomically:YES];
}
name = [[filepath lastPathComponent] stringByAppendingPathExtension:@"jpg"];
key = 0;
}else if ([type isEqualToString:@"public.movie"]){
//如果是视频
NSURL *url = info[UIImagePickerControllerMediaURL];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *dataMd5 = [Md5Utls getMD5WithData:data];
filepath = [NSString stringWithFormat:@"%@%@",NSTemporaryDirectory(),dataMd5];
if (![[NSFileManager defaultManager] fileExistsAtPath:filepath]) {
[data writeToFile:filepath atomically:YES];
}
name = [[filepath lastPathComponent] stringByAppendingString:@".MOV"];
key = 1;
}
//文件上传初始化
__weak typeof (self) weakSelf = self;
[upManager fileUploadInit:name userFileName:nil typeId:nil presetId:nil uploadCallbackUrl:nil callbackUrl:nil description:nil watermarkId:nil userDefInfo:nil success:^(id responseObject) {
NSLog(@"%@",responseObject);
if ([[responseObject objectForKey:@"code"] integerValue] == 200) {
[weakSelf doUpload:filepath type:key];
}
} fail:^(NSError *error) {
NSLog(@"%@",error);
}];
[picker dismissViewControllerAnimated:YES
completion:nil];
}
- 文件上传初始化方法介绍
/**
* 文件上传初始化接口:用于获取xNosToken(上传凭证)、bucket(存储对象的桶名)、object(生成的唯一对象名)
*
* @param originFileName 上传文件的原始名称(包含后缀名)
* @param userFileName 用户命名的上传文件名称
* @param typeId 视频所属的类别Id(不填写为默认分类)
* @param presetId 视频所需转码模板Id(不填写为默认模板,默认模板不进行转码)
* @param uploadCallbackUrl 上传成功后回调客户端的URL地址(需标准http格式)
* @param callbackUrl 转码成功后回调客户端的URL地址(需标准http格式)
* @param description 上传视频的描述信息
* @param watermarkId 视频水印Id(不填写为不添加水印,如果选择,
请务必在水印管理中提前完成水印图片的上传和参数的配置;
且必需设置prestId字段,且presetId字段不为默认模板)
* @param userDefInfo 用户自定义信息,回调会返回此信息
*/
-(void)fileUploadInit:(NSString *)originFileName
userFileName:(NSString *)userFileName
typeId:(NSString *)typeId
presetId:(NSString *)presetId
uploadCallbackUrl:(NSString *)uploadCallbackUrl
callbackUrl:(NSString *)callbackUrl
description:(NSString *)description
watermarkId:(NSString *)watermarkId
userDefInfo:(NSString *)userDefInfo
success:(NOSUploadRequestSuccess)success
fail:(NOSUploadRequestFail)fail;
- 上传方法
//查看图片上传成功:http://nos.netease.com/桶名/对象名
//http://nos.netease.com/vodk32ywxdf/8078e838-8fc1-411f-b980-9676e945c4f6.jpg
//查看视频上传成功:看控制管理台:http://test.vcloud.163.com/admin#/vod/video
- (void)doUpload:(NSString *)filepath type:(NSInteger)key{
cancelUpload = NO;
//文件初始化后可获取
NSString *bucket = upManager.bucketName;
NSString *object = upManager.objectName;
NSString *token = upManager.xNosToken;
NSString *localFileName = filepath;
NSString* type = @"";
if (key == 0) {
type = @"image/jpeg";
}else{
type = @"application/octet-stream";
}
//可选参数集合:上传进度回调 视频:application/octet-stream 图片:image/jpeg
NOSUploadOption *option = [[NOSUploadOption alloc] initWithMime:type
progressHandler: ^(NSString *key, float percent) {
NSLog(@"current progress:%f", percent);
}
metas: nil
cancellationSignal: ^BOOL{
return cancelUpload;
}];
//文件上传
[upManager putFileByHttps: localFileName bucket:bucket key:object
token: token
complete: ^(NOSResponseInfo *info, NSString *key, NSDictionary *resp) {
NSLog(@"上传完成~~");
NSLog(@"info=%@", info);
NSLog(@"resp=%@", resp);
}
option: option];
NSLog(@"开始上传~~");
}
Android使用上传SDK介绍
package com.netease.vcloud.upload.demo;
import com.netease.vcloud.auth.BasicCredentials;
import com.netease.vcloud.auth.Credentials;
import com.netease.vcloud.client.VcloudClient;
import com.netease.vcloud.upload.param.QueryVideoIDorWatermarkIDParam;
import com.netease.vcloud.util.FileUtil;
import org.apache.log4j.Logger;
import java.util.HashMap;
import java.util.Map;
/**
* Title: UploadVideoDemo
* Description: 简单的视频上传的Demo
* Company: com.netease.vcloud
*
* @date 2016-6-22
*/
public class UploadVideoDemo {
/**
* 日志实例
*/
public static final Logger logger = Logger.getLogger(UploadVideoDemo.class);
public static void main(String[] args) {
/* 输入个人信息 */
/* 开发者平台分配的appkey 和 appSecret */
String appKey = "";
String appSecret = "";
Credentials credentials;
credentials = new BasicCredentials(appKey, appSecret);
VcloudClient vclient = new VcloudClient(credentials);
try {
/*请输入上传文件路径*/
String filePath = "e:\\1.mp4";
//String filePath = "e:\\image_20160711145925.png";
Map initParamMap = new HashMap();
/*输入上传文件的相关信息 */
/* 上传文件的原始名称(包含后缀名) 此参数必填*/
initParamMap.put("originFileName", FileUtil.getFileName(filePath));
/* 用户命名的上传文件名称 此参数非必填*/
//initParamMap.put("userFileName", "for_love.mp4");
/* 视频所属的类别ID(不填写为默认分类)此参数非必填*/
// initParamMap.put("typeId", new Long(1056));
/* 频所需转码模板ID(不填写为默认模板) 此参数非必填*/
// initParamMap.put("presetId", new Long(2007));
/* 转码成功后回调客户端的URL地址(需标准http格式) 此参数非必填*/
//initParamMap.put("callbackUrl", "");
/* 上传视频的描述信息 此参数非必填*/
//initParamMap.put("description", "love.mp4");
/* 上传视频的视频水印Id 此参数非必填*/
//initParamMap.put("watermarkId", new Long(1));
/** 上传成功后回调客户端的URL地址(需标准http格式) 此参数非必填*/
//initParamMap.put("uploadCallbackUrl", "");
/** 用户自定义信息,会在上传成功或转码成功后通过回调返回给用户 此参数非必填 */
// initParamMap.put("userDefInfo", null);
QueryVideoIDorWatermarkIDParam queryVideoIDParam = vclient.uploadVideo(filePath, initParamMap);
if (null != queryVideoIDParam) {
logger.info("[UploadDemo] upload video successfully and the vid is " + queryVideoIDParam.getRet().getList().get(0).getVid());
logger.info("[UploadDemo] upload video successfully and the imgid is " + queryVideoIDParam.getRet().getList().get(0).getImgId());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}