Пример #1
0
        public void WriteBlob(string containerName, string blobName, string localFilename,bool gzip, Action<long> progress)
        {
            if (!File.Exists(localFilename)) {
                throw new FileNotFoundException("local filename does not exist", localFilename);
            }

            if (ContainerExists(containerName)) {
                var container = _blobStore.GetContainerReference(containerName);

                var blob = container.GetBlobReference(blobName);
                var md5 = string.Empty;
                try {
                    blob.FetchAttributes();

                    md5 = blob.Properties.ContentMD5;
                    if (string.IsNullOrEmpty(md5)) {
                        if (blob.Metadata.AllKeys.Contains("MD5")) {
                            md5 = blob.Metadata["MD5"];
                        }
                    }
                } catch {

                }

                var localMD5 = string.Empty;
                using (var stream = new FileStream(localFilename, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                    localMD5 = MD5.Create().ComputeHash(stream).ToHexString();
                }

                if (!string.Equals(md5, localMD5, StringComparison.CurrentCultureIgnoreCase)) {
                    // different file
                    blob.Properties.ContentType = LookupMimeType(Path.GetExtension(localFilename));
                    if (gzip) {
                        blob.Properties.ContentEncoding = "gzip";
                    }

                    try {
                        // copy to tmp file to compress to gz.
                        try {
                            if (gzip) {
                                var localGZFilename = localFilename.GenerateTemporaryFilename();
                                Console.WriteLine("TEMP: {0}", localGZFilename);
                                using (var gzStream = new GZipStream(
                                    new FileStream(localGZFilename, FileMode.CreateNew), CompressionMode.Compress, CompressionLevel.BestCompression)) {
                                        using (var fs = new FileStream(localFilename, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                                        fs.CopyTo(gzStream);
                                        localFilename = localGZFilename;
                                    }
                                }
                            }
                        } catch( Exception e ) {
                            Console.WriteLine("\r\n\r\n{0} - {1}\r\n\r\n", e.Message, e.StackTrace);
                            return;
                        }

                        using (var stream = new ProgressStream(new FileStream(localFilename, FileMode.Open, FileAccess.Read, FileShare.Read), progress)) {
                            blob.UploadFromStream(stream);
                            if (blob.Metadata.AllKeys.Contains("MD5")) {
                                blob.Metadata["MD5"] = localMD5;
                            } else {
                                blob.Metadata.Add("MD5", localMD5);
                            }
                            blob.SetMetadata();
                        }
                    } catch (StorageException e) {
                        if (e.ErrorCode == StorageErrorCode.BlobAlreadyExists || e.ErrorCode == StorageErrorCode.ConditionFailed) {
                            throw new ApplicationException("Concurrency Violation", e);
                        }
                        throw;
                    }
                } else {
                    if (progress != null) {
                        progress(100);
                    }
                }
                return;
            }
            throw new CoAppException("Container '{0}' does not exist".format(containerName));
        }
Пример #2
0
        public void ReadBlob(string containerName, string blobName, string localFilename, Action<long> progress)
        {
            if (ContainerExists(containerName)) {
                var container = _blobStore.GetContainerReference(containerName);
                var blob = container.GetBlobReference(blobName);
                var md5 = string.Empty;
                try {
                    blob.FetchAttributes();

                    md5 = blob.Properties.ContentMD5;
                    if (string.IsNullOrEmpty(md5)) {
                        if (blob.Metadata.AllKeys.Contains("MD5")) {
                            md5 = blob.Metadata["MD5"];
                        }
                    }
                } catch {

                }

                if (blob.Properties.Length == 0) {
                    throw new CoAppException("Remote blob '{0}' not found".format(blobName));
                }

                if (File.Exists(localFilename)) {
                    var localMD5 = string.Empty;
                    using (var stream = new FileStream(localFilename, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                        localMD5 = MD5.Create().ComputeHash(stream).ToHexString();
                    }
                    if (string.Equals(localMD5, md5, StringComparison.CurrentCultureIgnoreCase)) {
                        if (progress != null) {
                            progress(100);
                        }

                        return;
                    }

                    localFilename.TryHardToDelete();
                }

                try {
                    using (var stream = new ProgressStream(new FileStream(localFilename, FileMode.CreateNew), blob.Properties.Length, progress)) {
                        blob.DownloadToStream(stream);
                    }
                    // blob.dow
                    // blob.DownloadToFile(localFilename);
                } catch (StorageException e) {
                    if (e.ErrorCode == StorageErrorCode.BlobAlreadyExists || e.ErrorCode == StorageErrorCode.ConditionFailed) {
                        throw new ApplicationException("Concurrency Violation", e);
                    }
                    throw;
                }
                return;
            }
            throw new CoAppException("Container '{0}' does not exist".format(containerName));
        }