private void ProcessEvent(LoggingEvent loggingEvent)
        {
            try
            {
                var blob = container.GetAppendBlobReference(Path.Combine(DirectoryName, FileName));
                try
                {
                    blob.CreateOrReplace(
                        AccessCondition.GenerateIfNotExistsCondition(),
                        new BlobRequestOptions()
                    {
                        RetryPolicy = new LinearRetry(TimeSpan.FromSeconds(1), 10)
                    },
                        null);
                }
                catch (StorageException ex) when(ex.RequestInformation?.HttpStatusCode == (int)HttpStatusCode.Conflict)
                {
                }

                var message = loggingEvent.GetFormattedString(Layout);
                blob.AppendText(message, Encoding.UTF8);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                throw;
            }
        }
示例#2
0
        protected override void Write(LogEventInfo logEvent)
        {
            if (_client == null)
            {
                return;
            }

            string containerName = Container.Render(logEvent);
            string blobName      = BlobName.Render(logEvent);

            if (_container == null || _container.Name != containerName)
            {
                _container = _client.GetContainerReference(containerName);
                _blob      = null;
            }

            if (_blob == null || _blob.Name != blobName)
            {
                _blob = _container.GetAppendBlobReference(blobName);

                if (!_blob.Exists())
                {
                    try
                    {
                        _blob.Properties.ContentType = "text/plain";
                        _blob.CreateOrReplace(AccessCondition.GenerateIfNotExistsCondition(), null, null);
                    }
                    catch (StorageException)
                    { }
                }
            }
            var logMessage = this.Layout.Render(logEvent);

            _blob.AppendText(logMessage + "\r\n", Encoding.UTF8);
        }
        public async Task OpenWriteAsyncRejectsETagMismatchFoundBeforeUploadStarts()
        {
            // Arrange
            var folderName      = CoreConstants.Folders.ValidationFolderName;
            var fileName        = _prefixA;
            var expectedContent = "Hello, world.";

            await _targetA.SaveFileAsync(
                folderName,
                fileName,
                new MemoryStream(Encoding.ASCII.GetBytes(expectedContent)),
                overwrite : false);

            var container = _clientA.GetContainerReference(folderName);
            var file      = container.GetBlobReference(fileName);

            // Act & Assert
            var ex = await Assert.ThrowsAsync <StorageException>(
                async() =>
            {
                using (var stream = await file.OpenWriteAsync(AccessCondition.GenerateIfNotExistsCondition()))
                {
                    await stream.WriteAsync(new byte[0], 0, 0);
                }
            });

            Assert.Equal(HttpStatusCode.Conflict, (HttpStatusCode)ex.RequestInformation.HttpStatusCode);
        }
        public async Task <Stream> OpenWriteAsync(string fullname)
        {
            var all = await Blobs(fullname);

            var name = fullname + "/" + DateTime.UtcNow.ToString("yyyyMMddHHmmss");

            // Not the latest: don't bother writing.
            if (all.Length > 0 && string.CompareOrdinal(all[all.Length - 1].Name, name) >= 0)
            {
                return(null);
            }

            // Clean-up the old and deprecated versions of the cache
            if (all.Length > 3)
            {
                for (var i = 0; i < all.Length - 3; ++i)
                {
                    await _container.GetBlockBlobReference(all[i].Name).DeleteAsync();
                }
            }

            var blob = _container.GetBlockBlobReference(name);

            return(await blob.OpenWriteAsync(
                       AccessCondition.GenerateIfNotExistsCondition(),
                       new BlobRequestOptions(),
                       new OperationContext()));
        }
示例#5
0
        public async Task CopyAsync(string sourceFileName, string id, long version, string suffix, CancellationToken ct = default)
        {
            var targetName = GetObjectName(id, version, suffix);
            var targetBlob = blobContainer.GetBlobReference(targetName);

            var sourceBlob = blobContainer.GetBlockBlobReference(sourceFileName);

            try
            {
                await targetBlob.StartCopyAsync(sourceBlob.Uri, null, AccessCondition.GenerateIfNotExistsCondition(), null, null, ct);

                while (targetBlob.CopyState.Status == CopyStatus.Pending)
                {
                    ct.ThrowIfCancellationRequested();

                    await Task.Delay(50, ct);

                    await targetBlob.FetchAttributesAsync(null, null, null, ct);
                }

                if (targetBlob.CopyState.Status != CopyStatus.Success)
                {
                    throw new StorageException($"Copy of temporary file failed: {targetBlob.CopyState.Status}");
                }
            }
            catch (StorageException ex) when(ex.RequestInformation.HttpStatusCode == 409)
            {
                throw new AssetAlreadyExistsException(targetName);
            }
            catch (StorageException ex) when(ex.RequestInformation.HttpStatusCode == 404)
            {
                throw new AssetNotFoundException(sourceFileName, ex);
            }
        }
示例#6
0
        private async Task CreateNewBlobAsync(byte[] serial)
        {
            await _container.CreateIfNotExistsAsync();

            var blob = _container.GetBlockBlobReference(BlobName);
            await blob.UploadFromByteArrayAsync(serial, 0, serial.Length, AccessCondition.GenerateIfNotExistsCondition(), null, null);
        }
示例#7
0
        public async Task PutAsync(Stream content, CancellationToken cancellationToken)
        {
            string path;

            using (var hasher = SHA512.Create())
            {
                var hashHex  = hasher.ComputeHash(content).Select(b => b.ToString("X2"));
                var fileName = string.Concat(hashHex).ToLowerInvariant();

                path = Path.Combine("SHA512", fileName);
            }

            var blob      = _container.GetBlockBlobReference(path);
            var condition = AccessCondition.GenerateIfNotExistsCondition();

            try
            {
                _logger.LogInformation("Saving content at path {Path}...", path);

                await blob.UploadFromStreamAsync(
                    content,
                    condition,
                    options : null,
                    operationContext : null,
                    cancellationToken : cancellationToken);
            }
            catch (StorageException e) when(e?.RequestInformation?.HttpStatusCode == 409)
            {
                _logger.LogInformation(
                    "Content already exists at path {Path}",
                    path);
            }
        }
示例#8
0
        protected override BlobWriterResponse Write(CloudBlockBlob blob, MemoryStream input, ReceiveContentArgs content)
        {
            blob.Properties.ContentType  = MediaTypeNames.Application.Zip;
            blob.Properties.CacheControl = "immutable;max-age=31536000"; //TODO hard coded 1 year.
            try
            {
                blob.UploadFromStream(input, AccessCondition.GenerateIfNotExistsCondition());
                return(new BlobWriterResponse {
                    Uri = blob.Uri, ItemAddedOrOverwritten = true
                });
            }
            catch (StorageException e)
            {
                if (e.RequestInformation.HttpStatusCode == 409)
                {
                    return new BlobWriterResponse {
                               Uri = blob.Uri, ItemAddedOrOverwritten = false
                    }
                }
                ;

                throw;
            }
        }
    }
示例#9
0
        public void UpdateFromExistingUpdatesExistingStatus(PackageState previousState, AccessCondition accessCondition, PackageState newState)
        {
            // Arrange
            var feedPackageIdentity = new FeedPackageIdentity("howdy", "3.4.6");

            var existingStatus = PackageMonitoringStatusTestUtility.CreateStatusWithPackageValidationResult(
                feedPackageIdentity.Id,
                feedPackageIdentity.Version,
                PackageMonitoringStatusTestUtility.GetTestResultFromPackageState(previousState));

            existingStatus.AccessCondition = accessCondition;

            var newStatus = PackageMonitoringStatusTestUtility.CreateStatusWithPackageValidationResult(
                feedPackageIdentity.Id,
                feedPackageIdentity.Version,
                PackageMonitoringStatusTestUtility.GetTestResultFromPackageState(newState));

            // Act
            PackageMonitoringStatusAccessConditionHelper.UpdateFromExisting(newStatus, existingStatus);

            // Assert
            foreach (var state in Enum.GetValues(typeof(PackageState)).Cast <PackageState>())
            {
                PackageMonitoringStatusTestUtility.AssertAccessCondition(
                    state == previousState ? accessCondition : AccessCondition.GenerateIfNotExistsCondition(),
                    newStatus.ExistingState[state]);
            }
        }
示例#10
0
        /// <summary>
        /// If the blob was successfully uploaded, returns true.
        /// If the blob already exists, returns false.
        /// Otherwise throws an exception.
        /// </summary>
        private async Task <bool> TryUploadNewExecutableAsBlob(Stream source, string blobName, string creator, string originalFileName)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (blobName == null)
            {
                throw new ArgumentNullException("blobName");
            }

            CloudBlockBlob blob = binContainer.GetBlockBlobReference(blobName);

            try
            {
                await blob.UploadFromStreamAsync(source, AccessCondition.GenerateIfNotExistsCondition(),
                                                 new BlobRequestOptions()
                {
                    RetryPolicy = retryPolicy
                }, null);

                blob.Metadata.Add(KeyCreator, StripNonAscii(creator));
                blob.Metadata.Add(KeyFileName, StripNonAscii(originalFileName));
                await blob.SetMetadataAsync(AccessCondition.GenerateEmptyCondition(), new BlobRequestOptions { RetryPolicy = retryPolicy }, null);

                return(true);
            }
            catch (StorageException ex) when(ex.RequestInformation?.HttpStatusCode == (int)HttpStatusCode.Conflict)
            {
                return(false);
            }
        }
示例#11
0
        public async Task <StoragePutResult> PutAsync(
            string path,
            Stream content,
            string contentType,
            CancellationToken cancellationToken)
        {
            var blob      = _container.GetBlockBlobReference(path);
            var condition = AccessCondition.GenerateIfNotExistsCondition();

            blob.Properties.ContentType = contentType;

            try
            {
                await blob.UploadFromStreamAsync(
                    content,
                    condition,
                    options : null,
                    operationContext : null,
                    cancellationToken : cancellationToken);

                return(StoragePutResult.Success);
            }
            catch (StorageException e) when(e.IsAlreadyExistsException())
            {
                using (var targetStream = await blob.OpenReadAsync(cancellationToken))
                {
                    content.Position = 0;
                    return(content.Matches(targetStream)
                        ? StoragePutResult.AlreadyExists
                        : StoragePutResult.Conflict);
                }
            }
        }
示例#12
0
        private void AssertAccessCondition(Uri resourceUri, AccessCondition accessCondition)
        {
            Content.TryGetValue(resourceUri, out var existingContent);
            if (IsAccessCondition(AccessCondition.GenerateEmptyCondition(), accessCondition))
            {
                return;
            }

            if (IsAccessCondition(AccessCondition.GenerateIfNotExistsCondition(), accessCondition))
            {
                Assert.Null(existingContent);
                return;
            }

            if (IsAccessCondition(AccessCondition.GenerateIfExistsCondition(), accessCondition))
            {
                Assert.NotNull(existingContent);
                return;
            }

            if (existingContent is StringStorageContentWithETag eTagContent)
            {
                var eTag = eTagContent.ETag;
                if (IsAccessCondition(AccessCondition.GenerateIfMatchCondition(eTag), accessCondition))
                {
                    return;
                }
            }

            throw new InvalidOperationException("Could not validate access condition!");
        }
        /// <summary>
        /// Creates a new blob from the specified stream content.
        /// </summary>
        /// <param name="blobPath">The relative path, from the storage root, to the blob.</param>
        /// <param name="data">The stream to add to the blob.</param>
        /// <param name="overwrite">Whether blob should be overwritten if it already exists.</param>
        /// <exception cref="ConflictException">The blob specified in <paramref name="blobPath"/> already exists (<see cref="ErrorCodes.BlobAlreadyExists"/>).</exception>
        /// <returns></returns>
        public async Task WriteBlobAsync(string blobPath, BlobStreamDecorator data, bool overwrite = false)
        {
            CloudBlobClient blobClient = m_LazyBlobClient.Value;
            CloudBlockBlob  blockBlob  = new CloudBlockBlob(GetAbsoluteBlobUri(blobPath), blobClient.Credentials);

            try
            {
                //associate metadata to the blob, which will be saved by the next upload operation
                data.ApplyDecoratorTo(blockBlob);

                //use IfNotExistsCondition by default to avoid overriding an existing blob without knowing about it
                //access condition to ensure blob does not exist yet to catch concurrency issues
                AccessCondition condition = null;
                if (!overwrite)
                {
                    condition = AccessCondition.GenerateIfNotExistsCondition();
                }

                await blockBlob.UploadFromStreamAsync(data, condition, null, null);
            }
            catch (StorageException ex) when(ex.RequestInformation.ErrorCode == BlobErrorCodeStrings.BlobAlreadyExists)
            {
                //blob already exists, overwriting is currently not implemented => concurrency error on the client side
                throw new ConflictException(ErrorCodes.BlobAlreadyExists);
            }
        }
示例#14
0
        private async Task <CloudAppendBlob> GetBlobReferenceAsync(CloudStorageAccount storageAccount, string blobContainerName, string blobName, bool bypassBlobCreationValidation)
        {
            CloudBlobClient    cloudBlobClient    = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(blobContainerName);

            await CreateBlobContainerIfNotExistsAsync(cloudBlobContainer, bypassBlobCreationValidation).ConfigureAwait(false);

            CloudAppendBlob newCloudAppendBlob = null;

            try
            {
                newCloudAppendBlob = cloudBlobContainer.GetAppendBlobReference(blobName);
                newCloudAppendBlob.CreateOrReplaceAsync(AccessCondition.GenerateIfNotExistsCondition(), null, null).GetAwaiter().GetResult();
            }
            catch (StorageException ex) when(ex.RequestInformation?.HttpStatusCode == (int)HttpStatusCode.Conflict && ex.RequestInformation?.ErrorCode == "BlobAlreadyExists")
            {
                //StorageException (http 409 conflict, error code BlobAlreadyExists) is thrown due to the AccessCondition. The append blob already exists.
                //No problem this is expected
            }
            catch (Exception ex)
            {
                Debugging.SelfLog.WriteLine($"Failed to create blob: {ex}");
                throw;
            }

            if (newCloudAppendBlob != null)
            {
                //this is the first time the code gets its hands on this blob reference, get the blob properties from azure.
                //used later on to know when to roll over the file if the 50.000 max blocks is getting close.
                await newCloudAppendBlob.FetchAttributesAsync().ConfigureAwait(false);
            }

            return(newCloudAppendBlob);
        }
示例#15
0
        public void GenerateIfNotExistsConditionReturnsIfNoneMatchAccessCondition()
        {
            AccessCondition result = AccessCondition.GenerateIfNotExistsCondition();

            Assert.Equal("*", result.IfNoneMatch);
            Assert.Null(result.IfMatch);
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            string name = req.Query["name"];

            try
            {
                log.LogInformation("C# HTTP trigger function processed a request.");


                var StorageURL = Environment.GetEnvironmentVariable("StorageURL");


                string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                dynamic data        = JsonConvert.DeserializeObject(requestBody);
                name = name ?? data?.name;


                var    azureServiceTokenProvider = new AzureServiceTokenProvider();
                string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://storage.azure.com");


                log.LogInformation("accessToken : retrieved");


                // create the credential, using the
                var tokenCredential    = new Microsoft.WindowsAzure.Storage.Auth.TokenCredential(accessToken);
                var storageCredentials = new StorageCredentials(tokenCredential);

                log.LogInformation("credentials : created");
                var fileName = "append";
                var Uri      = new Uri(StorageURL + fileName);
                var blob     = new CloudAppendBlob(Uri, storageCredentials);

                log.LogInformation($"blobfile : setup {0}", Uri);

                if (!(await blob.ExistsAsync()))
                {
                    await blob.CreateOrReplaceAsync(AccessCondition.GenerateIfNotExistsCondition(), null, null);
                }

                await blob.AppendTextAsync(name);

                var fileName2 = "regular.txt";
                var Uri2      = new Uri(StorageURL + fileName2);
                var blob2     = new CloudBlockBlob(Uri2, storageCredentials);

                await blob2.UploadTextAsync(name);
            }
            catch (Exception ex)
            {
                log.LogInformation($"EXEC {ex.ToString()} ");
            }
            return(name != null
            ? (ActionResult) new OkObjectResult($"Hello, {name}")
            : new BadRequestObjectResult("Please pass a name on the query string or in the request body"));
        }
示例#17
0
        public async Task WriteLog(ILog log, CloudBlobContainer container = null)
        {
            try
            {
                if (container == null)
                {
                    container = await GetCloudBlobLogsContainer(LOGS_KEY);
                }
                var    path = this.GetCloudContainerLogPath(log.LogType);
                var    dir  = container.GetDirectoryReference(path);
                var    file = DateTime.Now.Hour.ToString("00") + "00.log";
                string json = JsonConvert.SerializeObject(log, JSON.SerializationSettings);

                // Note: AppendBlockBlob is not available in the Storage Emulator/Explorer
                if (GetCloudConnectionString(LOGS_KEY) == "UseDevelopmentStorage=true")
                {
                    CloudBlockBlob blob = dir.GetBlockBlobReference(file);
                    try
                    {
                        await blob.UploadTextAsync(json);
                    }
                    catch (StorageException ex)
                    {
                    }
                    catch (Exception ex)
                    {
                    }
                }
                else
                {
                    CloudAppendBlob blob = dir.GetAppendBlobReference(DateTime.Now.Hour.ToString("00") + "00.log");
                    if (!(await blob.ExistsAsync()))
                    {
                        try
                        {
                            await blob.CreateOrReplaceAsync(AccessCondition.GenerateIfNotExistsCondition(), null, null);
                        }
                        catch (StorageException ex)
                        {
                        }
                        catch (Exception ex)
                        {
                        }
                    }

                    using (var stream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)))
                    {
                        await blob.AppendBlockAsync(stream);
                    }
                }
            }
            catch (StorageException ex)
            {
            }
            catch (Exception ex)
            {
            }
        }
示例#18
0
        public static async Task <Stream> GetBlobOutputStreamAsync(CloudBlobContainer cloudBlobContainer, string blobName, bool allowOverwriteExisting = false, CancellationToken cancellationToken = default(CancellationToken))
        {
            var blob            = cloudBlobContainer.GetBlockBlobReference(blobName);
            var accessCondition = allowOverwriteExisting
                                ? AccessCondition.GenerateEmptyCondition()
                                : AccessCondition.GenerateIfNotExistsCondition();

            return(new NonFlushingStream(await blob.OpenWriteAsync(accessCondition, null, null, cancellationToken)));
        }
示例#19
0
 internal static async Task EnsureExistsAsync(this CloudAppendBlob blob)
 {
     try
     {
         await blob.CreateOrReplaceAsync(AccessCondition.GenerateIfNotExistsCondition(), null, null).ConfigureAwait(false);
     }
     catch (StorageException ex) when(ex.StorageErrorCodeIs("BlobAlreadyExists"))
     {
     }
 }
示例#20
0
        private bool CreateBlobWithJob(IFetchRequest request, string hash, double allowedCreationTimeSec)
        {
            var requestBlob = resultsContainer.GetPageBlobReference(ResultBlobName(hash));

            while (true)
            {
                try
                {
                    JobManagerTrace.TraceVerbose("Trying to create blob ({0})", hash);
                    requestBlob.Create(0, accessCondition: AccessCondition.GenerateIfNotExistsCondition());
                    JobManagerTrace.TraceVerbose("Blob {0} successfully created", hash);

                    //loading request into blob
                    string blobUri = ResultDataSetUri(hash, true).ToString();

                    JobManagerTrace.TraceVerbose("Filling blob with request. uri = {0}", blobUri);

                    RequestDataSetFormat.CreateRequestBlobDataSet(blobUri, request).Dispose();

                    return(true);
                }
                catch (StorageException e)
                {
                    // Blob already exist - probably someone already works with it
                    if (e.RequestInformation.HttpStatusCode == 412 /* Procondition Failed*/)
                    {
                        JobManagerTrace.TraceVerbose("Can't create blob {0}. It is already exists", hash);
                        try
                        {
                            requestBlob.FetchAttributes();
                        }
                        catch (StorageException)
                        {
                            JobManagerTrace.TraceWarning("Can't get modification time of blob {0}. Retrying", hash);
                            continue;
                        }
                        double allowdSeconds = allowedCreationTimeSec;
                        if ((DateTime.UtcNow - requestBlob.Properties.LastModified.Value).TotalSeconds > allowdSeconds)
                        {
                            JobManagerTrace.TraceWarning("Job blob {0} exists but there are no job records in the job table exists longer than permitted time ({1}). deleting it", hash, allowdSeconds);
                            requestBlob.DeleteIfExists();
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
示例#21
0
 public static async Task <bool> CreateIfNotExistsAsync(this CloudAppendBlob appendBlob, CancellationToken cancellationToken = default(CancellationToken))
 {
     try
     {
         await appendBlob.CreateOrReplaceAsync(AccessCondition.GenerateIfNotExistsCondition(), null, null, cancellationToken);
     }
     catch (StorageException ex) when(ex.RequestInformation.HttpStatusCode == 409)
     {
         return(false);
     }
     return(true);
 }
        internal static void CreateOrUpdateIfNotExistsSucceedsOnNoResource <T>(
            Func <T, SearchRequestOptions, AccessCondition, T> createOrUpdateFunc,
            Func <T> newResourceDefinition)
            where T : IResourceWithETag
        {
            Func <T, AccessCondition, T> createOrUpdate = (a, b) => createOrUpdateFunc(a, null, b);
            var resource = newResourceDefinition();

            var updatedResource = createOrUpdate(resource, AccessCondition.GenerateIfNotExistsCondition());

            Assert.NotEmpty(updatedResource.ETag);
        }
        public static void UpdateFromExisting(PackageMonitoringStatus status, PackageMonitoringStatus existingStatus)
        {
            foreach (var state in Enum.GetValues(typeof(PackageState)).Cast <PackageState>())
            {
                status.ExistingState[state] = AccessCondition.GenerateIfNotExistsCondition();
            }

            if (existingStatus != null)
            {
                status.ExistingState[existingStatus.State] = existingStatus.AccessCondition;
            }
        }
        private async Task UploadCoreAsync(string blobName, Stream stream, CancellationToken ct = default(CancellationToken))
        {
            try
            {
                var tempBlob = blobContainer.GetBlockBlobReference(blobName);

                await tempBlob.UploadFromStreamAsync(stream, AccessCondition.GenerateIfNotExistsCondition(), null, null, ct);
            }
            catch (StorageException ex) when(ex.RequestInformation.HttpStatusCode == 409)
            {
                throw new AssetAlreadyExistsException(blobName);
            }
        }
示例#25
0
 private async Task <ErrorCode> CreateAppendBlobIfNotExists(string blobName)
 {
     try
     {
         var currentBlob = this.Conatiner.GetAppendBlobReference(blobName);
         await currentBlob.CreateOrReplaceAsync(AccessCondition.GenerateIfNotExistsCondition(), null, null);
     }
     catch (StorageException se) when(se.RequestInformation?.HttpStatusCode == (int)HttpStatusCode.Conflict)
     {
         return(ErrorCode.Conflict);
     }
     return(ErrorCode.OK);
 }
示例#26
0
        public async Task TryWriteAsync(string stateName, Func <Stream, Task> write)
        {
            if (!AllowWrite)
            {
                return;
            }

            var all = await Blobs(stateName);

            var name = stateName + "/" + DateTime.UtcNow.ToString("yyyyMMddHHmmss");

            // Not the latest: don't bother writing.
            if (all.Length > 0 && string.CompareOrdinal(all[0].Name, name) >= 0)
            {
                return;
            }

            // Clean-up the old and deprecated versions of the cache
            for (var i = MaxCacheBlobsCount; i < all.Length; ++i)
            {
                try
                {
                    await _container.GetBlockBlobReference(all[i].Name).DeleteAsync();
                }
                catch
                {
                    // It's not a problem if deletion failed.
                }
            }

            var blob   = _container.GetBlockBlobReference(name);
            var stream = await blob.OpenWriteAsync(
                AccessCondition.GenerateIfNotExistsCondition(),
                new BlobRequestOptions(),
                new OperationContext());

            try
            {
                await write(stream).ConfigureAwait(false);

                stream.Dispose();
            }
            catch
            {
                // If failed to write, delete the incomplete blob
                stream.Dispose();
                await blob.DeleteIfExistsAsync().ConfigureAwait(false);

                throw;
            }
        }
        internal static void CreateOrUpdateIfNotExistsFailsOnExistingResource <T>(
            Func <T, SearchRequestOptions, AccessCondition, T> createOrUpdateFunc,
            Func <T> newResourceDefinition,
            Func <T, T> mutateResourceDefinition)
            where T : IResourceWithETag
        {
            Func <T, AccessCondition, T> createOrUpdate = (a, b) => createOrUpdateFunc(a, null, b);
            var createdResource = createOrUpdate(newResourceDefinition(), AccessCondition.GenerateEmptyCondition());
            var mutatedResource = mutateResourceDefinition(createdResource);

            SearchAssert.ThrowsCloudException(
                () => createOrUpdate(mutatedResource, AccessCondition.GenerateIfNotExistsCondition()),
                e => e.IsAccessConditionFailed());
        }
示例#28
0
        //  save
        protected override async Task OnSave(Uri resourceUri, StorageContent content, bool overwrite, CancellationToken cancellationToken)
        {
            string name = GetName(resourceUri);

            CloudBlockBlob blob = _directory.GetBlockBlobReference(name);

            blob.Properties.ContentType  = content.ContentType;
            blob.Properties.CacheControl = content.CacheControl;

            var accessCondition = overwrite ? null : AccessCondition.GenerateIfNotExistsCondition();

            if (CompressContent)
            {
                blob.Properties.ContentEncoding = "gzip";
                using (Stream stream = content.GetContentStream())
                {
                    MemoryStream destinationStream = new MemoryStream();

                    using (GZipStream compressionStream = new GZipStream(destinationStream, CompressionMode.Compress, true))
                    {
                        await stream.CopyToAsync(compressionStream);
                    }

                    destinationStream.Seek(0, SeekOrigin.Begin);

                    await blob.UploadFromStreamAsync(
                        destinationStream,
                        accessCondition,
                        options : null,
                        operationContext : null,
                        cancellationToken : cancellationToken);

                    _logger.LogInformation("Saved compressed blob {BlobUri} to container {ContainerName}", blob.Uri.ToString(), _directory.Container.Name);
                }
            }
            else
            {
                using (Stream stream = content.GetContentStream())
                {
                    await blob.UploadFromStreamAsync(
                        stream,
                        accessCondition,
                        options : null,
                        operationContext : null,
                        cancellationToken : cancellationToken);

                    _logger.LogInformation("Saved uncompressed blob {BlobUri} to container {ContainerName}", blob.Uri.ToString(), _directory.Container.Name);
                }
            }
        }
示例#29
0
        /// <summary>
        /// If blob uploaded, returns its etag; otherwise, if precondition failed for the mode, returns null.
        /// </summary>
        private static async Task <string> UploadBlobAsync(CloudBlobContainer container, string blobName, Stream content, UploadBlobMode mode, string etag = null)
        {
            if (mode == UploadBlobMode.ReplaceExact && etag == null)
            {
                throw new ArgumentException("Etag must be provided when using ReplaceExact mode");
            }

            try
            {
                var stdoutBlob = container.GetBlockBlobReference(blobName);

                AccessCondition accessCondition;
                switch (mode)
                {
                case UploadBlobMode.CreateNew:
                    accessCondition = AccessCondition.GenerateIfNotExistsCondition();
                    break;

                case UploadBlobMode.CreateOrReplace:
                    accessCondition = AccessCondition.GenerateEmptyCondition();
                    break;

                case UploadBlobMode.ReplaceExact:
                    accessCondition = AccessCondition.GenerateIfMatchCondition(etag);
                    break;

                default:
                    throw new ArgumentException("Unknown mode");
                }

                await stdoutBlob.UploadFromStreamAsync(content,
                                                       accessCondition,
                                                       new BlobRequestOptions()
                {
                    RetryPolicy = new Microsoft.WindowsAzure.Storage.RetryPolicies.ExponentialRetry(TimeSpan.FromMilliseconds(100), 14)
                },
                                                       null);

                return(stdoutBlob.Properties.ETag);
            }
            catch (StorageException ex) when(ex.RequestInformation.HttpStatusCode == (int)HttpStatusCode.PreconditionFailed)
            {
                return(null);
            }
            catch (StorageException ex)
            {
                Trace.WriteLine(string.Format("Failed to upload text to the blob: {0}, blob name: {1}, response code: {2}", ex.Message, blobName, ex.RequestInformation.HttpStatusCode));
                throw;
            }
        }
        public override async Task WriteObjectAsync(Sha1 objectId, ArraySegment <byte> item, bool forceOverwrite, CancellationToken cancellationToken)
        {
            var objectsContainer = _objectsContainer.Value;

            var blobName = DeriveBlobName(objectId);
            var blobRef  = objectsContainer.GetAppendBlobReference(blobName);

            // Objects are immutable
            var accessCondition = forceOverwrite ? null : AccessCondition.GenerateIfNotExistsCondition(); // If-None-Match *

            try
            {
                // Required to create blob header before appending to it
                await blobRef.CreateOrReplaceAsync(accessCondition, default, default).ConfigureAwait(false);