コード例 #1
0
        /// <summary>
        /// Implements the Create method.
        /// </summary>
        /// <param name="options">An object that specifies any additional options for the request.</param>
        /// <param name="sizeInBytes">The size in bytes.</param>
        /// <returns>A <see cref="TaskSequence"/> that creates the blob.</returns>
        private TaskSequence CreateImpl(BlobRequestOptions options, long sizeInBytes)
        {
            CommonUtils.AssertNotNull("options", options);

            this.Properties.BlobType = BlobType.PageBlob;

            var webRequest = ProtocolHelper.GetWebRequest(
                this.ServiceClient,
                options,
                (timeout) => BlobRequest.Put(this.TransformedAddress, timeout, this.Properties, BlobType.PageBlob, null, sizeInBytes));

            BlobRequest.AddMetadata(webRequest, this.Metadata);

            this.ServiceClient.Credentials.SignRequest(webRequest);

            var task = webRequest.GetResponseAsyncWithTimeout(this.ServiceClient, options.Timeout);

            yield return(task);

            // Parse the response
            using (HttpWebResponse webResponse = task.Result as HttpWebResponse)
            {
                this.ParseSizeAndLastModified(webResponse);
            }
        }
コード例 #2
0
ファイル: ConfigUpdater.cs プロジェクト: RBSystems/LoT
        private bool UploadConfig_Azure(string configZipPath, string AzureAccountName, string AzureAccountKey)
        {
            CloudStorageAccount storageAccount = null;
            CloudBlobClient     blobClient     = null;
            CloudBlobContainer  container      = null;
            CloudBlockBlob      blockBlob      = null;
            string leaseId = null;

            try
            {
                storageAccount = new CloudStorageAccount(new StorageCredentialsAccountAndKey(AzureAccountName, AzureAccountKey), true);
                blobClient     = storageAccount.CreateCloudBlobClient();
                container      = blobClient.GetContainerReference(AzureConfigContainerName);
                container.CreateIfNotExist();
                blockBlob = container.GetBlockBlobReference(ActualConfigBlobName());

                bool blobExists = AzureUtils.BlockBlobExists(logger, blockBlob);

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

                if (blobExists && leaseId == null)
                {
                    Utils.structuredLog(logger, "ER", "AcquireLease on Blob: " + ActualConfigBlobName() + " 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 BlobProperties(), 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();
                AzureUtils.ReleaseLease(logger, blockBlob, leaseId, AzureBlobLeaseTimeout); // Release Lease on Blob
                return(true);
            }
            catch (Exception e)
            {
                Utils.structuredLog(logger, "E", e.Message + ". UploadConfig_Azure, configZipPath: " + configZipPath + ". " + e);
                AzureUtils.ReleaseLease(logger, blockBlob, leaseId, AzureBlobLeaseTimeout);
                return(false);
            }
        }
        protected override void DoSave(IConcurrencyControlContext context, T obj)
        {
            if (string.IsNullOrWhiteSpace(context.ObjectId))
            {
                throw new ArgumentNullException("context.ObjectId", "ObjectId cannot be null or empty");
            }

            if (context is OptimisticConcurrencyContext)
            {
                CloudBlob blob = this.Container.GetBlobReference(context.ObjectId);
                blob.Properties.ContentType = "application/json";

                var blobRequestOptions = new BlobRequestOptions()
                {
                    AccessCondition = (context as OptimisticConcurrencyContext).AccessCondition
                };

                blob.UploadText(new JavaScriptSerializer().Serialize(obj), Encoding.Default, blobRequestOptions);
            }
            else if (context is PessimisticConcurrencyContext)
            {
                if (string.IsNullOrWhiteSpace((context as PessimisticConcurrencyContext).LockId))
                {
                    throw new ArgumentNullException("context.LockId", "LockId cannot be null or empty");
                }

                var blobProperties = new BlobProperties();
                blobProperties.ContentType = "application/json";

                var updateText = BlobRequest.Put(
                    this.GetUri(context.ObjectId),
                    BlobRequestTimeout,
                    blobProperties,
                    BlobType.BlockBlob,
                    (context as PessimisticConcurrencyContext).LockId,
                    0);
                using (var stream = new StreamWriter(updateText.GetRequestStream(), Encoding.Default))
                {
                    stream.Write(new JavaScriptSerializer().Serialize(obj));
                }
                this.Account.Credentials.SignRequest(updateText);

                using (var response = updateText.GetResponse())
                {
                    if (response is HttpWebResponse &&
                        !HttpStatusCode.Created.Equals((response as HttpWebResponse).StatusCode))
                    {
                        TraceHelper.TraceError("Error writing leased blob '{0}': {1}", context.ObjectId, (response as HttpWebResponse).StatusDescription);
                        throw new InvalidOperationException((response as HttpWebResponse).StatusDescription);
                    }
                }
            }
            else
            {
                throw new InvalidOperationException("IConcurrencyControlContext implementation cannot be handled");
            }
        }
コード例 #4
0
        // NOTE: This method doesn't do everything that the regular UploadText does.
        // Notably, it doesn't update the BlobProperties of the blob (with the new
        // ETag and LastModifiedTimeUtc). It also, like all the methods in this file,
        // doesn't apply any retry logic. Use this at your own risk!
        public static void UploadText(this CloudBlob blob, string text, string leaseId)
        {
            string url = blob.Uri.ToString();

            if (blob.ServiceClient.Credentials.NeedsTransformUri)
            {
                url = blob.ServiceClient.Credentials.TransformUri(url);
            }
            var req = BlobRequest.Put(new Uri(url), 60, new BlobProperties(), BlobType.BlockBlob, leaseId, 0);

            using (var writer = new StreamWriter(req.GetRequestStream()))
            {
                writer.Write(text);
            }
            blob.ServiceClient.Credentials.SignRequest(req);
            req.GetResponse().Close();
        }
コード例 #5
0
        protected virtual void PessimisticControlContextWriteStrategy(IConcurrencyControlContext context, T obj)
        {
            if (string.IsNullOrWhiteSpace((context as PessimisticConcurrencyContext).LockId))
            {
                throw new ArgumentNullException("context.LockId", "LockId cannot be null or empty");
            }

            var blobProperties = new BlobProperties();

            var binarizedObject = this.BinarizeObjectForStreaming(blobProperties, obj);

            var updateRequest = BlobRequest.Put(
                this.GetUri(context.ObjectId),
                BlobRequestTimeout,
                blobProperties,
                BlobType.BlockBlob,
                (context as PessimisticConcurrencyContext).LockId,
                0);

            using (var writer = new BinaryWriter(updateRequest.GetRequestStream(), Encoding.Default))
            {
                writer.Write(binarizedObject);
            }

            this.Account.Credentials.SignRequest(updateRequest);

            using (var response = updateRequest.GetResponse())
            {
                if (response is HttpWebResponse &&
                    !HttpStatusCode.Created.Equals((response as HttpWebResponse).StatusCode))
                {
                    this._logger.LogError("Error writing leased blob '{0}': {1}", context.ObjectId, (response as HttpWebResponse).StatusDescription);
                    throw new InvalidOperationException((response as HttpWebResponse).StatusDescription);
                }
            }
        }
コード例 #6
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);
            }
        }