Example #1
0
        private UploadFileOutDto UpOss(FileStream stream)
        {
            try
            {
                UploadFileOutDto dto = new UploadFileOutDto();
                if (stream == null || stream.Length <= 0)
                {
                    throw new Exception("文件不能为空!");
                }

                dto.FileName = GenerateFileName(stream.Name);

                //获取该文件的输入流
                using (var fs = stream)
                {
                    OssClient ossClient = OssManager.GetInstance(_model.AccessId, _model.AccessKey, _model.EndPoint);
                    ossClient.SetBucketAcl(_model.BucketName, CannedAccessControlList.PublicRead);
                    //将该文件流保存到OSS中
                    var ret = ossClient.PutObject(_model.BucketName, dto.FileName, fs);
                    var uri = ossClient.GeneratePresignedUri(_model.BucketName, dto.FileName);
                    dto.FilePath = $"{uri.Scheme}://{uri.Host}/{dto.FileName}";
                }

                return(dto);
            }
            catch (OssException oe)
            {
                throw new Exception($"调用OSS上传时,访问服务器端时出错。错误码Code:{oe.ErrorCode},错误信息Message:{oe.Message}");
            }
        }
Example #2
0
        /// <summary>
        /// 获取文件oss地址
        /// </summary>
        /// <param name="fileKey"></param>
        /// <returns></returns>
        public string GetFilePath(string fileKey)
        {
            OssClient ossClient = OssManager.GetInstance(_model.AccessId, _model.AccessKey, _model.EndPoint);
            var       uri       = ossClient.GeneratePresignedUri(_model.BucketName, fileKey);

            return($"{uri.Scheme}://{uri.Host}/{fileKey}");
        }