关于私有化部署Appcube访问第三方图片失败,http禁止访问
【摘要】 在ABC的https域名下,发起http图片访问请求失败
在https域名下,发起http图片访问请求失败
由于浏览器的安全限制导致的:https安全性高,不允许降级发起http的请求;http安全性低,是允许发起https请求的。平台页面都使用https,无法发起http请求。
针对第三方子系统,如人员通行,车辆通行的抓拍图片,由于部署时未签名认证,都是使用http协议,抓拍的图片如果直接同步到平台,是无法显示的
解决方案
方式一:第三方子系统使用https协议。
方式二:在获取时第三方图片转存OBS或者minio。
如下:先通过GET请求文件流,再通过OBS存入ABC。
// Here's your code.
//
// 图片转存OBS
import * as buffer from 'buffer'
import * as obs from 'objectstorage';
import * as context from 'context';
import * as uuid from 'uuid';
import * as db from 'db'
import * as sys from 'sys';
import * as http from 'http';
import { Logger } from './Common_logger';
import { format } from 'date'
const log = new Logger;
@action.object({ type: 'params' })
export class ParamsInput {
@action.param({ type: 'String', label: 'alarmType', description: "第三方图片url" })
url: string;
}
@action.object({ type: 'param' })
export class ParamsOutput {
@action.param({ type: 'String', label: 'path', description: "图片地址" })
path: string;
}
@action.object({ type: 'method' })
export const EveryMonth: string = 'EveryMonth';
export class HandleCamerAlarmProcess {
@action.method({ input: 'ParamsInput', output: 'ParamsOutput' })
public run(input: ParamsInput): ParamsOutput {
let out = new ParamsOutput();
out.path = this.fileUploadcharonTemp(input.url);
return out;
}
private fileUploadcharonTemp(fileUrl: string): any {
let httpClient = http.newClient();
try {
// 例如图片地址为http://192.168.0.1/pictrue,由于图片不能直接通过Http访问,需要转存OBS进行访问。
let resp = httpClient.get(fileUrl, null);
log.info("图片流:" + JSON.stringify(resp))
log.info("图片字节:" + JSON.stringify(resp.body))
console.error(this.commonDealPic(resp.body), 'resp')
return this.commonDealPic(resp.body);
} catch (e) {
console.error("图片上传错误", e);
log.error("图片上传错误:" + JSON.stringify(e))
return null;
}
}
//上传图片
private upLoadToOBS(data, filename) {
let res = this.uploadToOBS(data, filename);
return res && res.fileUrl;
}
private commonDealPic(imageBase64) {
// let imgBinary = buffer.from(imageBase64, buffer.Encoding.Base64).bytes();
let imgBinary = imageBase64;
let imgName = uuid.v4() + ".jpg"
let picFilePath = this.upLoadToOBS(imgBinary, imgName);
return picFilePath;
}
private uploadToOBS(data, fileName) {
let filePath = this.getUpLoadFilePath(fileName);
let storageName = sys.getParameter("public_ObsStoreName") || "";
let obsClient = obs.newClient(obs.StoreType.PROXY, storageName);
let view = obsClient.uploadObject(filePath, data);
let fileUrl = '/u-route/baas/sys/v1.1/connectors/objectstorageproxy/' + storageName + '/viewobject?object=' + filePath;
let res = {
"view": view,
"path": filePath,
"fileUrl": fileUrl
}
return res;
}
//新增
private getUpLoadFilePath(fileName) {
//依据日期建立文件夹,分目录存储
let objectName = 'lXX97e-upload/smartcampus/personmanagefile/';
let formatDate = format(new Date(), "yyyyMM", context.getTimeZone());
let filePath = objectName + EveryMonth + '/' + formatDate + '/' + fileName.substr(fileName.lastIndexOf('/') + 1);
return filePath;
}
}
返回地址拼接域名就可以直接在浏览器访问了
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)