Represents a blob that is uploaded as a set of blocks.
Inheritance: CloudBlob
Exemplo n.º 1
0
        private static string AcquireLease(Microsoft.WindowsAzure.StorageClient.CloudBlockBlob blob, NLog.Logger logger)
        {
            try
            {
                var creds          = blob.ServiceClient.Credentials;
                var transformedUri = new Uri(creds.TransformUri(blob.Uri.ToString()));
                var req            = BlobRequest.Lease(transformedUri, AzureBlobLeaseTimeout,                             // timeout (in seconds)
                                                       Microsoft.WindowsAzure.StorageClient.Protocol.LeaseAction.Acquire, // as opposed to "break" "release" or "renew"
                                                       null);                                                             // name of the existing lease, if any
                blob.ServiceClient.Credentials.SignRequest(req);
                using (var response = req.GetResponse())
                {
                    return(response.Headers["x-ms-lease-id"]);
                }
            }

            catch (WebException e)
            {
                if (null != logger)
                {
                    logger.ErrorException("AcquireLease, blob: " + blob, e);
                }
                return(null);
            }
        }
 public static void ExecuteMethod_ReleaseLock(CloudBlockBlob[] batchToProcess)
 {
     if (batchToProcess == null)
         return;
     // Release lock
     StorageSupport.ReleaseLogicalLockByDeletingBlob(LockLocation, null);
 }
Exemplo n.º 3
0
        //This function is using storage client ddl of blob
        public byte[] DownloadBlobClient(string UserId, string BlobName)
        {
            // Retrieve storage account from connection string.
            Microsoft.WindowsAzure.CloudStorageAccount StorageAccount = Microsoft.WindowsAzure.CloudStorageAccount.Parse(Convert.ToString(ConfigurationManager.AppSettings["StorageConnectionString"]));

            // Create the blob client.
            Microsoft.WindowsAzure.StorageClient.CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.StorageClient.CloudBlobContainer container = blobClient.GetContainerReference(UserId);

            // Create the container if it doesn't exist.
            container.CreateIfNotExist();


            //Set permission to public
            container.SetPermissions(
                new Microsoft.WindowsAzure.StorageClient.BlobContainerPermissions
            {
                PublicAccess =
                    Microsoft.WindowsAzure.StorageClient.BlobContainerPublicAccessType.Off
            });


            // Retrieve reference to a blob named

            Microsoft.WindowsAzure.StorageClient.CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobName);


            return(blockBlob.DownloadByteArray());
        }
        public void Setup()
        {
            var storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
            var container = storageAccount.CreateCloudBlobClient().GetContainerReference("webdeploylease");
            container.CreateIfNotExist();

            _blob = container.GetBlockBlobReference(Constants.WebDeployBlobName);
        }
 public static DateTime GetTarget_EndingTimeOfCurrentItems(CloudBlockBlob[] currentMonitoringItems)
 {
     if (currentMonitoringItems.Length == 0)
         return DateTime.MinValue.ToUniversalTime();
     var lastBlobName = currentMonitoringItems[currentMonitoringItems.Length - 1].Name;
     DateTime startTime;
     DateTime endTime;
     getTimeRangeFromBlobName(lastBlobName, out startTime, out endTime);
     return endTime;
 }
Exemplo n.º 6
0
 /// <summary>Initializes a new instance of the <see cref="ParallelUpload"/> class.</summary>
 /// <param name="source">The source stream. </param>
 /// <param name="options">The request options. </param>
 /// <param name="blockSize">The block size to use. </param>
 /// <param name="blob">The blob to upload to. </param>
 internal ParallelUpload(Stream source, BlobRequestOptions options, long blockSize, CloudBlockBlob blob)
 {
     this.sourceStream = source;
     this.blockSize = blockSize;
     this.options = options;
     this.dispensizedStreamSize = 0;
     this.blob = blob;
     this.blobHash = MD5.Create();
     this.blockList = new List<string>();
     this.parellelism = this.GetParallelismFactor();
 }
 public static DateTime GetTarget_EndingTimeOfNewItems(int monitoringItemTimeSpanInMinutes, DateTime startingTimeOfNewItems, CloudBlockBlob[] newResourceUsageBlobs)
 {
     DateTime endingTimeOfNewItems = startingTimeOfNewItems;
     if (newResourceUsageBlobs.Length > 0)
     {
         var lastBlobName = newResourceUsageBlobs[newResourceUsageBlobs.Length - 1].Name;
         DateTime startTime;
         DateTime endTime;
         getTimeRangeFromBlobName(lastBlobName, out startTime, out endTime);
         while (endingTimeOfNewItems.AddMinutes(monitoringItemTimeSpanInMinutes) < endTime)
             endingTimeOfNewItems = endingTimeOfNewItems.AddMinutes(monitoringItemTimeSpanInMinutes);
     }
     return endingTimeOfNewItems;
 }
Exemplo n.º 8
0
 /// <summary>
 /// Returns a String value of the blob's ContentType Metadata Property.
 /// </summary>
 /// <param name="listBlobItem">Consumes Microsoft.WindowsAzure.StorageClient.CloudBlockBlob</param>
 /// <returns>Returns string of blob's Content Type, Will return "N/A" if getting the blob metadata was unsuccessful</returns>
 public static string GetBlob_ContentType(CloudBlockBlob listBlobItem)
 {
     try
     {
         var ContentType = listBlobItem.Properties.ContentType;
         string SContentType = Convert.ToString(ContentType);
         if (SContentType.Length < 1)
         { return "N/A"; }
         return XmlConvert.EncodeName(SContentType);
     }
     catch
     {
         return "N/A";
     }
 }
Exemplo n.º 9
0
 /// <summary>
 /// Returns a String value of the blob's Last Modified Date in UTC.
 /// </summary>
 /// <param name="listBlobItem">Consumes Microsoft.WindowsAzure.StorageClient.CloudBlockBlob</param>
 /// <returns>Returns string of blob's Last Modified Date in UTC, Will return "N/A" if getting the blob metadata was unsuccessful</returns>
 public static string GetBlob_LastModifiedUtc(CloudBlockBlob listBlobItem)
 {
     try
     {
         var LastModifiedUtc = listBlobItem.Properties.LastModifiedUtc;
         string SLastModifiedUtc = Convert.ToString(LastModifiedUtc);
         if (SLastModifiedUtc.Length < 1)
         { return "N/A"; }
         return XmlConvert.EncodeName(SLastModifiedUtc);
     }
     catch
     {
         return "N/A";
     }
 }
Exemplo n.º 10
0
 /// <summary>
 /// Returns a String value of the blob's DownloadLocation Metadata Property.
 /// </summary>
 /// <param name="listBlobItem">Consumes Microsoft.WindowsAzure.StorageClient.CloudBlockBlob</param>
 /// <returns>Returns string of blob's download location, Will return "N/A" if getting the blob metadata was unsuccessful</returns>
 public static string GetBlob_DownloadLocation(CloudBlockBlob listBlobItem)
 {
     try
     {
         var DownloadLocation = listBlobItem.Uri;
         string SDownloadLocation = Convert.ToString(DownloadLocation);
         if (SDownloadLocation.Length < 1)
         { return "N/A"; }
         return XmlConvert.EncodeName(SDownloadLocation);
     }
     catch
     {
         return "N/A";
     }
 }
Exemplo n.º 11
0
        /// <summary>Initializes a new instance of the BlobWriteStream class.</summary>
        /// <param name="blob">The blob used for uploads. </param>
        /// <param name="options">The options used for the stream. </param>
        /// <param name="blockSize">The size of the blocks to use. </param>
        internal BlobWriteStream(CloudBlockBlob blob, BlobRequestOptions options, long blockSize)
        {
            CommonUtils.AssertNotNull("blob", blob);
            CommonUtils.AssertNotNull("options", options);

            this.Blob = blob;
            this.blobHash = MD5.Create();
            this.BlockSize = blockSize;
            this.blockList = new List<string>();
            this.currentModifier = options;
            this.canWrite = true;

            var rand = new Random();
            this.blockIdSequenceNumber = (long)rand.Next() << 32;
            this.blockIdSequenceNumber += rand.Next();
        }
 public static void ExecuteMethod_ProcessBatch(CloudBlockBlob[] batchToProcess)
 {
     if (batchToProcess == null)
         return;
     Dictionary<string, List<RequestResourceUsage>> ownerGroupedUsages = new Dictionary<string, List<RequestResourceUsage>>();
     Type type = typeof (RequestResourceUsage);
     int i = 0;
     foreach (var blob in batchToProcess)
     {
         i++;
         Debug.WriteLine("Reading resource " + i + ": " + blob.Name);
         RequestResourceUsage resourceUsage = (RequestResourceUsage) StorageSupport.RetrieveInformation(blob.Name, type);
         addResourceUsageToOwner(resourceUsage, ownerGroupedUsages);
     }
     storeOwnerContentsAsCollections(ownerGroupedUsages);
 }
Exemplo n.º 13
0
 private static bool BlockBlobExists(Microsoft.WindowsAzure.StorageClient.CloudBlockBlob blob)
 {
     try
     {
         blob.FetchAttributes();
         return(true);
     }
     catch (Microsoft.WindowsAzure.StorageClient.StorageClientException e)
     {
         if (e.ErrorCode == Microsoft.WindowsAzure.StorageClient.StorageErrorCode.ResourceNotFound)
         {
             return(false);
         }
         else
         {
             throw;
         }
     }
 }
Exemplo n.º 14
0
 /// <summary>
 /// Method to check if a blockblob exists already
 /// </summary>
 /// <param name="logger"></param>
 /// <param name="blob"></param>
 /// <returns></returns>
 public static bool BlockBlobExists(VLogger logger, CloudBlockBlob blob)
 {
     try
     {
         blob.FetchAttributes();
         return true;
     }
     catch (StorageClientException e)
     {
         if (e.ErrorCode == StorageErrorCode.ResourceNotFound)
         {
             Utils.structuredLog(logger, "E", "BlockBlob: " + blob.Name + " does not exist.");
             return false;
         }
         else
         {
             throw;
         }
     }
 }
 public static void ExecuteMethod_DeleteProcessedItems(CloudBlockBlob[] batchToProcess)
 {
     if (batchToProcess == null)
         return;
     //int i = 0;
     //foreach (var blob in batchToProcess)
     //{
     //    i++;
     //    Debug.WriteLine("Deleting blob " + i + ": " + blob.Name);
     //    blob.DeleteWithoutFiringSubscriptions();
     //}
     int i = 0;
     ParallelOptions options = new ParallelOptions {MaxDegreeOfParallelism = 50};
     Parallel.ForEach(batchToProcess, options, blob =>
         {
             i++;
             Debug.WriteLine("Deleting blob " + i + ": " + blob.Name);
             blob.DeleteIfExists(); // NOTE! Need to use direct delete so that the InformationContext is not bothered with Task usage
         });
 }
Exemplo n.º 16
0
        public void CopyFromBlob(string destinationSasUri, string srcBlobSasUri)
        {
            CloudBlockBlob blob = new CloudBlockBlob(srcBlobSasUri);
            string fileName = (blob.Name.Contains("/"))
                ? blob.Name.Substring(blob.Name.LastIndexOf("/"))
                : blob.Name;
            CloudBlobContainer cbc = new CloudBlobContainer(destinationSasUri);

            //UriBuilder ub = new UriBuilder(destUri);
            //ub.Path += "/" + fileName;
            //CloudBlockBlob destBlob = new CloudBlockBlob(ub.Uri);
            CloudBlockBlob destBlob = cbc.GetBlockBlobReference(fileName);
            BlobRequestOptions bro = new BlobRequestOptions();
            bro.RetryPolicy =  RetryPolicies.RetryExponential(5, TimeSpan.FromMilliseconds(150));
            destBlob.BeginCopyFromBlob(blob, bro, (result) => {  }, null);

               // destBlob.UploadFromStream(System.IO.File.OpenRead(@"D:\Install.txt"));

            System.Diagnostics.Debug.WriteLine(destBlob.Name);
        }
Exemplo n.º 17
0
 public static string CalculateBlockBlob_SHA512(CloudBlockBlob blob, long BlobByteSize)
 {
     if ((BlobByteSize < Config.Settings.MaxDownloadSize) && (BlobByteSize > 0))
     {
         try
         {
             string filename = BlobStorage.Access.DownloadBlockBlob(blob, Config.Settings.DownloadPath);
             string StaticFilePath = Config.Settings.DownloadPath + filename;
             string checksumSha512 = GetChecksum(StaticFilePath, Algorithms.SHA512);
             DeleteFile(StaticFilePath);
             if (checksumSha512.Equals("N/A"))
             { return "N/A"; }
             return checksumSha512;
         }
         catch
         {
             return "N/A";
         }
     }
     return "N/A";
 }
Exemplo n.º 18
0
        public static string AcquireLease(VLogger logger, CloudBlockBlob blob, int AzureBlobLeaseTimeout)
        {
            try
            {
                var creds = blob.ServiceClient.Credentials;
                var transformedUri = new Uri(creds.TransformUri(blob.Uri.ToString()));
                var req = BlobRequest.Lease(transformedUri, AzureBlobLeaseTimeout, // timeout (in seconds)
                    LeaseAction.Acquire, // as opposed to "break" "release" or "renew"
                    null); // name of the existing lease, if any
                blob.ServiceClient.Credentials.SignRequest(req);
                using (var response = req.GetResponse())
                {
                    return response.Headers["x-ms-lease-id"];
                }
            }

            catch (WebException e)
            {
                Utils.structuredLog(logger, "WebException", e.Message + ". AcquireLease, blob: " + blob);
                return null;
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// given a specific CloudBlockBlob blob, this will download the blob to a specifed location with a guid as the file name
        /// </summary>
        /// <param name="blob">Consumes Microsoft.WindowsAzure.StorageClient.CloudBlockBlob</param>
        /// <param name="LocalDownloadPath">A string of where the blob should be downloaded to</param>
        /// <returns>Returns a string of the name of the file the blob was saved to, it will be random guid</returns>
        public static string DownloadBlockBlob(CloudBlockBlob blob, string LocalDownloadPath)
        {
            try
            {
                // Create and display the value of two GUIDs.
                string filename = Guid.NewGuid().ToString();
                string contianername = blob.Container.Name;
                string blobname = blob.Name;

                if (contianername.Length > 50)
                {
                    contianername = contianername.Substring(0, 49);
                }
                if (blobname.Length > 50)
                {
                    blobname = blobname.Substring(0, 49);
                }
                filename = filename.Substring(0, 8);
                filename = filename+"_" + contianername + "_" + blobname;
                foreach (char c in System.IO.Path.GetInvalidFileNameChars())
                {
                    filename = filename.Replace(c, '-');
                }

                using (var fileStream = System.IO.File.OpenWrite(LocalDownloadPath + "/" + filename))
                {
                    blob.DownloadToStream(fileStream);
                }
                return filename;
            }
            catch (Exception e)
            {
                Alerting.ErrorLogging.WriteTo_Log("Failed Download Blob", e.ToString());
                return "N/A";
            }
        }
Exemplo n.º 20
0
 /// <summary>
 /// Returns a String value of the blob's MD5 Metadata Property.
 /// </summary>
 /// <param name="listBlobItem">Consumes Microsoft.WindowsAzure.StorageClient.CloudBlockBlob</param>
 /// <returns>Returns string of blob's MD5, Will return "N/A" if getting the blob metadata was unsuccessful</returns>
 public static string GetBlob_MD5(CloudBlockBlob listBlobItem)
 {
     try
     {
         var md5 = listBlobItem.Properties.ContentMD5;
         string Smd5 = Convert.ToString(md5);
         if (Smd5.Length < 1)
         {return "N/A";}
         return XmlConvert.EncodeName(Smd5);
     }
     catch
     {
         return "N/A";
     }
 }
Exemplo n.º 21
0
 public AzureBlobFileStorage(CloudBlockBlob blob, string rootPath) {
     _blob = blob;
     _rootPath = rootPath;
 }
Exemplo n.º 22
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ParallelUpload"/> class.
 /// </summary>
 /// <param name="source">The source stream.</param>
 /// <param name="options">The request options.</param>
 /// <param name="blockSize">The block size to use.</param>
 /// <param name="blob">The blob to upload to.</param>
 internal ParallelUpload(Stream source, BlobRequestOptions options, long blockSize, CloudBlockBlob blob)
 {
     this.sourceStream          = source;
     this.blockSize             = blockSize;
     this.options               = options;
     this.dispensizedStreamSize = 0;
     this.blob        = blob;
     this.blobHash    = MD5.Create();
     this.blockList   = new List <string>();
     this.parellelism = this.GetParallelismFactor();
 }
 public static ProcessBatchOfResourceUsagesToOwnerCollectionsReturnValue Get_ReturnValue(int processBatchSize, CloudBlockBlob[] batchToProcess)
 {
     return new ProcessBatchOfResourceUsagesToOwnerCollectionsReturnValue
         {
             ProcessedAnything = batchToProcess != null,
             ProcessedFullCount = batchToProcess != null && processBatchSize == batchToProcess.Length
         };
 }
Exemplo n.º 24
0
 /// <summary>
 /// Returns a String value of the blob's Size Metadata Property.
 /// </summary>
 /// <param name="listBlobItem">Consumes Microsoft.WindowsAzure.StorageClient.CloudBlockBlob</param>
 /// <returns>Returns string of blob's Size in Bytes, Will return "N/A" if getting the blob metadata was unsuccessful</returns>
 public static string GetBlob_Size(CloudBlockBlob listBlobItem)
 {
     try
     {
         var size = listBlobItem.Properties.Length;
         string Ssize = Convert.ToString(size);
         if (Ssize.Length < 1)
         { return "N/A"; }
         return XmlConvert.EncodeName(Ssize);
     }
     catch
     {
         return "N/A";
     }
 }
 public static RequestResourceUsageCollection[] GetTarget_ResourcesToIncludeInMonitoring(CloudBlockBlob[] newResourceUsageBlobs, DateTime endingTimeOfNewItems)
 {
     List<RequestResourceUsageCollection> includedItems = new List<RequestResourceUsageCollection>();
     Type type = typeof (RequestResourceUsageCollection);
     foreach (var blob in newResourceUsageBlobs)
     {
         DateTime startTime;
         DateTime endTime;
         getTimeRangeFromBlobName(blob.Name, out startTime, out endTime);
         if (startTime >= endingTimeOfNewItems)
             break;
         RequestResourceUsageCollection resourceCollection = (RequestResourceUsageCollection)StorageSupport.RetrieveInformation(blob.Name, type);
         includedItems.Add(resourceCollection);
     }
     return includedItems.ToArray();
 }
 public static DateTime GetTarget_StartingTimeOfNewItems(int monitoringItemTimeSpanInMinutes, DateTime endingTimeOfCurrentItems, CloudBlockBlob[] newResourceUsageBlobs)
 {
     if (newResourceUsageBlobs.Length == 0)
         return endingTimeOfCurrentItems;
     DateTime newItemStartingTime = endingTimeOfCurrentItems;
     var firstBlobName = newResourceUsageBlobs[0].Name;
     DateTime blobStartTime;
     DateTime blobEndTime;
     getTimeRangeFromBlobName(firstBlobName, out blobStartTime, out blobEndTime);
     if (newItemStartingTime.Date != blobStartTime.Date)
     {
         // if new item starting time is not same date, let's seek "closest" timespan minute from beginning of day
         newItemStartingTime = blobStartTime.Date;
         while (newItemStartingTime.AddMinutes(monitoringItemTimeSpanInMinutes) < blobStartTime)
             newItemStartingTime = newItemStartingTime.AddMinutes(monitoringItemTimeSpanInMinutes);
     }
     return newItemStartingTime;
 }
Exemplo n.º 27
0
 /// <summary>
 /// Returns a String value of the blob's Name Metadata Property.
 /// </summary>
 /// <param name="listBlobItem">Consumes Microsoft.WindowsAzure.StorageClient.CloudBlockBlob</param>
 /// <returns>Returns string of blob's Name, Will return "N/A" if getting the blob metadata was unsuccessful</returns>
 public static string GetBlob_Name(CloudBlockBlob listBlobItem)
 {
     try
     {
         var name = listBlobItem.Name;
         string Sname = Convert.ToString(name);
         if (Sname.Length < 1)
         { return "N/A"; }
         return XmlConvert.EncodeName(Sname);
     }
     catch
     {
         return "N/A";
     }
 }
Exemplo n.º 28
0
        public static bool DownloadConfig(string downloadedZipPath, string AzureAccountName, string AzureAccountKey, string orgID, string studyID, string homeID, string configFilename, NLog.Logger logger = null)
        {
            Microsoft.WindowsAzure.CloudStorageAccount              storageAccount = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobClient    blobClient     = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobContainer container      = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlockBlob     blockBlob      = null;
            string leaseId = null;

            try
            {
                storageAccount = new Microsoft.WindowsAzure.CloudStorageAccount(new Microsoft.WindowsAzure.StorageCredentialsAccountAndKey(AzureAccountName, AzureAccountKey), true);
                blobClient     = storageAccount.CreateCloudBlobClient();
                container      = blobClient.GetContainerReference(AzureConfigContainerName);

                if (configFilename == PackagerHelper.ConfigPackagerHelper.actualConfigFileName)
                {
                    blockBlob = container.GetBlockBlobReference(ActualConfigBlobName(orgID, studyID, homeID, configFilename));
                }
                else if (configFilename == PackagerHelper.ConfigPackagerHelper.desiredConfigFileName)
                {
                    blockBlob = container.GetBlockBlobReference(DesiredConfigBlobName(orgID, studyID, homeID, configFilename));
                }

                bool blobExists = BlockBlobExists(blockBlob);

                if (blobExists)
                {
                    leaseId = AcquireLease(blockBlob, logger); // Acquire Lease on Blob
                }
                else
                {
                    return(false);
                }

                if (blobExists && leaseId == null)
                {
                    if (null != logger)
                    {
                        logger.Error("AcquireLease on Blob: " + ActualConfigBlobName(orgID, studyID, homeID, configFilename) + " Failed");
                    }
                    return(false);
                }

                string url = blockBlob.Uri.ToString();
                if (blockBlob.ServiceClient.Credentials.NeedsTransformUri)
                {
                    url = blockBlob.ServiceClient.Credentials.TransformUri(url);
                }

                var req = BlobRequest.Get(new Uri(url), AzureBlobLeaseTimeout, null, leaseId);
                blockBlob.ServiceClient.Credentials.SignRequest(req);

                using (var reader = new BinaryReader(req.GetResponse().GetResponseStream()))
                {
                    FileStream zipFile = new FileStream(downloadedZipPath, FileMode.OpenOrCreate);
                    reader.BaseStream.CopyTo(zipFile);
                    zipFile.Close();
                }
                req.GetResponse().GetResponseStream().Close();

                ReleaseLease(blockBlob, leaseId); // Release Lease on Blob
                return(true);
            }
            catch (Exception e)
            {
                if (null != logger)
                {
                    logger.ErrorException("DownloadConfig_Azure, downloadZipPath: " + downloadedZipPath, e);
                }
                ReleaseLease(blockBlob, leaseId);
                return(false);
            }
        }
 private void Commit(CloudBlockBlob blob, List<string> originalOrder)
 {
     var commitAttempt = 0;
     while (commitAttempt <= MaxRetries)
     {
         try
         {
             blob.PutBlockList(originalOrder);
             break;
         }
         catch (Exception)
         {
             commitAttempt++;
             if (commitAttempt > MaxRetries) throw;
         }
     }
 }
Exemplo n.º 30
0
        /// <summary>
        /// Selects the protocol response.
        /// </summary>
        /// <param name="protocolItem">The protocol item.</param>
        /// <param name="service">The service.</param>
        /// <param name="container">The container.</param>
        /// <returns>The parsed <see cref="IListBlobItem"/>.</returns>
        internal static IListBlobItem SelectProtocolResponse(IListBlobEntry protocolItem, CloudBlobClient service, CloudBlobContainer container)
        {
            BlobEntry blob = protocolItem as BlobEntry;
            if (blob != null)
            {
                var attributes = blob.Attributes;
                CloudBlob cloudBlob;
                if (attributes.Properties.BlobType == BlobType.BlockBlob)
                {
                    cloudBlob = new CloudBlockBlob(attributes, service, ConvertDateTimeToSnapshotString(attributes.Snapshot));
                }
                else if (attributes.Properties.BlobType == BlobType.PageBlob)
                {
                    cloudBlob = new CloudPageBlob(attributes, service, ConvertDateTimeToSnapshotString(attributes.Snapshot));
                }
                else
                {
                    cloudBlob = new CloudBlob(attributes, service, ConvertDateTimeToSnapshotString(attributes.Snapshot));
                }

                return cloudBlob;
            }

            BlobPrefixEntry blobPrefix = protocolItem as BlobPrefixEntry;

            if (blobPrefix != null)
            {
                if (container != null)
                {
                    return container.GetDirectoryReference(blobPrefix.Name);
                }
                else
                {
                    return new CloudBlobDirectory(blobPrefix.Name, service);
                }
            }

            throw new InvalidOperationException("Invalid blob list item returned");
        }
Exemplo n.º 31
0
 public AzureBlobFileStorage(CloudBlockBlob blob) {
     _blob = blob;
 }
Exemplo n.º 32
0
            public Stream CreateFile() {
                // as opposed to the File System implementation, if nothing is done on the stream
                // the file will be emptied, because Azure doesn't implement FileMode.Truncate
                _blob.DeleteIfExists();
                _blob = _blob.Container.GetBlockBlobReference(_blob.Uri.ToString());
                _blob.OpenWrite().Dispose(); // force file creation

                return OpenWrite();
            }
 public AzureBlockBlob(CloudBlockBlob blob)
 {
     this.blob = blob;
 }
Exemplo n.º 34
0
        public static void ParallelDownloadToFile(CloudBlockBlob blob, string fileName, int maxBlockSize)
        {
            try
            {
                // refresh the values
                blob.FetchAttributes();
                long fileSize = blob.Attributes.Properties.Length;
                var filePath = Path.GetDirectoryName(fileName);
                var fileNameWithoutPath = Path.GetFileNameWithoutExtension(fileName);

                // let's figure out how big the file is here
                long leftToRead = fileSize;
                int startPosition = 0;

                // have 1 block for every maxBlockSize bytes plus 1 for the remainder
                var blockCount =
                ((int)Math.Floor((double)(fileSize / maxBlockSize))) + 1;

                // setup the control array
                BlockTransferDetail[] transferDetails =
                new BlockTransferDetail[blockCount];

                // create an array of block keys
                string[] blockKeys = new string[blockCount];
                var blockIds = new List<string>();

                // populate the control array...
                for (int j = 0; j < transferDetails.Length; j++)
                {
                    int toRead = (int)(maxBlockSize < leftToRead ?
                    maxBlockSize :
                    leftToRead);

                    string blockId = Path.Combine(filePath,
                    string.Format("{0}_{1}.dat",
                    fileNameWithoutPath,
                    j.ToString("00000000000")));

                    if (startPosition < 0)
                        startPosition = startPosition * -1;
                    if (toRead < 0)
                        toRead = toRead * -1;
                    transferDetails[j] = new BlockTransferDetail()
                    {
                        StartPosition = startPosition,
                        BytesToRead = toRead,
                        BlockId = blockId
                    };

                    if (toRead > 0)
                    {
                        blockIds.Add(blockId);
                    }

                    // increment the starting position
                    startPosition += toRead;
                    leftToRead -= toRead;
                }

                // now we do a || download of the file.
                var result = Parallel.For(0, transferDetails.Length, j =>
                {
                    // get the blob as a stream
                    try
                    {
                        using (BlobStream stream = blob.OpenRead())
                        {
                            Thread.Sleep(10000);
                            stream.Seek(transferDetails[j].StartPosition, SeekOrigin.Begin);

                            // setup a buffer with the proper size
                            byte[] buff = new byte[transferDetails[j].BytesToRead];

                            // read into the buffer
                            stream.Read(buff, 0, transferDetails[j].BytesToRead);

                            using (Stream fileStream = new FileStream(transferDetails[j].BlockId,
                                FileMode.Create, FileAccess.Write, FileShare.None))
                            {
                                using (BinaryWriter bw = new BinaryWriter(fileStream))
                                {
                                    bw.Write(buff);
                                    bw.Close();
                                }
                            }
                            buff = null;
                        }
                    }
                    catch (Exception )
                    {
                        throw;
                    }
                });

                // assemble the file into one now...
                using (Stream fileStream = new FileStream(fileName,
                FileMode.Append, FileAccess.Write, FileShare.None))
                {
                    using (BinaryWriter bw = new BinaryWriter(fileStream))
                    {
                        // loop through each of the files on the disk
                        for (int j = 0; j < transferDetails.Length; j++)
                        {
                            // read them into the file (append)
                            bw.Write(System.IO.File.ReadAllBytes(transferDetails[j].BlockId));

                            // and then delete them
                            System.IO.File.Delete(transferDetails[j].BlockId);
                        }
                    }
                }

                transferDetails = null;
            }
            catch (Exception )
            {
                throw;
            }
        }
        private void UploadBlobInParallel(CloudBlockBlob blob, Stream stream)
        {
            var blocksToUpload = new Queue<Block>();
            var order = new List<string>();
            CalculateBlocks(blocksToUpload, (int)stream.Length, order);

            ExecuteInParallel(() => AsLongAsThereAre(blocksToUpload, block =>
            {
                try
                {
                    var buffer = new byte[BlockSize];
                    lock (stream)
                    {
                        stream.Position = block.Offset;
                        stream.Read(buffer, 0, block.Length);
                    }
                    using (var memoryStream = new MemoryStream(buffer, 0, block.Length))
                    {
                        blob.PutBlock(block.Name, memoryStream, null);
                    }
                }
                catch
                {
                    if (++block.Attempt > MaxRetries) throw;

                    lock (blocksToUpload) blocksToUpload.Enqueue(block);
                }
            }));

            Commit(blob, order);
        }
Exemplo n.º 36
0
        public static bool UploadConfig(string configZipPath, string AzureAccountName, string AzureAccountKey, string orgID, string studyID, string homeID, string desiredConfigFilename, NLog.Logger logger = null)
        {
            Microsoft.WindowsAzure.CloudStorageAccount              storageAccount = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobClient    blobClient     = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlobContainer container      = null;
            Microsoft.WindowsAzure.StorageClient.CloudBlockBlob     blockBlob      = null;
            string leaseId = null;

            try
            {
                storageAccount = new Microsoft.WindowsAzure.CloudStorageAccount(new Microsoft.WindowsAzure.StorageCredentialsAccountAndKey(AzureAccountName, AzureAccountKey), true);
                blobClient     = storageAccount.CreateCloudBlobClient();
                container      = blobClient.GetContainerReference(AzureConfigContainerName);
                container.CreateIfNotExist();
                blockBlob = container.GetBlockBlobReference(DesiredConfigBlobName(orgID, studyID, homeID, desiredConfigFilename));

                bool blobExists = BlockBlobExists(blockBlob);

                if (blobExists)
                {
                    leaseId = AcquireLease(blockBlob, logger); // Acquire Lease on Blob
                }
                else
                {
                    blockBlob.Container.CreateIfNotExist();
                }

                if (blobExists && leaseId == null)
                {
                    if (null != logger)
                    {
                        logger.Error("AcquireLease on Blob: " + DesiredConfigBlobName(orgID, studyID, homeID, desiredConfigFilename) + " Failed");
                    }
                    return(false);
                }

                string url = blockBlob.Uri.ToString();
                if (blockBlob.ServiceClient.Credentials.NeedsTransformUri)
                {
                    url = blockBlob.ServiceClient.Credentials.TransformUri(url);
                }

                var req = BlobRequest.Put(new Uri(url), AzureBlobLeaseTimeout, new Microsoft.WindowsAzure.StorageClient.BlobProperties(), Microsoft.WindowsAzure.StorageClient.BlobType.BlockBlob, leaseId, 0);

                using (var writer = new BinaryWriter(req.GetRequestStream()))
                {
                    writer.Write(File.ReadAllBytes(configZipPath));
                    writer.Close();
                }

                blockBlob.ServiceClient.Credentials.SignRequest(req);
                req.GetResponse().Close();
                ReleaseLease(blockBlob, leaseId); // Release Lease on Blob
                return(true);
            }
            catch (Exception e)
            {
                if (null != logger)
                {
                    logger.ErrorException("UploadConfig_Azure, configZipPath: " + configZipPath, e);
                }
                ReleaseLease(blockBlob, leaseId);
                return(false);
            }
        }