public static Stream Download(string cloudName) { try { Debug.WriteLine(string.Format("/----- start download:{0} from cloud-----/", cloudName)); GetObjectRequest req = new GetObjectRequest(bucket, cloudName); OssObject obj = ossClient.GetObject(req); Stream result = null; if (obj != null) { result = obj.Content; } return result; } catch (Exception ex) { MessageBox.Show(string.Format("还原失败:{0}", ex.Message)); } return null; }
public void _DownLoadFile(object value) { OssObject ossobject = null; GetObjectRequest request = null; ObjectMetadata metadata; FileStream fs = null; int readline = 0; //当前读取的字节长度 long countlength = 0; //已经读取的字节 byte[] buffer = new byte[length]; //文件块 string bucket = "", fileKey = "", targetPath = ""; if (value is string[]) { bucket = ((string[])value)[0]; fileKey = ((string[])value)[1]; targetPath = ((string[])value)[2]; } else { return; } if (File.Exists(targetPath)) { try { File.Delete(targetPath); } catch (IOException ioEx) //文件正在使用中,不能删除。 { return; } } fs = new FileStream(targetPath, FileMode.Create); try { request = new Aliyun.OpenServices.OpenStorageService.GetObjectRequest(bucket, fileKey); ossobject = client.GetObject(request);//获取文件流 //获取需要下载文件的信息。 metadata = client.GetObjectMetadata(bucket, fileKey); while ((readline = ossobject.Content.Read(buffer, 0, length)) > 0) { fs.Write(buffer, 0, readline); countlength += readline; if (DownLoadProcessing != null) { DownLoadProcessing(Convert.ToDouble(countlength) / Convert.ToDouble(metadata.ContentLength)); } } } catch (Aliyun.OpenServices.ServiceException serviceEx) //下载过程中出现错误。 { } finally { ossobject.Dispose(); fs.Close(); } }
public ActionResult GetFile(int id, int width = 0, int height = 0) { int bfid = id; BLLBFInfo bllbfinfo = new BLLBFInfo(); var bfinfo = bllbfinfo.Find(bfid); if (bfinfo == null) { return Content("不存在的数据"); } else { bfinfo.LastViewDateTime = DateTime.Now; bfinfo.GetCount++; bllbfinfo.UpDate(bfinfo); //返回一个二进制流!根据类型,来判断,需要返回的内容! MemoryStream ms = new MemoryStream(); OssClient oc = new OssClient(AccessKeyID, AccessKeySecret); GetObjectRequest getObjectRequest = new GetObjectRequest(BucketName, bfinfo.OSSFileName); oc.GetObject(getObjectRequest, ms); if (bfinfo.MineType.Contains("image")) { if (width == 0 && height == 0) { return File(ms.ToArray(), bfinfo.MineType); } else { Image image = System.Drawing.Image.FromStream(ms); return File(GetThumbnail(image, width, height).ToArray(), bfinfo.MineType); } } else { return File(ms.ToArray(), bfinfo.MineType); } } }
public ObjectMetadata GetObject(AliyunDownloadRequst downloadRequst) { ObjectMetadata ret = null; using (FileStream fs = new FileStream(downloadRequst.file, FileMode.Create)) { long totalBytes = GetObjectSize(downloadRequst.bucketName, downloadRequst.key); AliyunStream stream = new AliyunStream(fs); stream.WritePositionChangeEvent += delegate(long nowPosition, long _totalBytes) { downloadRequst.TransferFile(nowPosition, totalBytes); }; GetObjectRequest getObjectRequest = new GetObjectRequest(downloadRequst.bucketName, downloadRequst.key); ret = _ossClient.GetObject(getObjectRequest, stream); } return ret; }