示例#1
0
 public static CloudBlockBlob StoreFile(string containerName, string virtualFilename, FileInfo file, StorageFileConfig config)
 {
     using (var fs = file.OpenRead())
     {
         config.ContentType = MimeTypeHelper.GetMimeType(file.FullName);
         return(StoreFile(containerName, virtualFilename, fs, config));
     }
 }
示例#2
0
        public static bool StoreDirectory(string containerName, DirectoryInfo directory, StorageFileConfig defaultConfig,
                                          bool rootAsContainer = true)
        {
            if (!directory.Exists)
            {
                return(false);
            }
            var dirName    = directory.Name;
            var removePath = directory.FullName.Substring(0, directory.FullName.Length - dirName.Length);

            if (rootAsContainer)
            {
                removePath = directory.FullName;
            }

            var files = directory.GetFiles("*.*", SearchOption.AllDirectories);

            foreach (var fileInfo in files)
            {
                var virtualPath = fileInfo.FullName.Substring(removePath.Length + 1).ToLower();
                StoreFile(containerName.ToLower(), virtualPath, fileInfo, defaultConfig);
            }
            return(true);
        }
示例#3
0
        public static CloudBlockBlob StoreFile(string containerName, string virtualFilename, Stream fileStream, StorageFileConfig config)
        {
            var client    = StorageAccount.CreateCloudBlobClient();
            var container = client.GetContainerReference(containerName.ToLower());

            if (container.CreateIfNotExists())
            {
                container.SetPermissions(
                    new BlobContainerPermissions
                {
                    PublicAccess =
                        BlobContainerPublicAccessType.Blob
                });
            }
            var correctedPath = String.Join(client.DefaultDelimiter, virtualFilename.Split(new[] { '/', '\\' }));

            var blockBlob = container.GetBlockBlobReference(correctedPath.ToLower());

            if (!config.Replace && blockBlob.Exists())
            {
                return(blockBlob);
            }

            using (fileStream)
            {
                if (config.Gzip)
                {
                    using (var compressedData = new MemoryStream())
                    {
                        using (var gzipStream = new GZipStream(compressedData, CompressionMode.Compress))
                        {
                            var buffer = new byte[1024];
                            var c      = 0;

                            while ((c = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                gzipStream.Write(buffer, 0, c);
                            }
                            gzipStream.Close();
                        }


                        using (var tmp = blockBlob.OpenWrite())
                        {
                            var data = compressedData.ToArray();
                            tmp.Write(data, 0, data.Length);
                        }
                    }
                }
                else
                {
                    blockBlob.UploadFromStream(fileStream);
                }
            }

            blockBlob.Properties.ContentType     = config.ContentType;
            blockBlob.Properties.CacheControl    = config.CacheControl;
            blockBlob.Properties.ContentEncoding = String.IsNullOrWhiteSpace(config.ContentEncoding) ? (config.Gzip ? "gzip" : "") : config.ContentEncoding;
            blockBlob.Properties.ContentLanguage = config.ContentLanguage;
            blockBlob.SetProperties();

            return(blockBlob);
        }