配置信息,主要包括Zone配置(另请参阅Zone模块) 目前已支持的机房包括: 华东(CN_East), 华北(CN_North), 华南(CN_South), 北美(US_North), 新加坡(AS_Singapore) 默认设置为华东机房(CN_East)
        /// <summary>
        /// 得到七牛配置文件
        /// </summary>
        /// <returns></returns>
        public Qiniu.Storage.Config GetConfig(UploadPersistentOps uploadPersistentOps = null)
        {
            var config = new Qiniu.Storage.Config()
            {
                Zone = QiNiuConfig.GetZone(),
            };

            if (uploadPersistentOps != null)
            {
                config.UseHttps = uploadPersistentOps.IsUseHttps;

                config.ChunkSize = Get(uploadPersistentOps.ChunkUnit);
                if (uploadPersistentOps.MaxRetryTimes != -1)
                {
                    config.MaxRetryTimes = uploadPersistentOps.MaxRetryTimes;
                }

                if (uploadPersistentOps.MaxRetryTimes != -1)
                {
                    config.MaxRetryTimes = uploadPersistentOps.MaxRetryTimes;
                }
            }

            ChunkUnit Get(ChunkUnitEnum chunkUnit)
            {
                int chunkUnits = (int)chunkUnit;

                return((ChunkUnit)chunkUnits);
            }

            return(config);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 图片上传到七牛
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        private static ItemResult <string> UploadQiNiu(string filename, byte[] data)
        {
            ItemResult <string> res = new ItemResult <string>();
            Mac mac = new Mac(ApplicationContext.AppSetting.QiNiu_AccessKey, ApplicationContext.AppSetting.QiNiu_SecretKey);
            // 设置上传策略,详见:https://developer.qiniu.com/kodo/manual/1206/put-policy
            PutPolicy putPolicy = new PutPolicy();

            // 设置要上传的目标空间
            putPolicy.Scope = ApplicationContext.AppSetting.QiNiu_Bucket;
            // 上传策略的过期时间(单位:秒)
            putPolicy.SetExpires(3600);
            // 文件上传完毕后,在多少天后自动被删除
            putPolicy.DeleteAfterDays = 1;
            // 生成上传token
            string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());

            Qiniu.Storage.Config config = new Qiniu.Storage.Config();
            // 设置上传区域
            config.Zone = Zone.ZONE_CN_East;
            // 设置 http 或者 https 上传
            config.UseHttps      = true;
            config.UseCdnDomains = true;
            config.ChunkSize     = ChunkUnit.U512K;
            // 表单上传
            FormUploader target = new FormUploader(config);

            Qiniu.Http.HttpResult result = target.UploadData(data, filename, token, null);
            res.Data    = JsonConvert.DeserializeObject <QiniuOss>(result.Text).key;
            res.Success = true;
            return(res);
        }
        /// <summary>
        /// 得到七牛配置文件
        /// </summary>
        /// <returns></returns>
        internal Qiniu.Storage.Config GetConfig(UploadPersistentOps uploadPersistentOps = null)
        {
            var config = new Qiniu.Storage.Config()
            {
                Zone = _qiNiuConfig.GetZone(),
            };

            if (uploadPersistentOps != null)
            {
                config.UseHttps = uploadPersistentOps.IsUseHttps;

                config.ChunkSize = Get(uploadPersistentOps.ChunkUnit);
                if (uploadPersistentOps.MaxRetryTimes != -1)
                {
                    config.MaxRetryTimes = uploadPersistentOps.MaxRetryTimes;
                }

                if (uploadPersistentOps.MaxRetryTimes != -1)
                {
                    config.MaxRetryTimes = uploadPersistentOps.MaxRetryTimes;
                }
            }

            Qiniu.Storage.ChunkUnit Get(
                EInfrastructure.Core.Configuration.Ioc.Plugs.Storage.Enumerations.ChunkUnit chunkUnit)
            {
                int chunkUnits = chunkUnit.Id;

                return((Qiniu.Storage.ChunkUnit)chunkUnits);
            }

            return(config);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 上传到七牛
        /// </summary>
        /// <param name="myStream"></param>
        /// <param name="filename"></param>
        /// <param name="servePath">上传成功的路径</param>
        /// <returns></returns>
        public static HttpResult UploadFile(Stream myStream, string filename, out string servePath)
        {
            //code为200成功
            HttpResult result = new HttpResult()
            {
                Code = 1
            };

            servePath = "";
            try
            {
                // 上传文件名
                servePath = $"{Guid.NewGuid().ToString().Replace("-", "")}{filename}";
                Qiniu.Storage.Config config = new Qiniu.Storage.Config();
                // 设置上传区域
                config.Zone = Zone.ZONE_CN_South;
                // 设置 http 或者 https 上传
                config.UseHttps      = true;
                config.UseCdnDomains = true;
                config.ChunkSize     = ChunkUnit.U512K;
                ResumableUploader target = new ResumableUploader(config);
                PutExtra          extra  = new PutExtra();
                string            token  = GetToken();
                result = target.UploadStream(myStream, servePath, token, extra);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex, "QiniutokenHelper/UploadFile");
            }
            return(result);
        }
Ejemplo n.º 5
0
        public string UploadImage(string filePath)
        {
            Mac mac = new Mac("TYb8ZurNoN-xrUOhnNom_q3ZdPl1OLJqUsvoP0xB", "d7ajdjoitbUiJZQY7ANw5bSf3p6K3nQA8KkGxIDq");
            // 上传文件名
            string key = "putty2.gif";
            // 本地文件路径
            //string filePath = @"E:\MY\FastDevStu\Mvc\Models\微信图片_20190508193805.gif";
            // 空间名
            string Bucket = "cjj81pic";
            // 设置上传策略,详见:https://developer.qiniu.com/kodo/manual/1206/put-policy
            PutPolicy putPolicy = new PutPolicy();

            putPolicy.Scope = Bucket + ":" + key;
            putPolicy.SetExpires(3600);
            string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());

            Qiniu.Storage.Config config = new Qiniu.Storage.Config();
            // 设置上传区域
            config.Zone = Zone.ZONE_CN_South;
            // 设置 http 或者 https 上传
            config.UseHttps      = true;
            config.UseCdnDomains = true;
            config.ChunkSize     = ChunkUnit.U512K;
            ResumableUploader target = new ResumableUploader(config);
            PutExtra          extra  = new PutExtra();

            //设置断点续传进度记录文件
            extra.ResumeRecordFile = ResumeHelper.GetDefaultRecordKey(filePath, key);
            Console.WriteLine("record file:" + extra.ResumeRecordFile);
            extra.ResumeRecordFile = "test.progress";
            HttpResult result = target.UploadFile(filePath, key, token, extra);

            Console.WriteLine("resume upload: " + result.ToString());
            return(result.ToString());
        }
Ejemplo n.º 6
0
        public ItemResult <string> ImageToQiNiu(string key, byte[] data)
        {
            ItemResult <string> res = new ItemResult <string>();
            Mac mac = new Mac(AccessKey, SecretKey);
            // 上传文件名
            //   string key = "12312312312313123213";
            // 本地文件路径
            string filePath = "D:\\tools\\putty.exe";

            // 设置上传策略,详见:https://developer.qiniu.com/kodo/manual/1206/put-policy
            PutPolicy putPolicy = new PutPolicy();

            // 设置要上传的目标空间
            putPolicy.Scope = Bucket;
            // 上传策略的过期时间(单位:秒)
            putPolicy.SetExpires(3600);
            // 文件上传完毕后,在多少天后自动被删除
            putPolicy.DeleteAfterDays = 1;
            // 生成上传token
            string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());

            Qiniu.Storage.Config config = new Qiniu.Storage.Config();
            // 设置上传区域
            config.Zone = Zone.ZONE_CN_East;
            // 设置 http 或者 https 上传
            config.UseHttps      = true;
            config.UseCdnDomains = true;
            config.ChunkSize     = ChunkUnit.U512K;
            // 表单上传
            FormUploader target = new FormUploader(config);

            Qiniu.Http.HttpResult result = target.UploadData(data, key, token, null);
            res.Message = res.Message;
            return(res);
        }
Ejemplo n.º 7
0
    public FetchInfo FetchUpload(string targetUrl, string key)
    {
        var config = new Qiniu.Storage.Config {
            Zone = _zone
        };
        var bm     = new BucketManager(_mac, config);
        var result = bm.Fetch(targetUrl, _options.Bucket, key);

        if (result.Code != 200)
        {
            throw new HttpRequestException("自动存储图片失败!" + result.Text ?? result.RefText);
        }
        return(result.Result);
    }
Ejemplo n.º 8
0
    public override void Process()
    {
        string         uploadFileName = null;
        var            accessKey      = "填写在你七牛后台找到相应的accesskey";
        var            secretKey      = "填写在你七牛后台找到相应的secretkey";
        HttpPostedFile file           = Request.Files[UploadConfig.UploadFieldName];
        Stream         myStream       = file.InputStream;

        uploadFileName = file.FileName;

        if (!CheckFileType(uploadFileName))
        {
            Result.State = UploadState.TypeNotAllow;
            WriteResult();
            return;
        }
        if (!CheckFileSize(file.ContentLength))
        {
            Result.State = UploadState.SizeLimitExceed;
            WriteResult();
            return;
        }
        Mac mac = new Mac("TYb8ZurNoN-xrUOhnNom_q3ZdPl1OLJqUsvoP0xB", "d7ajdjoitbUiJZQY7ANw5bSf3p6K3nQA8KkGxIDq");
        // 上传文件名
        string key = $"{Guid.NewGuid().ToString().Replace("-","")}{uploadFileName}";
        // 本地文件路径
        //string filePath = @"E:\MY\FastDevStu\Mvc\Models\微信图片_20190508193805.gif";
        // 空间名
        string Bucket = "cjj81pic";
        // 设置上传策略,详见:https://developer.qiniu.com/kodo/manual/1206/put-policy
        PutPolicy putPolicy = new PutPolicy();

        putPolicy.Scope = Bucket + ":" + key;
        putPolicy.SetExpires(3600);
        string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());

        Qiniu.Storage.Config config = new Qiniu.Storage.Config();
        // 设置上传区域
        config.Zone = Zone.ZONE_CN_South;
        // 设置 http 或者 https 上传
        config.UseHttps      = true;
        config.UseCdnDomains = true;
        config.ChunkSize     = ChunkUnit.U512K;
        ResumableUploader target = new ResumableUploader(config);
        PutExtra          extra  = new PutExtra();

        //设置断点续传进度记录文件,,报文件没又访问权限
        //extra.ResumeRecordFile = ResumeHelper.GetDefaultRecordKey(uploadFileName, key);
        //Console.WriteLine("record file:" + extra.ResumeRecordFile);
        //extra.ResumeRecordFile = "test.progress";
        try
        {
            HttpResult result = target.UploadStream(myStream, key, token, extra);
            if (result.Code == 200)
            {
                this.Result.State = UploadState.Success;
                // this.Result.Url = $"http://ptdzsd6xm.bkt.clouddn.com{Path.DirectorySeparatorChar}{key}";
                this.Result.Url = key;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            WriteResult();
        }
    }
Ejemplo n.º 9
0
    public override void Process()
    {
        byte[] uploadFileBytes = null;
        string uploadFileName  = null;

        if (UploadConfig.Base64)
        {
            uploadFileName  = UploadConfig.Base64Filename;
            uploadFileBytes = Convert.FromBase64String(Request[UploadConfig.UploadFieldName]);
        }
        else
        {
            var file = Request.Files[UploadConfig.UploadFieldName];
            uploadFileName = file.FileName;

            if (!CheckFileType(uploadFileName))
            {
                Result.State = UploadState.TypeNotAllow;
                WriteResult();
                return;
            }
            if (!CheckFileSize(file.ContentLength))
            {
                Result.State = UploadState.SizeLimitExceed;
                WriteResult();
                return;
            }

            uploadFileBytes = new byte[file.ContentLength];
            try
            {
                file.InputStream.Read(uploadFileBytes, 0, file.ContentLength);
            }
            catch (Exception)
            {
                Result.State = UploadState.NetworkError;
                WriteResult();
            }
        }

        Result.OriginFileName = uploadFileName;

        //var savePath = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat);
        //var localPath = Server.MapPath(savePath);
        //try
        //{
        //    if (!Directory.Exists(Path.GetDirectoryName(localPath)))
        //    {
        //        Directory.CreateDirectory(Path.GetDirectoryName(localPath));
        //    }
        //    File.WriteAllBytes(localPath, uploadFileBytes);
        //    Result.Url = savePath;
        //    Result.State = UploadState.Success;
        //}
        //catch (Exception e)
        //{
        //    Result.State = UploadState.FileAccessError;
        //    Result.ErrorMessage = e.Message;
        //}
        //finally
        //{
        //    WriteResult();
        //}

        try
        {
            string AccessKey = "nDdaH3SGk5NXCyQ8eZMsnRTApJndj0fOKy_f6Lix";
            string SecretKey = "HgxcM58TEplyx6H9CBJTbYDqpF4uXAKsiBp-W6u4";
            Mac    mac       = new Mac(AccessKey, SecretKey);
            // 上传文件名

            string key = DateTime.Now.ToString("yyyy/MM/dd") + "/" + CommonHelper.CalcMd5(uploadFileBytes) + Path.GetExtension(uploadFileName);
            // 存储空间名
            string Bucket = "renthouse2019";
            // 设置上传策略,详见:https://developer.qiniu.com/kodo/manual/1206/put-policy
            PutPolicy putPolicy = new PutPolicy();
            // 设置要上传的目标空间
            putPolicy.Scope = Bucket;
            // 上传策略的过期时间(单位:秒)
            putPolicy.SetExpires(3600);
            // 文件上传完毕后,在多少天后自动被删除
            putPolicy.DeleteAfterDays = 1;
            // 生成上传token
            string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());
            Qiniu.Storage.Config qiniuConfig = new Qiniu.Storage.Config();
            // 设置上传区域
            qiniuConfig.Zone = Zone.ZONE_CN_South;
            // 设置 http 或者 https 上传
            qiniuConfig.UseHttps      = true;
            qiniuConfig.UseCdnDomains = true;
            qiniuConfig.ChunkSize     = ChunkUnit.U512K;
            // 表单上传
            FormUploader target = new FormUploader(qiniuConfig);
            HttpResult   result = target.UploadData(uploadFileBytes, key, token, null);
            Result.Url   = key;
            Result.State = UploadState.Success;
        }
        catch (Exception e)
        {
            Result.State        = UploadState.FileAccessError;
            Result.ErrorMessage = e.Message;
        }
        finally
        {
            WriteResult();
        }
    }