/// <summary>
        /// Opens a stream for writing to the blob.
        /// </summary>
        /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the access conditions for the blob. If <c>null</c>, no condition is used.</param>
        /// <param name="options">A <see cref="BlobRequestOptions"/> object that specifies any additional options for the request.</param>
        /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
        /// <returns>A stream to be used for writing to the blob.</returns>
        public IAsyncOperation<ICloudBlobStream> OpenWriteAsync(AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
        {
            this.attributes.AssertNoSnapshot();
            operationContext = operationContext ?? new OperationContext();
            BlobRequestOptions modifiedOptions = BlobRequestOptions.ApplyDefaults(options, this.BlobType, this.ServiceClient, false);

            if ((accessCondition != null) && accessCondition.IsConditional)
            {
                return AsyncInfo.Run(async (token) =>
                {
                    try
                    {
                        await this.FetchAttributesAsync(accessCondition, options, operationContext).AsTask(token);
                    }
                    catch (Exception)
                    {
                        if ((operationContext.LastResult != null) &&
                            (operationContext.LastResult.HttpStatusCode == (int)HttpStatusCode.NotFound) &&
                            string.IsNullOrEmpty(accessCondition.IfMatchETag))
                        {
                            // If we got a 404 and the condition was not an If-Match,
                            // we should continue with the operation.
                        }
                        else
                        {
                            throw;
                        }
                    }

                    ICloudBlobStream stream = new BlobWriteStreamHelper(this, accessCondition, modifiedOptions, operationContext);
                    return stream;
                });
            }
            else
            {
                ICloudBlobStream stream = new BlobWriteStreamHelper(this, accessCondition, modifiedOptions, operationContext);
                return Task.FromResult(stream).AsAsyncOperation();
            }
        }
        public IAsyncOperation<ICloudBlobStream> OpenWriteAsync(bool createNew, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
#endif
        {
            this.attributes.AssertNoSnapshot();
            BlobRequestOptions modifiedOptions = BlobRequestOptions.ApplyDefaults(options, BlobType.AppendBlob, this.ServiceClient, false);
            if (!createNew && modifiedOptions.StoreBlobContentMD5.Value)
            {
                throw new ArgumentException(SR.MD5NotPossible);
            }

#if ASPNET_K || PORTABLE
            return Task.Run(async () =>
#else
            return AsyncInfo.Run(async (token) =>
#endif
            {
                if (createNew)
                {
#if ASPNET_K || PORTABLE
                    await this.CreateOrReplaceAsync(accessCondition, options, operationContext, cancellationToken);
#else
                    await this.CreateOrReplaceAsync(accessCondition, options, operationContext).AsTask(token);
#endif
                }
                else
                {
                    // Although we don't need any properties from the service, we should make this call in order to honor the user specified conditional headers
                    // while opening an existing stream and to get the append position for an existing blob if user didn't specify one.
#if ASPNET_K || PORTABLE
                    await this.FetchAttributesAsync(accessCondition, options, operationContext, cancellationToken);
#else
                    await this.FetchAttributesAsync(accessCondition, options, operationContext).AsTask(token);
#endif
                }

                if (accessCondition != null)
                {
                    accessCondition = new AccessCondition() { LeaseId = accessCondition.LeaseId, IfAppendPositionEqual = accessCondition.IfAppendPositionEqual, IfMaxSizeLessThanOrEqual = accessCondition.IfMaxSizeLessThanOrEqual };
                }

#if ASPNET_K || PORTABLE
                CloudBlobStream stream = new BlobWriteStream(this, accessCondition, modifiedOptions, operationContext);
#else
                ICloudBlobStream stream = new BlobWriteStreamHelper(this, accessCondition, modifiedOptions, operationContext);
#endif
                return stream;
#if ASPNET_K || PORTABLE
            }, cancellationToken);
#else
            });
Exemplo n.º 3
0
        public IAsyncOperation<ICloudBlobStream> OpenWriteAsync(long? size, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
        {
            this.attributes.AssertNoSnapshot();
            bool createNew = size.HasValue;
            BlobRequestOptions modifiedOptions = BlobRequestOptions.ApplyDefaults(options, BlobType.PageBlob, this.ServiceClient, false);
            if (!createNew && modifiedOptions.StoreBlobContentMD5.Value)
            {
                throw new ArgumentException(SR.MD5NotPossible);
            }

            return AsyncInfo.Run(async (token) =>
            {
                if (createNew)
                {
                    await this.CreateAsync(size.Value, accessCondition, options, operationContext).AsTask(token);
                }
                else
                {
                    await this.FetchAttributesAsync(accessCondition, options, operationContext).AsTask(token);
                    size = this.Properties.Length;
                }

                if (accessCondition != null)
                {
                    accessCondition = AccessCondition.GenerateLeaseCondition(accessCondition.LeaseId);
                }

                ICloudBlobStream stream = new BlobWriteStreamHelper(this, size.Value, createNew, accessCondition, modifiedOptions, operationContext);
                return stream;
            });
        }