Exemple #1
0
        public static void UploadBlobFile(byte[] fileBytes, string fileName)
        {
            try
            {
                string storageAccountConnection = string.Empty;

                storageAccountConnection = ConfigurationManager.AppSettings["StorageAccount.ConnectionString"].ToString();

                // If you want to use Windows Azure cloud storage account, use the following
                // code (after uncommenting) instead of the code above.
                cloudStorageAccount = CloudStorageAccount.Parse(storageAccountConnection);

                // Create the blob client, which provides
                // authenticated access to the Blob service.
                blobClient = cloudStorageAccount.CreateCloudBlobClient();

                string deploymentPackageFolderString = string.Empty;

                deploymentPackageFolderString = ConfigurationManager.AppSettings["DeploymentPackageFolder"].ToString();

                // Get the container reference.
                blobContainer = blobClient.GetContainerReference(deploymentPackageFolderString);

                // Create the container if it does not exist.
                blobContainer.CreateIfNotExist();

                // Set permissions on the container.
                containerPermissions = new BlobContainerPermissions();

                // This sample sets the container to have public blobs. Your application
                // needs may be different. See the documentation for BlobContainerPermissions
                // for more information about blob container permissions.
                containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob;

                blobContainer.SetPermissions(containerPermissions);

                blob = blobContainer.GetBlobReference(fileName);

                // Open a stream using the cloud object
                using (BlobStream blobStream = blob.OpenWrite())
                {
                    blobStream.Write(fileBytes, 0, fileBytes.Count());

                    blobStream.Flush();

                    blobStream.Close();
                }
            }
            catch (System.Exception ex)
            {
                Logger.Write(string.Format("Error in UploadBlobFile()  Error: {0}", ex.Message));
            }
        }