Example #1
0
        internal async Task CompleteFileUploadAsync(FileUploadCompletionNotification notification, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            await _httpClientHelper.PostAsync(
                GetRequestUri(_deviceId, CommonConstants.BlobUploadStatusPathTemplate + "notifications", null),
                notification,
                ExceptionHandlingHelper.GetDefaultErrorMapping(),
                null,
                cancellationToken).ConfigureAwait(false);
        }
Example #2
0
        internal async Task UploadToBlobAsync(string blobName, Stream source, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var fileUploadRequest = new FileUploadSasUriRequest()
            {
                BlobName = blobName
            };

            FileUploadSasUriResponse fileUploadResponse = await _httpClientHelper.PostAsync <FileUploadSasUriRequest, FileUploadSasUriResponse>(
                GetRequestUri(_deviceId, CommonConstants.BlobUploadPathTemplate, null),
                fileUploadRequest,
                ExceptionHandlingHelper.GetDefaultErrorMapping(),
                null,
                cancellationToken).ConfigureAwait(false);

            string putString = string.Format(
                CultureInfo.InvariantCulture,
                "https://{0}/{1}/{2}{3}",
                fileUploadResponse.HostName,
                fileUploadResponse.ContainerName,
                Uri.EscapeDataString(fileUploadResponse.BlobName), // Pass URL encoded device name and blob name to support special characters
                fileUploadResponse.SasToken);

            var notification = new FileUploadCompletionNotification();

            try
            {
                // 2. Use SAS URI to send data to Azure Storage Blob (PUT)
                var  blob       = new CloudBlockBlob(new Uri(putString));
                Task uploadTask = blob.UploadFromStreamAsync(source);
                await uploadTask.ConfigureAwait(false);

                notification.CorrelationId     = fileUploadResponse.CorrelationId;
                notification.IsSuccess         = uploadTask.IsCompleted;
                notification.StatusCode        = uploadTask.IsCompleted ? 0 : -1;
                notification.StatusDescription = uploadTask.IsCompleted ? null : "Failed to upload to storage.";

                // 3. POST to IoTHub with upload status
                await _httpClientHelper.PostAsync(
                    GetRequestUri(_deviceId, CommonConstants.BlobUploadStatusPathTemplate + "notifications", null),
                    notification,
                    ExceptionHandlingHelper.GetDefaultErrorMapping(),
                    null,
                    cancellationToken).ConfigureAwait(false);
            }
            catch (Exception ex) when(!ex.IsFatal() && !(ex is OperationCanceledException))
            {
                // 3. POST to IoTHub with upload status
                notification.IsSuccess         = false;
                notification.StatusCode        = -1;
                notification.StatusDescription = ex.Message;

                await _httpClientHelper
                .PostAsync(
                    GetRequestUri(
                        _deviceId,
                        CommonConstants.BlobUploadStatusPathTemplate + "notifications/" + fileUploadResponse.CorrelationId,
                        null),
                    notification,
                    ExceptionHandlingHelper.GetDefaultErrorMapping(),
                    null,
                    cancellationToken)
                .ConfigureAwait(false);

                throw;
            }
        }