コード例 #1
0
ファイル: SimpleUpload.cs プロジェクト: qiniu/csharp-sdk
        public static void uploadFile()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);

            string bucket = "BUCKET";
            string saveKey = "SAVE_KEY";
            string localFile = "LOCAL_FILE";

            PutPolicy putPolicy = new PutPolicy();
            putPolicy.Scope = bucket;
            putPolicy.SetExpires(3600);
            putPolicy.DeleteAfterDays = 1;
            string token = Auth.createUploadToken(putPolicy, mac);

            UploadOptions uploadOptions = null;

            // 上传完毕事件处理
            UpCompletionHandler uploadCompleted = new UpCompletionHandler(OnUploadCompleted);

            // 方式1:使用UploadManager
            //默认设置 Qiniu.Common.Config.PUT_THRESHOLD = 512*1024;
            //可以适当修改,UploadManager会根据这个阈值自动选择是否使用分片(Resumable)上传
            UploadManager um = new UploadManager();
            um.uploadFile(localFile, saveKey, token, uploadOptions, uploadCompleted);

            // 方式2:使用FormManager
            //FormUploader fm = new FormUploader();
            //fm.uploadFile(localFile, saveKey, token, uploadOptions, uploadCompleted);
        }
コード例 #2
0
ファイル: Pfop.cs プロジェクト: satng/qiniu-csharp-sdk
 public Pfop(Mac mac, string bucket, string key, string fops)
 {
     this.httpManager = new HttpManager();
     this.Mac = mac;
     this.Bucket = bucket;
     this.Key = key;
     this.Fops = fops;
 }
コード例 #3
0
ファイル: FusionManagerTest.cs プロジェクト: qiniu/csharp-sdk
        public void fusionMgrTest()
        {
            //Settings.load();
            Settings.LoadFromFile();
            string testResUrl = "http://test.fengyh.cn/qiniu/files/hello.txt";
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);
            FusionManager target = new FusionManager(mac);

            //
        }
コード例 #4
0
ファイル: FusionDemo.cs プロジェクト: qiniu/csharp-sdk
        /// <summary>
        /// 文件预取
        /// </summary>
        public static void prefetch()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);
            FusionManager fusionMgr = new FusionManager(mac);

            string[] urls = new string[] { "URL1", "URL2" };
            PrefetchRequest request = new PrefetchRequest(urls);
            PrefetchResult result = fusionMgr.Prefetch(request);
            Console.WriteLine(result);
        }
コード例 #5
0
ファイル: FusionDemo.cs プロジェクト: qiniu/csharp-sdk
        /// <summary>
        /// 日志查询
        /// </summary>
        public static void loglist()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);
            FusionManager fusionMgr = new FusionManager(mac);

            LogListRequest request = new LogListRequest();
            request.Day = "DAY"; // "2016-09-01"
            request.Domains = "DOMAIN1"; // domains
            LogListResult result = fusionMgr.LogList(request);
            Console.WriteLine(result);
        }
コード例 #6
0
        /// <summary>
        /// 修改文件的MIME_TYPE
        /// </summary>
        public static void chgm()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);

            string bucket = "BUCKET";
            string key = "KEY";
            string mimeType = "MIME_TYPE";

            BucketManager bm = new BucketManager(mac);
            HttpResult result = bm.chgm(bucket, key, mimeType);
        }
コード例 #7
0
        /// <summary>
        /// 列举所有的bucket
        /// </summary>
        public static void buckets()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);
            BucketManager bm = new BucketManager(mac);
            BucketsResult result = bm.buckets();

            foreach(string bucket in result.Buckets)
            {
                System.Console.WriteLine(bucket);
            }
        }
コード例 #8
0
        public void resumeUploadTest()
        {
            //Settings.load();
            Settings.LoadFromFile();
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);

            ResumeRecorder recorder = new ResumeRecorder("dir");
            string token = "<token>";
            ResumeUploader target = new ResumeUploader(recorder, "big.record", "F:\\big.dat", "big_ResumeUpload.dat", token, null, null);
            target.uploadFile();
        }
コード例 #9
0
ファイル: FusionDemo.cs プロジェクト: qiniu/csharp-sdk
        /// <summary>
        /// 缓存刷新
        /// </summary>
        public static void refresh()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);
            FusionManager fusionMgr = new FusionManager(mac);

            string[] urls = new string[] { "URL1", "URL2" };
            string[] dirs = new string[] { "DIR1", "DIR2" };
            RefreshRequest request = new RefreshRequest();
            request.AddUrls(urls);
            request.AddDirs(dirs);
            RefreshResult result = fusionMgr.Refresh(request);
            Console.WriteLine(result);
        }
コード例 #10
0
ファイル: FusionDemo.cs プロジェクト: qiniu/csharp-sdk
        /// <summary>
        /// 流量
        /// </summary>
        public static void flux()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);
            FusionManager fusionMgr = new FusionManager(mac);

            FluxRequest request = new FluxRequest();
            request.StartDate = "START_DATE"; // "2016-09-01"
            request.EndDate = "END_DATE"; // "2016-09-20"
            request.Granularity = "GRANU"; // "day"
            request.Domains = "DOMAIN1;DOMAIN2"; // domains
            FluxResult result = fusionMgr.Flux(request);
            Console.WriteLine(result);
        }
コード例 #11
0
        /// <summary>
        /// 复制文件
        /// </summary>
        public static void copy()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);

            string srcBucket = "SRC_BUCKET";
            string srcKey = "SRC_KEY";
            string dstBucket = "SRC_BUCKET";
            string dstKey = "DST_BUCKET";

            BucketManager bm = new BucketManager(mac);
            HttpResult result = bm.copy(srcBucket, srcKey, dstBucket, dstKey);

            //支持force参数, bool force = true/false
            //HttpResult result = bm.copy(srcBucket, srcKey, dstBucket, dstKey, force);
        }
コード例 #12
0
        /// <summary>
        /// 批量操作
        /// </summary>
        public static void batch()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);

            // 批量操作类似于
            // op=<op1>&op=<op2>&op=<op3>...
            string batchOps = "BATCH_OPS";
            BucketManager bm = new BucketManager(mac);
            HttpResult result = bm.batch(batchOps);
            // 或者
            //string[] batch_ops={"<op1>","<op2>","<op3>",...};
            //bm.batch(batch_ops);

            System.Console.WriteLine(result.Response);
        }
コード例 #13
0
ファイル: DfopDemo.cs プロジェクト: qiniu/csharp-sdk
        /// <summary>
        /// dfop形式1:URL
        /// </summary>
        public static void dfopUrl()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);
            Dfop dfx = new Dfop(mac);

            string fop = "FOP"; // E.G.: "imageInfo"
            string url = "RES_URL";

            var ret = dfx.dfop(fop, url);

            if (ret.ResponseInfo.StatusCode != 200)
            {
                Console.WriteLine(ret.ResponseInfo);
            }
            Console.WriteLine(ret.Response);
        }
コード例 #14
0
ファイル: DfopDemo.cs プロジェクト: qiniu/csharp-sdk
        /// <summary>
        /// dfop形式2:Data
        /// </summary>
        public static void dfopData()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);
            Dfop dfx = new Dfop(mac);

            string fop = "FOP";
            byte[] data = System.IO.File.ReadAllBytes("LOCAL_FILE");

            var ret = dfx.dfop(fop, data);

            if (ret.ResponseInfo.StatusCode != 200)
            {
                Console.WriteLine(ret.ResponseInfo);
            }
            Console.WriteLine(ret.Response);
        }
コード例 #15
0
ファイル: SimpleDownload.cs プロジェクト: qiniu/csharp-sdk
        public static void downloadFile()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);

            // 加上过期参数,使用?e=<UnixTimestamp>
            // 如果rawUrl中已包含?,则改用&e=<UnixTimestamp>
            string rawUrl = "RAW_URL";
            string expireAt = "UNIX_TIMESTAMP";
            string mid = "?e=";
            if(rawUrl.Contains("?"))
            {
                mid = "&e=";
            }
            string token = Auth.createDownloadToken(rawUrl + mid + expireAt, mac);
            string accUrl = rawUrl + mid + expireAt + "&token=" + token;
            // 接下来可以使用accUrl来下载文件

            System.Console.WriteLine(accUrl);
        }
コード例 #16
0
ファイル: ResumableUpload.cs プロジェクト: qiniu/csharp-sdk
        public static void uploadBigFile()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);

            string bucket = "BUCKET";
            string saveKey = "SAVE_KEY";
            string localFile = "LOCAL_FILE";
            string recordPath = "RECORD_PATH";
            string recordFile = "RECORD_FILE";

            PutPolicy putPolicy = new PutPolicy();
            putPolicy.Scope = bucket;
            putPolicy.SetExpires(3600);
            putPolicy.DeleteAfterDays = 1;

            string token = Auth.createUploadToken(putPolicy, mac);

            ResumeRecorder rr = new ResumeRecorder(recordPath);

            UploadOptions uploadOptions = new UploadOptions(
                null, // ExtraParams
                null, // MimeType
                false,  // CheckCrc32
                new UpProgressHandler(OnUploadProgressChanged), // 上传进度
                null // CancelSignal
                );

            UpCompletionHandler uploadCompleted = new UpCompletionHandler(OnUploadCompleted); // 上传完毕

            ResumeUploader ru = new ResumeUploader(
                rr,               // 续传记录
                recordFile,       // 续传记录文件
                localFile,        // 待上传的本地文件
                saveKey,          // 要保存的文件名
                token,            // 上传凭证
                uploadOptions,    // 上传选项(其中包含进度处理),可为null
                uploadCompleted   // 上传完毕事件处理
                );

            ru.uploadFile();
        }
コード例 #17
0
ファイル: FormUploaderTest.cs プロジェクト: qiniu/csharp-sdk
        public void uploadDataTest()
        {
            //Settings.load();
            Settings.LoadFromFile("F:\\test.cfg");
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);
            FormUploader target = new FormUploader();
            byte[] data = Encoding.UTF8.GetBytes("hello world");
            string key = "test_FormUploaderUploadData.txt";

            PutPolicy putPolicy = new PutPolicy();
            putPolicy.Scope = Settings.Bucket;
            putPolicy.SetExpires(3600);
            putPolicy.DeleteAfterDays = 1;
            string token = Auth.createUploadToken(putPolicy, mac);
            UploadOptions uploadOptions = null;
            UpCompletionHandler upCompletionHandler = new UpCompletionHandler(delegate(string fileKey, ResponseInfo respInfo, string response)
            {
                Assert.AreEqual(200, respInfo.StatusCode);
            });
            target.uploadData(data, key, token, uploadOptions, upCompletionHandler);
        }
コード例 #18
0
ファイル: BucketManagerTest.cs プロジェクト: qiniu/csharp-sdk
        public void bktMgrTest()
        {
            //Settings.load();
            Settings.LoadFromFile();
            string testResUrl = "http://test.fengyh.cn/qiniu/files/hello.txt";
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);
            BucketManager target = new BucketManager(mac);

            target.fetch(testResUrl, Settings.Bucket, "test_BucketManager.txt");

            target.stat(Settings.Bucket, "test_BucketManager.txt");

            target.copy(Settings.Bucket, "test_BucketManager.txt", Settings.Bucket, "copy_BucketManager.txt", true);

            target.move(Settings.Bucket, "copy_BucketManager.txt", Settings.Bucket, "move_BucketManager.txt", true);

            target.delete(Settings.Bucket, "test_BucketManager.txt");

            DomainsResult domainsResult = target.domains(Settings.Bucket);

            BucketsResult bucketsResult = target.buckets();
        }
コード例 #19
0
ファイル: UploadManagerTest.cs プロジェクト: qiniu/csharp-sdk
        public void uploadFileTest()
        {
            //Settings.load();
            Settings.LoadFromFile();
            UploadManager target = new UploadManager();
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);
            string key = "test_UploadManagerUploadFile.png";
            string filePath = "F:\\test.png";

            PutPolicy putPolicy = new PutPolicy();
            putPolicy.Scope = Settings.Bucket;
            putPolicy.SetExpires(3600);
            putPolicy.DeleteAfterDays = 1;
            string token = Auth.createUploadToken(putPolicy, mac);
            UploadOptions uploadOptions = null;

            UpCompletionHandler upCompletionHandler = new UpCompletionHandler(delegate (string fileKey, ResponseInfo respInfo, string response)
            {
                Assert.AreEqual(200, respInfo.StatusCode);
            });
            target.uploadFile(filePath, key, token, uploadOptions, upCompletionHandler);
        }
コード例 #20
0
ファイル: UploadWithFop.cs プロジェクト: qiniu/csharp-sdk
        public static void uploadWithFop()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);

            string bucket = "BUCKET";
            string saveKey = "SAVE_KEY";
            string localFile = "LOCAL_FILE";

            // 如果想要将处理结果保存到SAVEAS_BUCKET空间下,文件名为SAVEAS_KEY
            // 可以使用savas参数 <FOPS>|saveas/<encodedUri>
            // encodedUri = StringUtils.urlSafeBase64Encode("SAVEAS_BUCKET" + ":" + "SAVEAS_KEY");
            string fops = "FOPS";

            PutPolicy putPolicy = new PutPolicy();
            putPolicy.Scope = bucket;
            putPolicy.PersistentOps = fops;
            putPolicy.SetExpires(3600);
            putPolicy.DeleteAfterDays = 1;
            string token = Auth.createUploadToken(putPolicy, mac);

            FormUploader fm = new FormUploader();
            fm.uploadFile(localFile, saveKey, token, null, null);
        }
コード例 #21
0
ファイル: PfopWithPipeline.cs プロジェクト: qiniu/csharp-sdk
        public static void pfopAndSave()
        {
            string bucket = "BUCKET";
            string key = "FILE";
            string pipeline = "MEDIAPROC_PIPELINE";
            string notifyUrl = "NOTIFY_URL";
            bool   force = false;

            string saveAsUri = StringUtils.urlSafeBase64Encode("<SAVEAS_BUCKET>:<SAVEAS_KEY>");
            string fops = "<FOPS>" + "|saveas/" + saveAsUri;

            Mac mac = new Mac(Settings.AccessKey,Settings.SecretKey);
            Pfop px = new Pfop(mac);
            PfopResult result = px.pfop(bucket, key, fops, pipeline, notifyUrl, force);

            System.Console.WriteLine(result.Response);

            // �Ժ���Ը���PersistentId��ѯ�������/���
            string persistentId = result.PersistentId;
            Prefop pz = new Prefop(persistentId);
            PrefopResult zr = pz.prefop();
            System.Console.WriteLine(zr.Response);
        }
コード例 #22
0
ファイル: Signature.cs プロジェクト: YuChenDayCode/KnowLedge
 /// <summary>
 /// 初始化
 /// </summary>
 /// <param name="mac">账号(密钥)</param>
 public Signature(Mac mac)
 {
     this.mac = mac;
 }
コード例 #23
0
ファイル: FileUploader.cs プロジェクト: qiniu/QSunSync
        public void uploadFile(object obj)
        {
            FileItem item = obj as FileItem;

            if (syncProgressPage.checkCancelSignal())
            {
                this.doneEvent.Set();
                return;
            }
            string fileFullPath = item.LocalFile;
            if (!File.Exists(fileFullPath))
            {
                Log.Error(string.Format("file not found error, {0}", fileFullPath));
                this.doneEvent.Set();
                return;
            }

            //set upload params
            Qiniu.Common.Config.PUT_THRESHOLD = this.syncSetting.ChunkUploadThreshold;
            Qiniu.Common.Config.CHUNK_SIZE = this.syncSetting.DefaultChunkSize;

            string myDocPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string recordPath = System.IO.Path.Combine(myDocPath, "qsunsync", "resume");
            if (!Directory.Exists(recordPath))
            {
                Directory.CreateDirectory(recordPath);
            }

            Mac mac = new Mac(SystemConfig.ACCESS_KEY, SystemConfig.SECRET_KEY);

            //current file info
            FileInfo fileInfo = new FileInfo(fileFullPath);
            long fileLength = fileInfo.Length;
            string fileLastModified = fileInfo.LastWriteTimeUtc.ToFileTime().ToString();
            //support resume upload
            string recorderKey = string.Format("{0}:{1}:{2}:{3}:{4}", this.syncSetting.LocalDirectory,
                this.syncSetting.TargetBucket, item.SaveKey, fileFullPath, fileLastModified);
            recorderKey = Tools.md5Hash(recorderKey);

            this.syncProgressPage.updateUploadLog("准备上传文件 " + fileFullPath);
            UploadManager uploadManger = new UploadManager(new Qiniu.Storage.Persistent.ResumeRecorder(recordPath),
                new Qiniu.Storage.Persistent.KeyGenerator(delegate() { return recorderKey; }));
            PutPolicy putPolicy = new PutPolicy();
            if (this.syncSetting.OverwriteDuplicate)
            {
                putPolicy.Scope = this.syncSetting.TargetBucket + ":" + item.SaveKey;
            }
            else
            {
                putPolicy.Scope = this.syncSetting.TargetBucket;
            }
            putPolicy.SetExpires(24 * 30 * 3600);
            string uptoken = Auth.createUploadToken(putPolicy, mac);

            this.syncProgressPage.updateUploadLog("开始上传文件 " + fileFullPath);
            uploadManger.uploadFile(fileFullPath, item.SaveKey, uptoken, new UploadOptions(null, null, false,
                new UpProgressHandler(delegate(string key, double percent)
                {
                    this.syncProgressPage.updateSingleFileProgress(taskId, fileFullPath, item.SaveKey, fileLength, percent);
                }), new UpCancellationSignal(delegate()
                {
                    return this.syncProgressPage.checkCancelSignal();
                }))
                , new UpCompletionHandler(delegate(string key, ResponseInfo respInfo, string response)
                {
                    if (respInfo.StatusCode != 200)
                    {
                        this.syncProgressPage.updateUploadLog("上传失败 " + fileFullPath + "," + respInfo.Error);
                        this.syncProgressPage.addFileUploadErrorLog(string.Format("{0}\t{1}\t{2}\t{3}", this.syncSetting.TargetBucket,
                                fileFullPath, item.SaveKey, respInfo.Error + "" + response));

                        //file exists error
                        if (respInfo.StatusCode == 614)
                        {
                            this.syncProgressPage.updateUploadLog("空间已存在,未覆盖 " + fileFullPath);
                            this.syncProgressPage.addFileNotOverwriteLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.TargetBucket,
                                fileFullPath, item.SaveKey));
                        }
                    }
                    else
                    {
                        // 写入记录
                        CachedHash.InsertOrUpdateCachedHash(item.LocalFile, item.FileHash, "", localHashDB);

                        //insert or update sync log for file
                        try
                        {
                            SyncLog.InsertOrUpdateSyncLog(item.SaveKey, fileFullPath, fileLastModified, this.syncLogDB);
                        }
                        catch (Exception ex)
                        {
                            Log.Error(string.Format("insert ot update sync log error {0}", ex.Message));
                        }

                        //update
                        if(this.syncProgressPage.isOverwritten(item.LocalFile))
                        {
                            // 如果该文件覆盖了云端原始文件
                            this.syncProgressPage.addFileOverwriteLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.TargetBucket, item.LocalFile, item.SaveKey));
                            this.syncProgressPage.updateUploadLog("空间已存在相同文件,强制覆盖 " + item.LocalFile);
                        }
                        this.syncProgressPage.updateUploadLog("上传成功 " + fileFullPath);
                        this.syncProgressPage.addFileUploadSuccessLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.TargetBucket,
                                fileFullPath, item.SaveKey));
                        this.syncProgressPage.updateTotalUploadProgress();
                    }

                    this.doneEvent.Set();
                }));
        }
コード例 #24
0
ファイル: FileUploader.cs プロジェクト: aleen42/QSunSync
        public void uploadFile(object file)
        {
            if (syncProgressPage.checkCancelSignal())
            {
                this.doneEvent.Set();
                return;
            }
            string fileFullPath = file.ToString();
            if (!File.Exists(fileFullPath))
            {
                Log.Error(string.Format("file not found error, {0}", fileFullPath));
                this.doneEvent.Set();
                return;
            }
            //check skipped rules
            int fileRelativePathIndex = fileFullPath.IndexOf(this.syncSetting.SyncLocalDir);
            string fileRelativePath = fileFullPath.Substring(fileRelativePathIndex + this.syncSetting.SyncLocalDir.Length);
            if (fileRelativePath.StartsWith("\\"))
            {
                fileRelativePath = fileRelativePath.Substring(1);
            }

            string[] skippedPrefixes = this.syncSetting.SkipPrefixes.Split(',');

            foreach (string prefix in skippedPrefixes)
            {
                if (!string.IsNullOrWhiteSpace(prefix))
                {
                    if (fileRelativePath.StartsWith(prefix.Trim()))
                    {
                        //skip by prefix
                        this.syncProgressPage.addFileSkippedLog(string.Format("{0}\t{1}", this.syncSetting.SyncTargetBucket,
                                fileFullPath));
                        this.syncProgressPage.updateUploadLog("按照前缀规则跳过文件不同步 " + fileFullPath);
                        this.syncProgressPage.updateTotalUploadProgress();
                        this.doneEvent.Set();
                        return;
                    }
                }
            }

            string[] skippedSuffixes = this.syncSetting.SkipSuffixes.Split(',');
            foreach (string suffix in skippedSuffixes)
            {
                if (!string.IsNullOrWhiteSpace(suffix))
                {
                    if (fileRelativePath.EndsWith(suffix.Trim()))
                    {
                        //skip by suffix
                        this.syncProgressPage.addFileSkippedLog(string.Format("{0}\t{1}", this.syncSetting.SyncTargetBucket,
                               fileFullPath));
                        this.syncProgressPage.updateUploadLog("按照后缀规则跳过文件不同步 " + fileFullPath);
                        this.syncProgressPage.updateTotalUploadProgress();
                        this.doneEvent.Set();
                        return;
                    }
                }
            }

            //generate the file key
            string fileKey = "";
            if (this.syncSetting.IgnoreDir)
            {
                //check ignore dir
                fileKey = System.IO.Path.GetFileName(fileFullPath);
            }
            else
            {
                string newFileFullPath = fileFullPath.Replace('\\', '/');
                string newLocalSyncDir = this.syncSetting.SyncLocalDir.Replace('\\', '/');
                int fileKeyIndex = newFileFullPath.IndexOf(newLocalSyncDir);
                fileKey = newFileFullPath.Substring(fileKeyIndex + newLocalSyncDir.Length);
                if (fileKey.StartsWith("/"))
                {
                    fileKey = fileKey.Substring(1);
                }
            }

            //add prefix
            fileKey = this.syncSetting.SyncPrefix + fileKey;

            //set upload params
            Qiniu.Common.Config.PUT_THRESHOLD = this.syncSetting.ChunkUploadThreshold;
            Qiniu.Common.Config.CHUNK_SIZE = this.syncSetting.DefaultChunkSize;

            string myDocPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string recordPath = System.IO.Path.Combine(myDocPath, "qsunsync", "resume");
            if (!Directory.Exists(recordPath))
            {
                Directory.CreateDirectory(recordPath);
            }

            bool overwriteUpload = false;
            Mac mac = new Mac(SystemConfig.ACCESS_KEY, SystemConfig.SECRET_KEY);

            //current file info
            FileInfo fileInfo = new FileInfo(fileFullPath);
            long fileLength = fileInfo.Length;
            string fileLastModified = fileInfo.LastWriteTimeUtc.ToFileTime().ToString();
            //support resume upload
            string recorderKey = string.Format("{0}:{1}:{2}:{3}:{4}", this.syncSetting.SyncLocalDir,
                this.syncSetting.SyncTargetBucket, fileKey, fileFullPath, fileLastModified);
            recorderKey = Tools.md5Hash(recorderKey);

            if (syncSetting.CheckRemoteDuplicate)
            {
                //check remotely
                BucketManager bucketManager = new BucketManager(mac);
                StatResult statResult = bucketManager.stat(this.syncSetting.SyncTargetBucket, fileKey);

                if (!string.IsNullOrEmpty(statResult.Hash))
                {
                    //file exists in bucket
                    string localHash = "";
                    //cached file info
                    try
                    {
                        CachedHash cachedHash = CachedHash.GetCachedHashByLocalPath(fileFullPath, localHashDB);
                        string cachedEtag = cachedHash.Etag;
                        string cachedLmd = cachedHash.LastModified;
                        if (!string.IsNullOrEmpty(cachedEtag) && !string.IsNullOrEmpty(cachedLmd))
                        {
                            if (cachedLmd.Equals(fileLastModified))
                            {
                                //file not modified
                                localHash = cachedEtag;
                            }
                            else
                            {
                                //file modified, calc the hash and update db
                                string newEtag = QETag.hash(fileFullPath);
                                localHash = newEtag;
                                try
                                {
                                    CachedHash.UpdateCachedHash(fileFullPath, newEtag, fileLastModified, localHashDB);
                                }
                                catch (Exception ex)
                                {
                                    Log.Error(string.Format("update local hash failed {0}", ex.Message));
                                }
                            }
                        }
                        else
                        {
                            //no record, calc hash and insert into db
                            string newEtag = QETag.hash(fileFullPath);
                            localHash = newEtag;
                            try
                            {
                                CachedHash.InsertCachedHash(fileFullPath, newEtag, fileLastModified, localHashDB);
                            }
                            catch (Exception ex)
                            {
                                Log.Error(string.Format("insert local hash failed {0}", ex.Message));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error(string.Format("get hash from local db failed {0}", ex.Message));
                        localHash = QETag.hash(fileFullPath);
                    }

                    if (localHash.Equals(statResult.Hash))
                    {
                        //same file, no need to upload
                        this.syncProgressPage.addFileExistsLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.SyncTargetBucket,
                            fileFullPath, fileKey));
                        this.syncProgressPage.updateUploadLog("空间已存在,跳过文件 " + fileFullPath);
                        this.syncProgressPage.updateTotalUploadProgress();

                        //compatible, insert or update sync log for file
                        try
                        {
                            SyncLog.InsertOrUpdateSyncLog(fileKey, fileFullPath, fileLastModified, this.syncLogDB);
                        }
                        catch (Exception ex)
                        {
                            Log.Error(string.Format("insert ot update sync log error {0}", ex.Message));
                        }
                        this.doneEvent.Set();
                        return;
                    }
                    else
                    {
                        if (this.syncSetting.OverwriteFile)
                        {
                            overwriteUpload = true;
                            this.syncProgressPage.updateUploadLog("空间已存在,将覆盖 " + fileFullPath);
                        }
                        else
                        {
                            this.syncProgressPage.updateUploadLog("空间已存在,不覆盖 " + fileFullPath);
                            this.syncProgressPage.addFileNotOverwriteLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.SyncTargetBucket,
                                fileFullPath, fileKey));
                            this.syncProgressPage.updateTotalUploadProgress();
                            this.doneEvent.Set();
                            return;
                        }
                    }
                }
            }
            else
            {
                //check locally
                try
                {
                    SyncLog syncLog = SyncLog.GetSyncLogByKey(fileKey, this.syncLogDB);
                    if (!string.IsNullOrEmpty(syncLog.Key))
                    {
                        //has sync log and check whether it changes
                        if (syncLog.LocalPath.Equals(fileFullPath) && syncLog.LastModified.Equals(fileLastModified))
                        {
                            this.syncProgressPage.addFileExistsLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.SyncTargetBucket,
                            fileFullPath, fileKey));
                            this.syncProgressPage.updateUploadLog("本地检查已同步,跳过" + fileFullPath);
                            this.syncProgressPage.updateTotalUploadProgress();
                            this.doneEvent.Set();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(string.Format("get sync log failed {0}", ex.Message));
                }

                if (this.syncSetting.OverwriteFile)
                {
                    overwriteUpload = true;
                }
            }

            //if file not exists or need to overwrite
            this.syncProgressPage.updateUploadLog("准备上传文件 " + fileFullPath);
            UploadManager uploadManger = new UploadManager(new Qiniu.Storage.Persistent.ResumeRecorder(recordPath),
                new Qiniu.Storage.Persistent.KeyGenerator(delegate() { return recorderKey; }));
            PutPolicy putPolicy = new PutPolicy();
            if (overwriteUpload)
            {
                putPolicy.Scope = this.syncSetting.SyncTargetBucket + ":" + fileKey;
            }
            else
            {
                putPolicy.Scope = this.syncSetting.SyncTargetBucket;
            }
            putPolicy.SetExpires(24 * 30 * 3600);
            string uptoken = Auth.createUploadToken(putPolicy, mac);

            this.syncProgressPage.updateUploadLog("开始上传文件 " + fileFullPath);
            uploadManger.uploadFile(fileFullPath, fileKey, uptoken, new UploadOptions(null, null, false,
                new UpProgressHandler(delegate(string key, double percent)
                {
                    this.syncProgressPage.updateSingleFileProgress(taskId, fileFullPath, fileKey, fileLength, percent);
                }), new UpCancellationSignal(delegate()
                {
                    return this.syncProgressPage.checkCancelSignal();
                }))
                , new UpCompletionHandler(delegate(string key, ResponseInfo respInfo, string response)
                {
                    if (respInfo.StatusCode != 200)
                    {
                        this.syncProgressPage.updateUploadLog("上传失败 " + fileFullPath + "," + respInfo.Error);
                        this.syncProgressPage.addFileUploadErrorLog(string.Format("{0}\t{1}\t{2}\t{3}", this.syncSetting.SyncTargetBucket,
                                fileFullPath, fileKey, respInfo.Error + "" + response));

                        //file exists error
                        if (respInfo.StatusCode == 614)
                        {
                            this.syncProgressPage.updateUploadLog("空间已存在,未覆盖 " + fileFullPath);
                            this.syncProgressPage.addFileNotOverwriteLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.SyncTargetBucket,
                                fileFullPath, fileKey));
                        }
                    }
                    else
                    {
                        //insert or update sync log for file
                        try
                        {
                            SyncLog.InsertOrUpdateSyncLog(fileKey, fileFullPath, fileLastModified, this.syncLogDB);
                        }
                        catch (Exception ex)
                        {
                            Log.Error(string.Format("insert ot update sync log error {0}", ex.Message));
                        }

                        //write new file hash to local db
                        if (!overwriteUpload)
                        {
                            PutRet putRet = JsonConvert.DeserializeObject<PutRet>(response);
                            string fileHash = putRet.Hash;
                            if (this.localHashDB != null)
                            {
                                try
                                {
                                    CachedHash.InsertOrUpdateCachedHash(fileFullPath, fileHash, fileLastModified, this.localHashDB);
                                }
                                catch (Exception ex)
                                {
                                    Log.Error(string.Format("insert or update cached hash error {0}", ex.Message));
                                }
                            }
                            Log.Debug(string.Format("insert or update qiniu hash to local: '{0}' => '{1}'", fileFullPath, fileHash));
                        }

                        //update
                        if (overwriteUpload)
                        {
                            this.syncProgressPage.addFileOverwriteLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.SyncTargetBucket,
                                 fileFullPath, fileKey));
                        }
                        this.syncProgressPage.updateUploadLog("上传成功 " + fileFullPath);
                        this.syncProgressPage.addFileUploadSuccessLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.SyncTargetBucket,
                                fileFullPath, fileKey));
                        this.syncProgressPage.updateTotalUploadProgress();
                    }

                    this.doneEvent.Set();
                }));
        }
コード例 #25
0
ファイル: BucketManager.cs プロジェクト: qiniu/csharp-sdk
 public BucketManager(Mac mac)
 {
     this.mac = mac;
     this.mHttpManager = new HttpManager();
 }
コード例 #26
0
        private bool UpLoadFile(string filepath)
        {
            string filename = System.IO.Path.GetFileName(filepath);

            Qiniu.Util.Mac lMac           = new Qiniu.Util.Mac(UserAccount.AccessKey, UserAccount.SecretKey);
            string         lRemoteHash    = RemoteFileInfo.RemoteFileStat(lMac, UserAccount.SpaceName, filename);
            bool           lSkip          = false;
            bool           lUpLoadSuccess = false;
            //check local
            string lLocalHash = String.Empty;

            if (!string.IsNullOrEmpty(lRemoteHash))
            {
                lLocalHash = QETag.hash(filepath);

                if (historyLog.ContainsKey(lLocalHash))
                {
                    if (historyLog[lLocalHash].Contains(filename))
                    {
                        lSkip          = true;
                        URL            = CreateURL(filename);
                        lUpLoadSuccess = true;
                    }
                }
                else if (string.Equals(lLocalHash, lRemoteHash))
                {
                    lSkip          = true;
                    URL            = CreateURL(filename);
                    lUpLoadSuccess = true;
                }
            }
            if (!lSkip)
            {
                putPolicy.Scope = UserAccount.SpaceName;
                putPolicy.SetExpires(3600 * 24 * 30);
                string        lUploadToken = Auth.createUploadToken(putPolicy, lMac);
                UploadManager lUploadMgr   = new UploadManager();
                lUploadMgr.uploadFile(filepath, filename, lUploadToken, null, new UpCompletionHandler(delegate(string key, ResponseInfo responseinfo, string response)
                {
                    if (responseinfo.StatusCode != 200)
                    {
                        MessageStr = Properties.Resources.ErrorMessage;
                    }
                    else
                    {
                        MessageStr = Properties.Resources.SuccessMessage;
                        if (historyLog.ContainsKey(lLocalHash))
                        {
                            historyLog[lLocalHash].Add(filename);
                        }
                        URL            = CreateURL(filename);
                        lUpLoadSuccess = true;
                    }
                }));
            }

            if (lUpLoadSuccess)
            {
                DisplayImage(filepath);
                MessageStr = URL;
            }

            return(lUpLoadSuccess);
        }
コード例 #27
0
        /// <summary>
        /// sync setting page loaded event handler
        /// 更新 2016-10-20 15:51
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SyncSettingPageLoaded_EventHandler(object sender, RoutedEventArgs e)
        {
            // 尝试载入account
            this.account = Account.TryLoadAccount();

            // 检查AK&SK
            if (string.IsNullOrEmpty(this.account.AccessKey) || string.IsNullOrEmpty(this.account.SecretKey))
            {
                this.SettingsErrorTextBlock.Text = "请设置AK&SK";
                return;
            }

            // 设置AK&SK
            SystemConfig.ACCESS_KEY = this.account.AccessKey;
            SystemConfig.SECRET_KEY = this.account.SecretKey;

            // 初始化bucketManager
            Mac mac = new Mac(this.account.AccessKey, this.account.SecretKey);
            this.bucketManager = new BucketManager(mac);

            if (!ValidateAccount())
            {
                // Account设置不正确-->无法开始任务
                ButtonStartSync.IsEnabled = false;
                return;
            }

            ButtonStartSync.IsEnabled = true;

            // 重建bucket列表
            this.SyncTargetBucketsComboBox.ItemsSource = null;
            new Thread(new ThreadStart(this.reloadBuckets)).Start();

            // 其他界面元素初始化
            this.initUIDefaults();
        }
コード例 #28
0
ファイル: Dfop.cs プロジェクト: qiniu/csharp-sdk
 public Dfop(Mac mac)
 {
     this.mHttpManager = new HttpManager();
     this.mac = mac;
 }
コード例 #29
0
ファイル: Auth.cs プロジェクト: satng/qiniu-csharp-sdk
 public static string createUploadToken(PutPolicy putPolicy, Mac mac)
 {
     string jsonData = putPolicy.ToString();
     return mac.SignWithData(Encoding.UTF8.GetBytes(jsonData));
 }
コード例 #30
0
ファイル: Auth.cs プロジェクト: satng/qiniu-csharp-sdk
 public static string createDownloadToken(string rawUrl, Mac mac)
 {
     return mac.Sign(Encoding.UTF8.GetBytes(rawUrl));
 }
コード例 #31
0
ファイル: Auth.cs プロジェクト: satng/qiniu-csharp-sdk
 public static string createManageToken(string url, byte[] reqBody, Mac mac)
 {
     return string.Format("QBox {0}", mac.SignRequest(url, reqBody));
 }
コード例 #32
0
 public BucketManager(Mac mac)
 {
     this.mac = mac;
 }
コード例 #33
0
 public static string createManageToken(string url, byte[] reqBody, Mac mac)
 {
     return(string.Format("QBox {0}", mac.SignRequest(url, reqBody)));
 }
コード例 #34
0
 public static string createDownloadToken(string rawUrl, Mac mac)
 {
     return(mac.Sign(Encoding.UTF8.GetBytes(rawUrl)));
 }
コード例 #35
0
        public static string createUploadToken(PutPolicy putPolicy, Mac mac)
        {
            string jsonData = putPolicy.ToString();

            return(mac.SignWithData(Encoding.UTF8.GetBytes(jsonData)));
        }
コード例 #36
0
 /// <summary>
 ///     初始化
 /// </summary>
 /// <param name="mac">账号(密钥)</param>
 public Signature(Mac mac)
 {
     _mac = mac;
 }