/** * 读取目录列表 * @param $path 目录路径 * return array 数组 或 null */ public async Task <UpYunResult <List <FolderItem> > > ReadDirAsync(string url, CancellationToken cancellationToken = default) { Dictionary <string, object> headers = new Dictionary <string, object>(); //headers["Accept"] = "application/json"; byte[] a = null; using (var resp = await NewWorkAsync("GET", DL + this.bucketname + url, a, headers, cancellationToken)) { if (resp.StatusCode == System.Net.HttpStatusCode.OK) { string strhtml = await resp.Content.ReadAsStringAsync(); strhtml = strhtml.Replace("\t", "\\"); strhtml = strhtml.Replace("\n", "\\"); string[] ss = strhtml.Split('\\', StringSplitOptions.RemoveEmptyEntries); int i = 0; List <FolderItem> list = new List <FolderItem>(); while (i < ss.Length) { FolderItem fi = new FolderItem(ss[i], ss[i + 1], int.Parse(ss[i + 2]), int.Parse(ss[i + 3])); list.Add(fi); i += 4; } return(list); } else { return(await UpYunResult <List <FolderItem> > .CreateErrorAsync(resp)); } } }
/** * 获取文件信息 * @param $file 文件路径(包含文件名) * return array('type'=> file | folder, 'size'=> file size, 'date'=> unix time) 或 null */ public async Task <UpYunResult <Dictionary <string, object> > > GetFileInfoAsync(string file, CancellationToken cancellationToken = default) { Dictionary <string, object> headers = new Dictionary <string, object>(); byte[] a = null; using (var resp = await NewWorkAsync("HEAD", DL + this.bucketname + file, a, headers, cancellationToken)) { if (resp.StatusCode == System.Net.HttpStatusCode.OK) { Dictionary <string, object> respHeaders = new Dictionary <string, object>(); if (tmp_infos.ContainsKey("x-upyun-file-type") && tmp_infos.ContainsKey("x-upyun-file-size") && tmp_infos.ContainsKey("x-upyun-file-date")) { respHeaders.Add("type", tmp_infos["x-upyun-file-type"]); respHeaders.Add("size", tmp_infos["x-upyun-file-size"]); respHeaders.Add("date", tmp_infos["x-upyun-file-date"]); } return(respHeaders); } else { return(await UpYunResult <Dictionary <string, object> > .CreateErrorAsync(resp)); } } }
public static async Task <UpYunResult> CreateErrorAsync(HttpResponseMessage resp) { string body = await resp.Content.ReadAsStringAsync(); JsonSerializerOptions options = new JsonSerializerOptions(); options.PropertyNameCaseInsensitive = true; UpYunResult result = JsonSerializer.Deserialize <UpYunResult>(body, options); result.IsOK = false; return(result); }
private async Task <UpYunResult> DeleteAsync(string path, Dictionary <string, object> headers, CancellationToken cancellationToken = default) { var resp = await NewWorkAsync("DELETE", DL + this.bucketname + path, null, headers, cancellationToken); if (resp.StatusCode == System.Net.HttpStatusCode.OK) { return(UpYunResult.OK); } else { return(await UpYunResult.CreateErrorAsync(resp)); } }
/// <summary> /// 移动文件 /// </summary> /// <param name="path">源路径</param> /// <param name="dest">目标路径</param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task <UpYunResult> MoveFileAsync(string path, string dest, CancellationToken cancellationToken = default) { Dictionary <string, object> headers = new Dictionary <string, object>(); headers["X-Upyun-Move-Source"] = DL + this.bucketname + Uri.EscapeUriString(path); var resp = await NewWorkAsync("PUT", DL + this.bucketname + Uri.EscapeUriString(dest), null, headers, cancellationToken); if (resp.StatusCode == System.Net.HttpStatusCode.OK) { return(UpYunResult.OK); } else { return(await UpYunResult.CreateErrorAsync(resp)); } }
/** * 上传文件 * @param $file 文件路径(包含文件名) * @param $datas 文件内容 或 文件IO数据流 * return true or false */ public async Task <UpYunResult> WriteFileAsync(string path, byte[] data, bool auto_mkdir, CancellationToken cancellationToken = default) { Dictionary <string, object> headers = new Dictionary <string, object>(); this.auto_mkdir = auto_mkdir; using (var resp = await NewWorkAsync("POST", DL + this.bucketname + path, data, headers, cancellationToken)) { if (resp.StatusCode == System.Net.HttpStatusCode.OK) { return(UpYunResult.OK); } else { return(await UpYunResult.CreateErrorAsync(resp)); } } }
/** * 读取文件 * @param $file 文件路径(包含文件名) * @param $output_file 可传递文件IO数据流(默认为 null,结果返回文件内容,如设置文件数据流,将返回 true or false) * return 文件内容 或 null */ public async Task <UpYunResult <byte[]> > ReadFileAsync(string path, CancellationToken cancellationToken = default) { Dictionary <string, object> headers = new Dictionary <string, object>(); byte[] a = null; using (var resp = await NewWorkAsync("GET", DL + this.bucketname + path, a, headers, cancellationToken)) { if (resp.StatusCode == System.Net.HttpStatusCode.OK) { return(await resp.Content.ReadAsByteArrayAsync()); } else { return(await UpYunResult <byte[]> .CreateErrorAsync(resp)); } } }
/** * 创建目录 * @param $path 目录路径 * return true or false */ public async Task <UpYunResult> MkDirAsync(string path, bool auto_mkdir, CancellationToken cancellationToken = default) { this.auto_mkdir = auto_mkdir; Dictionary <string, object> headers = new Dictionary <string, object>(); headers.Add("folder", "create"); using (var resp = await NewWorkAsync("POST", DL + this.bucketname + path, null, headers, cancellationToken)) { if (resp.StatusCode == System.Net.HttpStatusCode.OK) { return(UpYunResult.OK); } else { return(await UpYunResult.CreateErrorAsync(resp)); } } }
/** * 获取总体空间的占用信息 * return 空间占用量,失败返回 null */ public async Task <UpYunResult <long> > GetFolderUsageAsync(string url, CancellationToken cancellationToken = default) { Dictionary <string, object> headers = new Dictionary <string, object>(); using (var resp = await NewWorkAsync("GET", DL + this.bucketname + url + "?usage", null, headers, cancellationToken)) { if (resp.StatusCode == System.Net.HttpStatusCode.OK) { string strhtml = await resp.Content.ReadAsStringAsync(); long size = long.Parse(strhtml); return(size); } else { return(await UpYunResult <long> .CreateErrorAsync(resp)); } } }