|
@@ -24,6 +24,18 @@ export class ProvierOssAli{
|
|
|
constructor(){
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 上传函数:可单独设置对象私有属性
|
|
|
+ * @param file
|
|
|
+ * @param onProcess
|
|
|
+ * @param options
|
|
|
+ * @returns
|
|
|
+ * @example
|
|
|
+ * // 普通上传
|
|
|
+ * let r1 = await upload(file)
|
|
|
+ * // 私密上传 - 单个对象设置私密,需要临时签名URL下载
|
|
|
+ * let r2 = await upload(file,null,{acl:"private"})
|
|
|
+ */
|
|
|
async upload(file:File,onProcess?:Function,options?:any){
|
|
|
await this.initClient();
|
|
|
let now = new Date();
|
|
@@ -38,9 +50,57 @@ export class ProvierOssAli{
|
|
|
console.log('put success: %j', r1);
|
|
|
return r1
|
|
|
}
|
|
|
- async get(url:string){
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载函数:可自动识别是否加密,获取私密签名
|
|
|
+ * @desc 判断URL是否可下载,若私有则临时授权
|
|
|
+ * @param fileUrl 必填,直接下载URL
|
|
|
+ * @param fileName 选填,指定下载文件名
|
|
|
+ * @example
|
|
|
+ */
|
|
|
+ async download(fileUrl:string,fileName?:string){
|
|
|
+ let response = await fetch(fileUrl)
|
|
|
+ if(response?.status==403){
|
|
|
+ let tempUrl = await this.signatureUrl(fileUrl);
|
|
|
+ if(tempUrl){
|
|
|
+ response = await fetch(tempUrl)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!response.ok) {
|
|
|
+ console.log(response)
|
|
|
+ throw new Error('Network response was not ok');
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ let blob = await response.blob(); // 将响应转换为blob
|
|
|
+ const url = window.URL.createObjectURL(blob);
|
|
|
+ const a = document.createElement('a');
|
|
|
+ a.href = url;
|
|
|
+ if(fileName){
|
|
|
+ a.download = fileName;
|
|
|
+ }
|
|
|
+ document.body.appendChild(a);
|
|
|
+ a.click();
|
|
|
+ document.body.removeChild(a);
|
|
|
+ window.URL.revokeObjectURL(url);
|
|
|
+ }catch(error){
|
|
|
+ console.error(error);
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 获取临时签名地址
|
|
|
+ async signatureUrl(url:string,options?:any):Promise<string|undefined>{
|
|
|
+ let expires = options?.expires || 600 // 默认10分钟
|
|
|
await this.initClient();
|
|
|
- // const r1 = await this.client?.get(fname, file, putOptions);
|
|
|
+ let urlObj = new URL(url)
|
|
|
+ // let r1 = await this.client?.get(fname, file, putOptions);
|
|
|
+ let r1 = await this.client?.signatureUrl(urlObj.pathname, {
|
|
|
+ expires: expires,
|
|
|
+ });
|
|
|
+ console.log(r1)
|
|
|
+ return r1;
|
|
|
|
|
|
}
|
|
|
|