【已更新最新开发文章,点击查看详细】
BIMFACE公有云使用了分布式对象存储来存储用户上传的模型/图纸文件。如使用普通的文件上传接口, 文件流会通过BIMFACE的服务器,再流向最终的分布式存储系统,整个上传过程会受BIMFACE服务器的带宽限制,上传速度非最优。 如使用文件直传接口,开发者应用在申请到一个Policy凭证后,可以直接上传文件跟BIMFACE后台的分布式存储系统, 这样上传速度和稳定性都会有提升,是我们推荐的上传方式。
另外,很多BIMFACE应用都有自己的Web前端页面,这种情况我们也推荐使用文件直传接口。
友情提醒:
BIMFACE 公有云支持文件直传。
私有化部署时使用的对象存储是 MinIO,不支持 Policy 上传。使用普通文件流上传 或者 指定外部文件URL方式上传。
文件直传的逻辑图如下:
- 使用流程如下:
-
-
开发者应用向 BIMFACE 申请上传 Policy 请求。
-
BIMFACE 返回上传 Policy 和签名给开发者应用。
-
开发者应用使用在第二个步骤中获取的 URL 信息,直接上传文件数据到 BIMFACE 后端的分布式对象存储。
-
步骤一:获取文件直传的policy凭证
请求地址:GET https://file.bimface.com/upload/policy
请求参数:
请求 path(示例):https://file.bimface.com/upload/policy?name=example.rvt
请求 header(示例):"Authorization: Bearer dc671840-bacc-4dc5-a134-97c1918d664b"
HTTP响应示例(200):
C#实现方法:application/octet-stream
1 /// <summary> 2 /// 获取文件直传的policy凭证 3 /// </summary> 4 /// <param name="accessToken">令牌</param> 5 /// <param name="fileName">【必填】文件的全名</param> 6 /// <param name="sourceId">【可选】调用方的文件源ID,不能重复</param> 7 /// <returns></returns> 8 public virtual FileUploadPolicyResponse GetFileUploadPolicy(string accessToken, string fileName, string sourceId = "") 9 { 10 /* BIMFACE 使用了分布式对象存储来存储用户上传的模型/图纸文件。 11 如使用普通的文件上传接口,文件流会通过BIMFACE的服务器,再流向最终的分布式存储系统,整个上传过程会受BIMFACE服务器的带宽限制,上传速度非最优。 12 如使用文件直传接口,开发者应用在申请到一个Policy凭证后,可以直接上传文件跟BIMFACE后台的分布式存储系统, 13 这样上传速度和稳定性都会有提升,是我们推荐的上传方式。 14 */ 15 16 /* 使用流程如下: 17 1、开发者应用向BIMFACE申请上传Policy请求 18 2、BIMFACE返回上传Policy和签名给开发者应用。 19 */ 20 21 //GET https://file.bimface.com/upload/policy 。例如:https://file.bimface.com/upload/policy?name=example.rvt 22 string url = string.Format(BimfaceConstants.FILE_HOST + "/upload/policy?name={0}", fileName.UrlEncode(Encoding.UTF8)); //文件的全名,使用URL编码(UTF-8),最多256个字符 23 if (sourceId.IsNotNullAndWhiteSpace()) 24 { 25 url = url + "&sourceId=" + sourceId; 26 } 27 28 BimFaceHttpHeaders headers = new BimFaceHttpHeaders(); 29 headers.AddOAuth2Header(accessToken); 30 31 try 32 { 33 FileUploadPolicyResponse response; 34 35 HttpManager httpManager = new HttpManager(headers); 36 HttpResult httpResult = httpManager.Get(url); 37 if (httpResult.Status == HttpResult.STATUS_SUCCESS) 38 { 39 response = httpResult.Text.DeserializeJsonToObject<FileUploadPolicyResponse>(); 40 } 41 else 42 { 43 response = new FileUploadPolicyResponse 44 { 45 Message = httpResult.RefText 46 }; 47 } 48 49 return response; 50 } 51 catch (Exception ex) 52 { 53 throw new Exception("获取文件直传的policy凭证时发生异常!", ex); 54 } 55 }
其中引用的 httpManager.Get() 方法如下:
1 /// <summary> 2 /// HTTP-GET方法,(不包含body数据)。 3 /// 发送 HTTP 请求并返回来自 Internet 资源的响应(HTML代码) 4 /// </summary> 5 /// <param name="url">请求目标URL</param> 6 /// <returns>HTTP-GET的响应结果</returns> 7 public HttpResult Get(string url) 8 { 9 return RequestString(url, null, WebRequestMethods.Http.Get, null); 10 }
1 /// <summary> 2 /// HTTP请求(包含文本的body数据) 3 /// </summary> 4 /// <param name="url">请求目标URL</param> 5 /// <param name="data">主体数据(普通文本或者JSON文本)。如果参数中有中文,请使用合适的编码方式进行编码,例如:gb2312或者utf-8</param> 6 /// <param name="method">请求的方法。请使用 WebRequestMethods.Http 的枚举值</param> 7 /// <param name="contentType"><see langword="Content-type" /> HTTP 标头的值。请使用 ContentType 类的常量来获取</param> 8 /// <returns></returns> 9 private HttpResult RequestString(string url, string data, string method, string contentType) 10 { 11 HttpResult httpResult = new HttpResult(); 12 HttpWebRequest httpWebRequest = null; 13 14 try 15 { 16 httpWebRequest = WebRequest.Create(url) as HttpWebRequest; 17 httpWebRequest.Method = method; 18 httpWebRequest.Headers = HeaderCollection; 19 httpWebRequest.CookieContainer = CookieContainer; 20 if (!string.IsNullOrWhiteSpace(contentType)) 21 { 22 httpWebRequest.ContentType = contentType;// 此属性的值存储在WebHeaderCollection中。如果设置了WebHeaderCollection,则属性值将丢失。所以放置在Headers 属性之后设置 23 } 24 httpWebRequest.UserAgent = _userAgent; 25 httpWebRequest.AllowAutoRedirect = _allowAutoRedirect; 26 httpWebRequest.ServicePoint.Expect100Continue = false; 27 28 if (data != null) 29 { 30 httpWebRequest.AllowWriteStreamBuffering = true; 31 using (Stream requestStream = httpWebRequest.GetRequestStream()) 32 { 33 requestStream.Write(EncodingType.GetBytes(data), 0, data.Length);//将请求参数写入请求流中 34 requestStream.Flush(); 35 } 36 } 37 38 HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse; 39 if (httpWebResponse != null) 40 { 41 GetResponse(ref httpResult, httpWebResponse); 42 httpWebResponse.Close(); 43 } 44 } 45 catch (WebException webException) 46 { 47 GetWebExceptionResponse(ref httpResult, webException); 48 } 49 catch (Exception ex) 50 { 51 GetExceptionResponse(ref httpResult, ex, method, contentType); 52 } 53 finally 54 { 55 if (httpWebRequest != null) 56 { 57 httpWebRequest.Abort(); 58 } 59 } 60 61 return httpResult; 62 }