从百度官网下载SDK,文档地址:https://cloud.baidu.com/doc/BOS/s/wjwvys18m
log4net.dll
和Newtonsoft.Json.dll为必要的文件,且Newtonsoft.Json.dll版本必须大于8.0否则异常。Newtonsoft.Json.dll最好通过Nuget包升级或安装,否则还需要自己改web.config里面的版本号。
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
前台代码:https://www.cnblogs.com/Lynnyin/p/13037266.html
private const string BaiduAccessKeyId = "你的AK"; private const string BaiduSecretAccessKey = "你的SAK"; private const string BaiduBucketName = "你的BucketName";//后台去创建 private const string RootPath = "Demo";//文件存储路径(如果写了,但是后台没有这个文件夹会自动创建) public JsonResult UploadBd() { JsonResult rJson = new JsonResult(); //前台获取图片列表插入到dropzone.js插件时,会调用Upload方法,但是没有文件传过来,所以这里会返回空信息会去 if (Request.Files.Count==0) { var returnData1 = new { State = false, Msg = "" }; rJson.Data = returnData1; rJson.JsonRequestBehavior = JsonRequestBehavior.AllowGet; return rJson; } var file = Request.Files[0]; //根据上传文件名获取完整名称 var uploadfileName = file.FileName; byte[] uploadFileBytes = new byte[file.ContentLength]; try { //读取文件二进制流 file.InputStream.Read(uploadFileBytes, 0, file.ContentLength); } catch (Exception ex) { var returnData1 = new { State = false,Msg=ex.Message }; // context.Response.Write(ex.Message); rJson.Data = returnData1; rJson.JsonRequestBehavior = JsonRequestBehavior.AllowGet; return rJson; } try { //以上代码都可要可不要。对象存储Bos这里才开始 const string accessKeyId = BaiduAccessKeyId; // 您的Access Key ID const string secretAccessKey = BaiduSecretAccessKey; // 您的Secret Access Key const string endpoint = "http://bj.bcebos.com"; //传入Bucket所在区域域名,这里所有用户都一样都是使用bj.bcebos.com // 初始化一个BosClient BceClientConfiguration config = new BceClientConfiguration(); config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey); config.Endpoint = endpoint; BosClient client = new BosClient(config); //上传文件至百度BOS PutObjectResponse presp = client.PutObject(BaiduBucketName, RootPath+"/"+file.FileName, uploadFileBytes); //初始化BceDoc的配置信息,有些文档或者网上的Demo使用的域名为:http://doc.bj.bcebos.com,现在这个是不可用的。可用的是:http://doc.bj.baidubce.com,现在只支持这个,且必须是华北,北京区域的 BceClientConfiguration docConfig = GetBceClientConfig("http://doc.bj.baidubce.com"); docConfig.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey); DocClient _docClient = new DocClient(docConfig); //DOC发布转码 CreateDocumentFromBosRequest rq = new CreateDocumentFromBosRequest(); rq.Title = file.FileName; rq.Bucket = BaiduBucketName; rq.Object = RootPath + "/" + file.FileName; CreateDocumentFromBosResponse resp = _docClient.CreateDocumentFromBos(rq); //如果docConfig的Endpoint不存在则会报异常404,如果Endpoint使用的不是http://doc.bj.baidubce.com,但是填写的Endpoint存在则会没有DocumentId返回。 var docid= resp.DocumentId; if (docid!=null&&docid!="") { var returnData = new { State=true,file = file.FileName, DocId =docid }; rJson.Data = returnData; rJson.JsonRequestBehavior = JsonRequestBehavior.AllowGet; return rJson; } else { var returnData = new { State = true, file = "", dataUrl = "" }; rJson.Data = returnData; rJson.JsonRequestBehavior = JsonRequestBehavior.AllowGet; return rJson; } } catch (Exception ex) { var returnData1 = new { State = false, Msg = ex.Message }; // context.Response.Write(ex.Message); rJson.Data = returnData1; rJson.JsonRequestBehavior = JsonRequestBehavior.AllowGet; return rJson; } }