当前位置:网站首页>OSS upload
OSS upload
2022-04-21 07:33:00 【A little weak chicken with a dream】
// obtain oss url
private String getUrl(File file, String excelName) {
String url=null;
try {
url = OssUploadController.uploadInputSteam(new FileInputStream(file), excelName+".rar");
System.out.println(url);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return url;
}
// Upload and obtain url
public void test(){
File file = ZipUtil.zip(PathKit.getWebRootPath() + "/" + fileUuid + "/workModel");
String url = getUrl(file, excelName);
if (StrKit.isBlank(url)){
re(" Failure ");
return;
}
rsPara(" success ",Kv.by("url",url));
}
/** * TODO * oss File upload controller **/
public class OssUploadController extends BaseController{
/** * oss Get file upload signature */
@Clear
@Before(AllowCrossDomainInterceptor.class)
public void getSign(){
String endpoint = get("OSS-ENDPOINT");
String accessId = get("OSS-ACCESSID");
String accessKey = get("OSS-ACCESSKEY");
String bucket = get("OSS-BUCKET");
String dir = get("OSS-DIR");
String host = "https://" + bucket + "." + endpoint;
try {
OSS client = new OSSClientBuilder().build(endpoint, accessId, accessKey);
long expireTime = 30;
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
Date expiration = new Date(expireEndTime);
PolicyConditions policyConds = new PolicyConditions();
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
String postPolicy = client.generatePostPolicy(expiration, policyConds);
byte[] binaryData = postPolicy.getBytes("utf-8");
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
String postSignature = client.calculatePostSignature(postPolicy);
Map<String, String> respMap = new LinkedHashMap<String, String>();
respMap.put("code", "0");
respMap.put("accessid", accessId);
respMap.put("policy", encodedPolicy);
respMap.put("signature", postSignature);
// respMap.put("expire", formatISO8601Date(expiration));
respMap.put("dir", dir);
respMap.put("host", host);
respMap.put("expire", String.valueOf(expireEndTime / 1000));
renderJson(respMap);
} catch (Exception e) {
e.printStackTrace();
rw(e.getMessage());
}
}
// Upload files and stream them to Alibaba oss
public static String uploadInputSteam(InputStream file, String fileName) {
String endpoint = get("OSS-ENDPOINT");
String accessId = get("OSS-ACCESSID");
String accessKey = get("OSS-ACCESSKEY");
String bucket = get("OSS-BUCKET");
// establish OSSClient example .
OSS ossClient = new OSSClientBuilder().build(endpoint, accessId, accessKey);
// Upload file stream .
ossClient.putObject(bucket, fileName, file);
// Set up URL The expiration date is 24 Hours .
Date expiration = new Date(System.currentTimeMillis() + 24 * 3600 * 1000);
ossClient.setBucketAcl(bucket, CannedAccessControlList.PublicRead);
// Generate to GET Method to access the signature URL, Visitors can access related content directly through the browser .
URL url = ossClient.generatePresignedUrl(bucket, fileName, expiration);
// close OSSClient.
ossClient.shutdown();
return url.toString();
}
/** * Cross domain request interceptor * */
public class AllowCrossDomainInterceptor implements Interceptor {
@Override
public void intercept(Invocation inv) {
inv.getController().getResponse().addHeader("Access-Control-Allow-Origin", "*");
inv.getController().getResponse().addHeader("Access-Control-Allow-Credentials", "true");
inv.getController().getResponse().addHeader("Access-Control-Allow-Methods", "OPTIONS,HEAD,GET,PUT,POST,DELETE,PATCH");
inv.getController().getResponse().addHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, login_code,x-token,platform_code");
inv.getController().getResponse().addHeader("Access-Control-Max-Age", "3600");
if (StrKit.notBlank(inv.getController().getRequest().getMethod()) && inv.getController().getRequest().getMethod().equals("OPTIONS")) {
inv.getController().renderText("");
return;
}
String json = HttpKit.readData(inv.getController().getRequest());
if (StrKit.notBlank(json)) {
if (PropKit.getBoolean("devMode", false)) {
System.out.println(json);
}
JSONObject obj = JSONObject.parseObject(json);
inv.getController().setAttr("obj", obj);
}
inv.invoke();
}
版权声明
本文为[A little weak chicken with a dream]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210623034650.html
边栏推荐
- 性能测试工具JMeter & Jprofiler 的安装
- Build a deep learning server and environment configuration from scratch
- Dpdk-16.04 IGB CRC length statistics
- Reading notes - how to be a great person
- nodered连接数据库
- 利用GoogleColab进行YOLO行人检测计数(YOLOv3+DeepSORT)
- Unity限制旋转角度
- 基于UnityGameFramework框架的AssetBundle打包
- Several views on individual and environment
- SourceTree版本回溯以及单个改动版本回溯
猜你喜欢

国产首发USBhub大全,USB HUB2.0,HUB3.0,旺玖PROLIFIC、PL2586、MA8601,和芯润德SL2.2A、SL2.2S,替换汤铭的FE1.1、FE1.1S、、威锋VL810

Porting ucosiii to ucosiii - task suspend / restore / hook / (nanny tutorial)

Study the process of automatically loading kernel modules from SYSTEMd udevd running log

宏晶微MS9123,USB 投屏控制芯片,USB 投屏器,支持 CVBS、S-Video 视频接口,

使用Render Texture在UI上显示3D模型动画

未授权访问漏洞

Export excel custom style with hutool tool ----- excel compression jar

Installation of performance testing tool JMeter & JProfiler

Nodered connection database
![[hand pose estimation] [paper accuracy] pose guided structured region ensemble network for cascaded hand pose estimation](/img/05/3058ef0519caf82573954b6195e5e7.png)
[hand pose estimation] [paper accuracy] pose guided structured region ensemble network for cascaded hand pose estimation
随机推荐
Dpdk problem location: a port of a four port x710 network card cannot be initialized
Common MySQL statements
Unity关于IsPointerOverGameObject接口真机失效问题
Lua programming -- Notes
The use of NuMA by dpdk and the basis of performance tuning under multi NUMA architecture
idea2021.1在Mapper中写sql出现:Unable to resolve column/table
Unable to resolve dependency for ':app@debug/compileClasspath': Could not download mapsforge-map.jar
Method and principle of obtaining the CPU core of the last execution of the process
Unity内置地形Terrain优化
如何使用Keil5开发MSP430及Tiva系列开发板
JS不同时间格式转换
用友OA漏洞复现手册
[intensive reading] deep surface normal estimation with hierarchical rgb-d fusion
mask + rgb实现背景减除
Run eNet on TX2
Huatian OA vulnerability reproduction manual
龙讯系列: LT8911,LVDS/MIPI DSI信号转换成EDP信号的转接芯片,LT8619,HDMI/MHL信号转换成LVDS/RGB信号
时钟IC,INS5101A,I2C低功耗 RTC 实时时钟芯片,替换HYM8563,替换(NXP)PCF8563,TCS8563,I2C低功耗 RTC 实时时钟芯片
PG database cannot use zh_ CN. UTF-8:initdb: error: locale “zh_CN.UTF-8“ requires unsupported encoding “GBK“
Installation of performance testing tool JMeter & JProfiler