
如果OSS文件设置保护,需要鉴权才能访问,添加请求头鉴权,SDK方法如上;
将鉴权信息和地址、时间返回给前端,前端直接从oss上读取
String filePath = "/admin/2023/6/183569314928918546.png";
RequestMessage requestMessage = new RequestMessage("sl-ops-oss", filePath);
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
Calendar cd = Calendar.getInstance();
sdf.setTimeZone(TimeZone.getTimeZone("GMT")); // 设置时区为GMT
String str = sdf.format(cd.getTime());
System.out.println(str);
Map<String, String> headers = new HashMap<>();
headers.put("Date", str);
requestMessage.setHeaders(headers);
String signature = SignUtils.buildSignature(ossSecretAccessKey, "GET", "/" + bucket + "/" + filePath, requestMessage);
String Authorization = SignUtils.composeRequestAuthorization(ossAccessKeyId, signature);
JSONObject jsonObject = new JSONObject();
jsonObject.put("Date", str);
jsonObject.put("Authorization", Authorization);
jsonObject.put("url", String.format(url, bucket) + filePath);