Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new BlockBlobURL object identical to the source but with the specified version ID.
        /// </summary>
        /// <remarks>
        /// Pass null or empty string to remove the snapshot returning a URL to the base blob.
        /// </remarks>
        /// <param name="versionId">A string of the version identifier.</param>
        /// <returns></returns>
        //public new BlockBlobClient WithVersionId(string versionId) => (BlockBlobUri)this.WithVersionIdImpl(versionId);

        //protected sealed override Blobclient WithVersionIdImpl(string versionId)
        //{
        //    var builder = new BlobUriBuilder(this.Uri) { VersionId = versionId };
        //    return new BlockBlobClient(builder.ToUri(), this.Pipeline);
        //}

        /// <summary>
        /// The <see cref="UploadAsync"/> operation creates a new block  blob,
        /// or updates the content of an existing block blob.  Updating an
        /// existing block blob overwrites any existing metadata on the blob.
        ///
        /// Partial updates are not supported with <see cref="UploadAsync"/>;
        /// the content of the existing blob is overwritten with the content
        /// of the new blob.  To perform a partial update of the content of a
        /// block blob, use the <see cref="StageBlockAsync"/> and
        /// <see cref="CommitBlockListAsync" /> operations.
        ///
        /// For more information, see <see href="https://docs.microsoft.com/rest/api/storageservices/put-blob" />.
        /// </summary>
        /// <param name="content">
        /// A <see cref="Stream"/> containing the content to upload.
        /// </param>
        /// <param name="blobHttpHeaders">
        /// Optional standard HTTP header properties that can be set for the
        /// block blob.
        /// </param>
        /// <param name="metadata">
        /// Optional custom metadata to set for this block blob.
        /// </param>
        /// <param name="blobAccessConditions">
        /// Optional <see cref="BlockBlobClient"/> to add
        /// conditions on the creation of this new block blob.
        /// </param>
        /// <param name="progressHandler">
        /// Optional <see cref="IProgress{StorageProgress}"/> to provide
        /// progress updates about data transfers.
        /// </param>
        /// <param name="cancellation">
        /// Optional <see cref="CancellationToken"/> to propagate
        /// notifications that the operation should be cancelled.
        /// </param>
        /// <returns>
        /// A <see cref="Task{Response{BlobContentInfo}}"/> describing the
        /// state of the updated block blob.
        /// </returns>
        /// <remarks>
        /// A <see cref="StorageRequestFailedException"/> will be thrown if
        /// a failure occurs.
        /// </remarks>
        public StorageTask <Response <BlobContentInfo> > UploadAsync(
            Stream content,
            BlobHttpHeaders?blobHttpHeaders = default,
            Metadata metadata = default,
            BlobAccessConditions?blobAccessConditions   = default,
            IProgress <StorageProgress> progressHandler = default,
            CancellationToken cancellation = default)
        {
            content = content.WithNoDispose().WithProgress(progressHandler);
            var uploadAttempt = 0;

            return(StorageTask.Create(
                       pipeline: this.Pipeline,
                       cancellationToken: cancellation,
                       reliabilityConfiguration: new ReliabilityConfiguration(reset: () => content.Seek(0, SeekOrigin.Begin)),
                       operation:
                       async(p, ct) =>
            {
                using (p.BeginLoggingScope(nameof(BlockBlobClient)))
                {
                    p.LogMethodEnter(
                        nameof(BlockBlobClient),
                        message:
                        $"{nameof(this.Uri)}: {this.Uri}\n" +
                        $"{nameof(blobHttpHeaders)}: {blobHttpHeaders}\n" +
                        $"{nameof(blobAccessConditions)}: {blobAccessConditions}");
                    try
                    {
                        p.LogTrace($"Upload attempt {++uploadAttempt}");
                        return await BlobRestClient.BlockBlob.UploadAsync(
                            p,
                            this.Uri,
                            body: content,
                            contentLength: content.Length,
                            blobContentType: blobHttpHeaders?.ContentType,
                            blobContentEncoding: blobHttpHeaders?.ContentEncoding,
                            blobContentLanguage: blobHttpHeaders?.ContentLanguage,
                            blobContentHash: blobHttpHeaders?.ContentHash,
                            blobCacheControl: blobHttpHeaders?.CacheControl,
                            metadata: metadata,
                            leaseId: blobAccessConditions?.LeaseAccessConditions?.LeaseId,
                            blobContentDisposition: blobHttpHeaders?.ContentDisposition,
                            ifModifiedSince: blobAccessConditions?.HttpAccessConditions?.IfModifiedSince,
                            ifUnmodifiedSince: blobAccessConditions?.HttpAccessConditions?.IfUnmodifiedSince,
                            ifMatch: blobAccessConditions?.HttpAccessConditions?.IfMatch,
                            ifNoneMatch: blobAccessConditions?.HttpAccessConditions?.IfNoneMatch,
                            cancellation: ct)
                        .ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        p.LogException(ex);
                        throw;
                    }
                    finally
                    {
                        p.LogMethodExit(nameof(BlockBlobClient));
                    }
                }
            }));
        }