/// <summary>
        /// Constructs a web request to create a new block blob or page blob, or to update the content 
        /// of an existing block blob. 
        /// </summary>
        /// <param name="uri">The absolute URI to the blob.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <param name="properties">The properties to set for the blob.</param>
        /// <param name="blobType">The type of the blob.</param>
        /// <param name="pageBlobSize">For a page blob, the size of the blob. This parameter is ignored
        /// for block blobs.</param>
        /// <param name="accessCondition">The access condition to apply to the request.</param>
        /// <returns>A web request to use to perform the operation.</returns>
        public static HttpRequestMessage Put(Uri uri, int? timeout, BlobProperties properties, BlobType blobType, long pageBlobSize, AccessCondition accessCondition, HttpContent content, OperationContext operationContext)
        {
            if (blobType == BlobType.Unspecified)
            {
                throw new InvalidOperationException(SR.UndefinedBlobType);
            }

            HttpRequestMessage request = HttpRequestMessageFactory.CreateRequestMessage(HttpMethod.Put, uri, timeout, null /* builder */, content, operationContext);

            if (properties.CacheControl != null)
            {
                request.Headers.CacheControl = CacheControlHeaderValue.Parse(properties.CacheControl);
            }

            if (content != null)
            {
                if (properties.ContentType != null)
                {
                    content.Headers.ContentType = MediaTypeHeaderValue.Parse(properties.ContentType);
                }

                if (properties.ContentMD5 != null)
                {
                    content.Headers.ContentMD5 = Convert.FromBase64String(properties.ContentMD5);
                }

                if (properties.ContentLanguage != null)
                {
                    content.Headers.ContentLanguage.Add(properties.ContentLanguage);
                }

                if (properties.ContentEncoding != null)
                {
                    content.Headers.ContentEncoding.Add(properties.ContentEncoding);
                }

                if (properties.ContentDisposition != null)
                {
                    content.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse(properties.ContentDisposition);
                }
            }

            if (blobType == BlobType.PageBlob)
            {
                request.Headers.Add(Constants.HeaderConstants.BlobType, Constants.HeaderConstants.PageBlob);
                request.Headers.Add(Constants.HeaderConstants.BlobContentLengthHeader, pageBlobSize.ToString(NumberFormatInfo.InvariantInfo));
                properties.Length = pageBlobSize;
            }
            else if (blobType == BlobType.BlockBlob)
            {
                request.Headers.Add(Constants.HeaderConstants.BlobType, Constants.HeaderConstants.BlockBlob);
            }
            else 
            {
                request.Headers.Add(Constants.HeaderConstants.BlobType, Constants.HeaderConstants.AppendBlob);
            }

            request.ApplyAccessCondition(accessCondition);
            return request;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Constructs a web request to create a new block blob or page blob, or to update the content 
        /// of an existing block blob. 
        /// </summary>
        /// <param name="uri">The absolute URI to the blob.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <param name="properties">The properties to set for the blob.</param>
        /// <param name="blobType">The type of the blob.</param>
        /// <param name="leaseId">The lease ID, if the blob has an active lease.</param>
        /// <param name="pageBlobSize">For a page blob, the size of the blob. This parameter is ignored
        /// for block blobs.</param>
        /// <returns>A web request to use to perform the operation.</returns>
        public static HttpWebRequest Put(Uri uri, int timeout, BlobProperties properties, BlobType blobType, string leaseId, long pageBlobSize)
        {
            if (blobType == BlobType.Unspecified)
            {
                throw new InvalidOperationException(SR.UndefinedBlobType);
            }

            HttpWebRequest request = CreateWebRequest(uri, timeout, null);

            request.Method = "PUT";

            if (properties.CacheControl != null)
            {
                request.Headers.Add(HttpRequestHeader.CacheControl, properties.CacheControl);
            }

            if (properties.ContentType != null)
            {
                // Setting it using Headers is an exception
                request.ContentType = properties.ContentType;
            }

            if (properties.ContentMD5 != null)
            {
                request.Headers.Add(HttpRequestHeader.ContentMd5, properties.ContentMD5);
            }

            if (properties.ContentLanguage != null)
            {
                request.Headers.Add(HttpRequestHeader.ContentLanguage, properties.ContentLanguage);
            }

            if (properties.ContentEncoding != null)
            {
                request.Headers.Add(HttpRequestHeader.ContentEncoding, properties.ContentEncoding);
            }

            if (blobType == BlobType.PageBlob)
            {
                request.ContentLength = 0;
                request.Headers.Add(Constants.HeaderConstants.BlobType, Constants.HeaderConstants.PageBlob);
                request.Headers.Add(Constants.HeaderConstants.Size, pageBlobSize.ToString(NumberFormatInfo.InvariantInfo));
                properties.Length = pageBlobSize;
            }
            else
            {
                request.Headers.Add(Constants.HeaderConstants.BlobType, Constants.HeaderConstants.BlockBlob);
            }

            Request.AddLeaseId(request, leaseId);

            return request;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Set the properties of for the blob. Using null for a property means that the original property ofthe blob
        /// remain unaffected, using empty string mean to "erase" the original property value
        /// Note that only
        /// </summary>
        /// <param name="blobName"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        protected async Task SetBlobPropertiesAsync(string blobName, BlobProperties properties)
        {
            Contract.Requires <ArgumentNullException>(!string.IsNullOrWhiteSpace(blobName), "blobName cannot be null or empty");
            ThrowWhenCloudBlobClientDoesNotExist();
            var blob = await Container.GetBlobReferenceFromServerAsync(blobName);

            await blob.FetchAttributesAsync();

            // Update the properties with new values if exist or use the original ones.
            blob.Properties.CacheControl    = properties.CacheControl ?? blob.Properties.CacheControl;
            blob.Properties.ContentEncoding = properties.ContentEncoding ?? blob.Properties.ContentEncoding;
            blob.Properties.ContentLanguage = properties.ContentLanguage ?? blob.Properties.ContentLanguage;
            blob.Properties.ContentMD5      = properties.ContentMD5 ?? blob.Properties.ContentMD5;
            blob.Properties.ContentType     = properties.ContentType ?? blob.Properties.ContentType;
            await blob.SetPropertiesAsync();
        }
Exemplo n.º 4
0
        public static BlobProperties CollectBlobMetadata(UploadedFile file, string fileNameOnly, string userName)
        {
            // Create metadata to be associated with the blob
            NameValueCollection metadata = new NameValueCollection();
            string desc = file.FormValues["fileDescription"];

            metadata["FileDescription"] = string.IsNullOrEmpty(desc) ? "{empty}" : desc;
            metadata["Submitter"]       = userName;
            BlobProperties properties = new BlobProperties(fileNameOnly);

            properties.Metadata = metadata;

            //This will always be .wmv because we just encoded it
            properties.ContentType = "video/x-ms-wmv";
            return(properties);
        }
        public async Task <ITriggerData> BindAsync(object value, ValueBindingContext context)
        {
            ConversionResult <BlobBaseClient> conversionResult = await _converter.TryConvertAsync(value, context.CancellationToken).ConfigureAwait(false);

            if (!conversionResult.Succeeded)
            {
                throw new InvalidOperationException("Unable to convert trigger to IStorageBlob.");
            }

            BlobBaseClient blobClient     = conversionResult.Result;
            BlobProperties blobProperties = await blobClient.FetchPropertiesOrNullIfNotExistAsync(cancellationToken : context.CancellationToken).ConfigureAwait(false);

            IReadOnlyDictionary <string, object> bindingData = CreateBindingData(blobClient, blobProperties);

            return(new TriggerData(bindingData));
        }
Exemplo n.º 6
0
        public static bool PutPropertiesValidator(BlobProperties properties)
        {
            if (properties.BlobType == BlobType.Unspecified)
            {
                return(false);
            }

            if ((properties.BlobType == BlobType.PageBlob) &&
                (properties.Length % 512 != 0) &&
                (properties.Length > 0))
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 7
0
        public void SaveImage(string id, string contentType, byte[] data)
        {
            BlobProperties      properties = new BlobProperties(string.Format(CultureInfo.InvariantCulture, "image_{0}", id));
            NameValueCollection metadata   = new NameValueCollection();

            metadata["Id"]          = id;
            metadata["Filename"]    = "userimage";
            metadata["ImageName"]   = "userimage";
            metadata["Description"] = "userimage";
            metadata["Tags"]        = "userimage";
            properties.Metadata     = metadata;
            properties.ContentType  = contentType;
            BlobContents imageBlob = new BlobContents(data);

            container.CreateBlob(properties, imageBlob, true);
        }
Exemplo n.º 8
0
        public void SetUp()
        {
            _calculationResultsRepository = Substitute.For <ICalculationResultsRepository>();
            _blobClient              = Substitute.For <IBlobClient>();
            _csvUtils                = Substitute.For <ICsvUtils>();
            _transformation          = Substitute.For <IProviderResultsToCsvRowsTransformation>();
            _cloudBlob               = Substitute.For <ICloudBlob>();
            _fileSystemAccess        = Substitute.For <IFileSystemAccess>();
            _fileSystemCacheSettings = Substitute.For <IFileSystemCacheSettings>();
            _jobManagement           = Substitute.For <IJobManagement>();
            _calcsApiClient          = Substitute.For <ICalculationsApiClient>();
            _specsApiClient          = Substitute.For <ISpecificationsApiClient>();

            _service = new ProviderResultsCsvGeneratorService(Substitute.For <ILogger>(),
                                                              _blobClient,
                                                              _calcsApiClient,
                                                              _specsApiClient,
                                                              _calculationResultsRepository,
                                                              new ResiliencePolicies
            {
                BlobClient              = Policy.NoOpAsync(),
                CalculationsApiClient   = Policy.NoOpAsync(),
                SpecificationsApiClient = Policy.NoOpAsync(),
                ResultsRepository       = Policy.NoOpAsync()
            },
                                                              _csvUtils,
                                                              _transformation,
                                                              _fileSystemAccess,
                                                              _fileSystemCacheSettings,
                                                              _jobManagement);

            _message  = new Message();
            _rootPath = NewRandomString();

            _fileSystemCacheSettings.Path
            .Returns(_rootPath);

            _fileSystemAccess
            .Append(Arg.Any <string>(), Arg.Any <Stream>())
            .Returns(Task.CompletedTask);

            _blobProperties = new BlobProperties();

            _cloudBlob
            .Properties
            .Returns(_blobProperties);
        }
Exemplo n.º 9
0
        public async Task IncludesEtagInConditions()
        {
            MemoryStream          stream      = new MemoryStream();
            MockDataSource        dataSource  = new MockDataSource(100);
            Mock <BlobBaseClient> blockClient = new Mock <BlobBaseClient>(MockBehavior.Strict, new Uri("http://mock"), new BlobClientOptions());

            blockClient.SetupGet(c => c.ClientDiagnostics).CallBase();
            BlobProperties properties = new BlobProperties()
            {
                ContentLength = 100,
                ETag          = s_etag
            };

            SetupDownload(blockClient, dataSource);

            PartitionedDownloader downloader = new PartitionedDownloader(
                blockClient.Object,
                new StorageTransferOptions()
            {
                MaximumTransferLength = 10,
                InitialTransferLength = 10
            });

            Response result = await InvokeDownloadToAsync(downloader, stream);

            Assert.AreEqual(dataSource.Requests.Count, 10);
            AssertContent(100, stream);
            Assert.NotNull(result);

            bool first = true;

            foreach ((HttpRange Range, BlobRequestConditions Conditions)request in dataSource.Requests)
            {
                Assert.AreEqual(s_conditions.LeaseId, request.Conditions.LeaseId);
                Assert.AreEqual(s_conditions.IfModifiedSince, request.Conditions.IfModifiedSince);
                Assert.AreEqual(s_conditions.IfUnmodifiedSince, request.Conditions.IfUnmodifiedSince);
                Assert.AreEqual(s_conditions.IfNoneMatch, request.Conditions.IfNoneMatch);
                if (first)
                {
                    first = false;
                }
                else
                {
                    Assert.AreEqual(s_etag, request.Conditions.IfMatch);
                }
            }
        }
Exemplo n.º 10
0
            public async Task InitializeAsync(WebJobsTestEnvironment testEnvironment)
            {
                RandomNameResolver nameResolver = new RandomNameResolver();

                CacheMock = CreateMockFunctionDataCache();
                CacheMock
                .Setup(c => c.IsEnabled)
                .Returns(true);
                IFunctionDataCache cache = CacheMock.Object;

                Host = new HostBuilder()
                       .ConfigureDefaultTestHost <CacheableBlobsEndToEndTests>(b =>
                {
                    b.AddAzureStorageBlobs().AddAzureStorageQueues();
                    b.AddAzureStorageCoreServices();
                })
                       .ConfigureServices(services =>
                {
                    services.AddSingleton <INameResolver>(nameResolver)
                    .AddSingleton(cache);
                })
                       .Build();

                JobHost = Host.GetJobHost();

                BlobServiceClient = new BlobServiceClient(testEnvironment.PrimaryStorageAccountConnectionString);

                BlobContainer = BlobServiceClient.GetBlobContainerClient(nameResolver.ResolveInString(ContainerName));
                Assert.False(await BlobContainer.ExistsAsync());
                await BlobContainer.CreateAsync();

                OutputBlobContainer = BlobServiceClient.GetBlobContainerClient(nameResolver.ResolveInString(OutputContainerName));

                await Host.StartAsync();

                // Upload some test blobs
                BlockBlobClient blob = BlobContainer.GetBlockBlobClient(InputBlobName);
                await blob.UploadTextAsync(TestData);

                // Get information about the uploaded blob
                BlobProperties blobProperties = await blob.GetPropertiesAsync();

                string blobId      = blob.Uri.ToString();
                string blobVersion = blobProperties.ETag.ToString();

                _expectedBlobCacheKey = new FunctionDataCacheKey(blobId, blobVersion);
            }
        private void ListBlobsStr(string containerName, string blobPrefix, string delimiter, int maxResults, ref string marker, IList <string> tn, string basePath)
        {
            IList <object> blobList = new List <object>(ListBlobs(containerName, blobPrefix, delimiter, maxResults, ref marker));


            foreach (object o in blobList)
            {
                if (o is BlobProperties)
                {
                    BlobProperties bp = (BlobProperties)o;

                    string[] structureArr = bp.Name.Split(char.Parse(delimiter));

                    if (structureArr != null && structureArr.Length > 0)
                    {
                        if (tn != null)
                        {
                            tn.Add(basePath + delimiter + (structureArr[structureArr.Length - 1]));
                        }
                    }
                }
                else if (o is string)
                {
                    string   bPrefix      = (string)o;
                    string[] structureArr = bPrefix.Split(char.Parse(delimiter));

                    if (structureArr != null && structureArr.Length > 0)
                    {
                        string node = string.Empty;
                        string t1   = null;
                        if (structureArr.Length > 1)
                        {
                            node = structureArr[structureArr.Length - 2];
                            t1   = node;
                        }
                        else
                        {
                            node = structureArr[0];
                            t1   = node;
                        }


                        ListBlobsStr(containerName, bPrefix, delimiter, maxResults, ref marker, tn, t1);
                    }
                }
            }
        }
Exemplo n.º 12
0
        public static HttpWebRequest PutBlobRequest(BlobContext context, string containerName, string blobName,
                                                    BlobProperties properties, BlobType blobType, byte[] content, long pageBlobSize, AccessCondition accessCondition)
        {
            bool valid = BlobTests.ContainerNameValidator(containerName) &&
                         BlobTests.BlobNameValidator(blobName) &&
                         BlobTests.PutPropertiesValidator(properties) &&
                         BlobTestUtils.ContentValidator(content);

            bool fatal = !BlobTests.PutPropertiesValidator(properties);

            Uri              uri       = BlobTests.ConstructPutUri(context.Address, containerName, blobName);
            HttpWebRequest   request   = null;
            OperationContext opContext = new OperationContext();

            try
            {
                request = BlobHttpWebRequestFactory.Put(uri, context.Timeout, properties, blobType, pageBlobSize, accessCondition, opContext);
                if (fatal)
                {
                    Assert.Fail();
                }
            }
            catch (InvalidOperationException)
            {
                if (valid)
                {
                    Assert.Fail();
                }
            }
            if (valid)
            {
                Assert.IsNotNull(request);
                Assert.IsNotNull(request.Method);
                Assert.AreEqual("PUT", request.Method);
                BlobTestUtils.VersionHeader(request, false);
                BlobTestUtils.ContentTypeHeader(request, null);
                BlobTestUtils.ContentDispositionHeader(request, properties.ContentDisposition);
                BlobTestUtils.ContentEncodingHeader(request, properties.ContentEncoding);
                BlobTestUtils.ContentLanguageHeader(request, null);
                BlobTestUtils.ContentMd5Header(request, null);
                BlobTestUtils.CacheControlHeader(request, null);
                BlobTestUtils.BlobTypeHeader(request, properties.BlobType);
                BlobTestUtils.BlobSizeHeader(request, (properties.BlobType == BlobType.PageBlob) ? properties.Length : (long?)null);
            }
            return(request);
        }
Exemplo n.º 13
0
        private BlobInfo ConvertBlobToBlobInfo(BlobClient blob, BlobProperties props)
        {
            var absoluteUrl = blob.Uri;
            var relativeUrl = UrlHelperExtensions.Combine(GetContainerNameFromUrl(blob.Uri.ToString()), EscapeUri(blob.Name));
            var fileName    = Path.GetFileName(Uri.UnescapeDataString(blob.Name));
            var contentType = MimeTypeResolver.ResolveContentType(fileName);

            return(new BlobInfo
            {
                Url = absoluteUrl.ToString(),
                Name = fileName,
                ContentType = contentType,
                Size = props.ContentLength,
                ModifiedDate = props.LastModified.DateTime,
                RelativeUrl = relativeUrl
            });
        }
Exemplo n.º 14
0
        // *********************************************************
        // Create a new Blob in a Specific Blob Container
        // *********************************************************
        public void CreateBlob(string ContainerName, string FileName,
                               string BlobEndPoint, string Account, string SharedKey)
        {
            StorageAccountInfo AccountInfo =
                new StorageAccountInfo(new Uri(BlobEndPoint), null, Account, SharedKey);

            var blobStore = BlobStorage.Create(AccountInfo);

            var container = blobStore.GetBlobContainer(ContainerName);

            BlobProperties props = new BlobProperties(System.IO.Path.GetFileName(FileName));

            BlobContents blobContents =
                new BlobContents(new System.IO.FileStream(FileName, FileMode.Open));

            container.CreateBlob(props, blobContents, true);
        }
Exemplo n.º 15
0
        public async Task BlobProtocolPutGetBlockListCloudOwnerSync()
        {
            string blockId1 = Convert.ToBase64String(new byte[] { 99, 100, 101 });
            string blockId2 = Convert.ToBase64String(new byte[] { 102, 103, 104 });

            // use a unique name since temp blocks from previous runs can exist
            string                  blobName       = "blob2" + DateTime.UtcNow.Ticks;
            BlobProperties          blobProperties = new BlobProperties();
            List <PutBlockListItem> blocks         = new List <PutBlockListItem>();
            PutBlockListItem        block1         = new PutBlockListItem(blockId1, BlockSearchMode.Uncommitted);

            blocks.Add(block1);
            PutBlockListItem block2 = new PutBlockListItem(blockId2, BlockSearchMode.Uncommitted);

            blocks.Add(block2);
            try
            {
                await cloudOwnerSync.PutBlockScenarioTest(cloudSetup.ContainerName, blobName, blockId1, cloudSetup.LeaseId, cloudSetup.Content, null);

                await cloudOwnerSync.GetBlockListScenarioTest(cloudSetup.ContainerName, blobName, BlockListingFilter.All, cloudSetup.LeaseId, null, blockId1);

                await cloudOwnerSync.GetBlockListScenarioTest(cloudSetup.ContainerName, blobName, BlockListingFilter.Uncommitted, cloudSetup.LeaseId, null, blockId1);

                await cloudOwnerSync.GetBlockListScenarioTest(cloudSetup.ContainerName, blobName, BlockListingFilter.Committed, cloudSetup.LeaseId, null);

                await cloudOwnerSync.PutBlockScenarioTest(cloudSetup.ContainerName, blobName, blockId2, cloudSetup.LeaseId, cloudSetup.Content, null);

                await cloudOwnerSync.GetBlockListScenarioTest(cloudSetup.ContainerName, blobName, BlockListingFilter.All, cloudSetup.LeaseId, null, blockId1, blockId2);

                await cloudOwnerSync.GetBlockListScenarioTest(cloudSetup.ContainerName, blobName, BlockListingFilter.Uncommitted, cloudSetup.LeaseId, null, blockId1, blockId2);

                await cloudOwnerSync.GetBlockListScenarioTest(cloudSetup.ContainerName, blobName, BlockListingFilter.Committed, cloudSetup.LeaseId, null);

                await cloudOwnerSync.PutBlockListScenarioTest(cloudSetup.ContainerName, blobName, blocks, blobProperties, cloudSetup.LeaseId, null);

                await cloudOwnerSync.GetBlockListScenarioTest(cloudSetup.ContainerName, blobName, BlockListingFilter.All, cloudSetup.LeaseId, null, blockId1, blockId2);

                await cloudOwnerSync.GetBlockListScenarioTest(cloudSetup.ContainerName, blobName, BlockListingFilter.Uncommitted, cloudSetup.LeaseId, null);

                await cloudOwnerSync.GetBlockListScenarioTest(cloudSetup.ContainerName, blobName, BlockListingFilter.Committed, cloudSetup.LeaseId, null, blockId1, blockId2);
            }
            finally
            {
                await cloudOwnerSync.DeleteBlob(cloudSetup.ContainerName, blobName);
            }
        }
Exemplo n.º 16
0
        // Called by AzureBlobSyncProvider.InsertItem.
        internal SyncedBlobAttributes InsertFile(FileData fileData, string relativePath, Stream dataStream)
        {
            if (fileData.Name.Length > MaxFileNameLength)
            {
                throw new ApplicationException("Name Too Long");
            }

            string path = fileData.RelativePath.ToLower();

            path = path.Replace(@"\", "/");
            CloudBlob      blob           = Container.GetBlobReference(path);
            BlobProperties blobProperties = blob.Properties;
            DateTime       uninitTime     = blobProperties.LastModifiedUtc;

            SetupMetadata(blob.Metadata, fileData, relativePath);
            blobProperties.ContentType = LookupMimeType(Path.GetExtension(fileData.Name));

            if (fileData.IsDirectory)
            {
                // Directories have no stream
                dataStream = new MemoryStream();
            }

            // Specify an optimistic concurrency check to prevent races with other endpoints syncing at the same time.
            BlobRequestOptions opts = new BlobRequestOptions();

            opts.AccessCondition = AccessCondition.IfNotModifiedSince(uninitTime);

            try
            {
                blob.UploadFromStream(dataStream, opts);
            }
            catch (StorageException e)
            {
                if (e.ErrorCode == StorageErrorCode.BlobAlreadyExists || e.ErrorCode == StorageErrorCode.ConditionFailed)
                {
                    throw new ApplicationException("Concurrency Violation", e);
                }
                throw;
            }

            blobProperties = blob.Properties;
            SyncedBlobAttributes attributes = new SyncedBlobAttributes(blob.Uri.ToString(), blobProperties.LastModifiedUtc);

            return(attributes);
        }
Exemplo n.º 17
0
        static async Task SetMetadataAsync()
        {
            string p_key   = "ApplicationType";
            string p_Value = "IDM";
            BlobContainerClient containerClient = client.GetBlobContainerClient(containerName);
            BlobClient          blob            = containerClient.GetBlobClient(filename);
            BlobProperties      props           = blob.GetProperties();

            props.Metadata.Add(p_key, p_Value);

            IDictionary <string, string> dictObj = new Dictionary <string, string>();

            dictObj.Add(p_key, p_Value);
            await blob.SetMetadataAsync(dictObj);

            Console.WriteLine("\nMetadata set");
        }
        public static bool PutBlob(WindowsAzureStorageHelper storageHelper, string containerName, string blobName, string fileName, byte[] fileContents, bool overwrite, NameValueCollection metadata)
        {
            BlobProperties blobProperties = new BlobProperties(blobName);

            blobProperties.ContentType = WindowsAzureStorageHelper.GetContentTypeFromExtension(Path.GetExtension(fileName));
            blobProperties.Metadata    = metadata;


            BlobContents blobContents = null;
            bool         ret          = false;

            blobContents = new BlobContents(fileContents);
            ret          = storageHelper.CreateBlob(containerName, blobProperties, blobContents, overwrite);


            return(ret);
        }
Exemplo n.º 19
0
        internal bool UploadStream(string blobName, Stream output, bool overwrite)
        {
            BlobContainer container = GetContainer();

            try
            {
                output.Position = 0; //Rewind to start
                Log.Write(EventKind.Information, "Uploading contents of blob {0}", ContainerUrl + _PathSeparator + blobName);
                BlobProperties properties = new BlobProperties(blobName);
                return(container.CreateBlob(properties, new BlobContents(output), overwrite));
            }
            catch (StorageException se)
            {
                Log.Write(EventKind.Error, "Error uploading blob {0}: {1}", ContainerUrl + _PathSeparator + blobName, se.Message);
                throw;
            }
        }
Exemplo n.º 20
0
        public async Task HandleAsync_ShouldReturnFalseAndLogEvents_WhenPublishingThrows()
        {
            // Arrange
            var     blobUri          = new Uri(_expectedInboxUrl);
            JObject operationContext = JObject.Parse("{\"something\": \"something value\"}");
            var     topicEndpointUri = new Uri("https://www.topichost.com");
            var     testEvent        = new EventGridEvent
            {
                EventTime   = DateTime.UtcNow,
                EventType   = CustomEventTypes.RequestBlobCopy,
                DataVersion = "1.0",
                Data        = JsonConvert.SerializeObject(new RequestBlobCopyDTO {
                    SourceUri = blobUri, DestinationUri = blobUri, OperationContext = JObject.FromObject(operationContext)
                })
            };
            var blobBaseClient = new BlobBaseClient(blobUri);
            var blobProperties = new BlobProperties();

            // Arrange Mocks
            Mock.Get(_settingsProvider)
            .Setup(x => x.GetAppSettingsValue(Publishing.TopicOutboundEndpointSettingName))
            .Returns(topicEndpointUri.ToString());
            Mock.Get(_storageService)
            .Setup(x => x.GetBlobMetadataAsync(blobUri, It.IsAny <StorageClientProviderContext>()))
            .ReturnsAsync(new JObject());
            Mock.Get(_eventGridPublisher)
            .Setup(x => x.PublishEventToTopic(It.IsAny <EventGridEvent>()))
            .ThrowsAsync(new InvalidOperationException());

            // Act
            var handleAsyncResult = await _handler.HandleAsync(testEvent).ConfigureAwait(true);

            // Assert
            handleAsyncResult.ShouldBe(false);
            Mock.Get(_logger).Verify(x =>
                                     x.LogExceptionObject(LogEventIds.FailedCriticallyToPublishEvent,
                                                          It.IsAny <Exception>(), It.IsAny <object>()),
                                     Times.AtLeastOnce,
                                     "An exception should be logged when the publishing fails");
            Mock.Get(_logger).Verify(x =>
                                     x.LogEventObject(LogEventIds.FailedToAcknowledge,
                                                      It.IsAny <object>()),
                                     Times.Once,
                                     "An exception should be logged when the acknowledgement fails");
        }
Exemplo n.º 21
0
        public static HttpWebRequest PutBlobRequest(BlobContext context, string containerName, string blobName,
            BlobProperties properties, BlobType blobType, byte[] content, long pageBlobSize, AccessCondition accessCondition)
        {
            bool valid = BlobTests.ContainerNameValidator(containerName) &&
                BlobTests.BlobNameValidator(blobName) &&
                BlobTests.PutPropertiesValidator(properties) &&
                BlobTestUtils.ContentValidator(content);

            bool fatal = !BlobTests.PutPropertiesValidator(properties);

            Uri uri = BlobTests.ConstructPutUri(context.Address, containerName, blobName);
            HttpWebRequest request = null;
            OperationContext opContext = new OperationContext();
            try
            {
                request = BlobHttpWebRequestFactory.Put(uri, context.Timeout, properties, blobType, pageBlobSize, accessCondition, opContext);
                if (fatal)
                {
                    Assert.Fail();
                }
            }
            catch (InvalidOperationException)
            {
                if (valid)
                {
                    Assert.Fail();
                }
            }
            if (valid)
            {
                Assert.IsNotNull(request);
                Assert.IsNotNull(request.Method);
                Assert.AreEqual("PUT", request.Method);
                BlobTestUtils.VersionHeader(request, false);
                BlobTestUtils.ContentTypeHeader(request, null);
                BlobTestUtils.ContentDispositionHeader(request, properties.ContentDisposition);
                BlobTestUtils.ContentEncodingHeader(request, properties.ContentEncoding);
                BlobTestUtils.ContentLanguageHeader(request, null);
                BlobTestUtils.ContentMd5Header(request, null);
                BlobTestUtils.CacheControlHeader(request, null);
                BlobTestUtils.BlobTypeHeader(request, properties.BlobType);
                BlobTestUtils.BlobSizeHeader(request, (properties.BlobType == BlobType.PageBlob) ? properties.Length : (long?)null);
            }
            return request;
        }
Exemplo n.º 22
0
        /// <inheritdoc />
        public async Task <bool> WritePolicyAsync(string filepath, Stream fileStream)
        {
            try
            {
                BlobClient blockBlob = CreateBlobClient(filepath);

                await blockBlob.UploadAsync(fileStream, true);

                BlobProperties properties = await blockBlob.GetPropertiesAsync();

                return(properties.ContentLength > 0);
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to save policy file {filepath}. " + ex);
                throw;
            }
        }
Exemplo n.º 23
0
        public async Task <IDictionary <string, string> > GetBlobMetadataAsync(string blobUrl)
        {
            var blobUriBuilder = new BlobUriBuilder(
                new Uri(blobUrl));

            var blobContainerClient = _blobServiceClient
                                      .GetBlobContainerClient(blobUriBuilder.BlobContainerName);

            var blobClient = blobContainerClient.GetBlobClient(blobUriBuilder.BlobName);

            BlobDownloadInfo blobDownloadInfo = await blobClient.DownloadAsync();

            BlobProperties blobProperties = await blobClient
                                            .GetPropertiesAsync()
                                            .ConfigureAwait(false);

            return(blobProperties.Metadata);
        }
Exemplo n.º 24
0
        private static async Task SetBlobPropertiesAsync(string blobName)
        {
            var blobClient = new BlobClient(_connectionString, _blobContainerName, blobName);

            BlobProperties blobProperties = await blobClient.GetPropertiesAsync();

            var blobHttpHeaders = new BlobHttpHeaders
            {
                ContentLanguage = "en-us",

                CacheControl       = blobProperties.CacheControl,
                ContentDisposition = blobProperties.ContentDisposition,
                ContentEncoding    = blobProperties.ContentEncoding,
                ContentHash        = blobProperties.ContentHash
            };

            await blobClient.SetHttpHeadersAsync(blobHttpHeaders);
        }
Exemplo n.º 25
0
        private PutObjectRequest CreateUpload(string containerName, string blobName, Stream source,
                                              BlobProperties properties, bool closeStream)
        {
            var putRequest = new PutObjectRequest()
            {
                BucketName                 = _bucket,
                Key                        = GenerateKeyName(containerName, blobName),
                InputStream                = source,
                ContentType                = properties?.ContentType,
                CannedACL                  = GetCannedACL(properties),
                AutoCloseStream            = closeStream,
                ServerSideEncryptionMethod = _serverSideEncryptionMethod
            };

            putRequest.Headers.ContentDisposition = properties?.ContentDisposition;
            putRequest.Metadata.AddMetadata(properties?.Metadata);
            return(putRequest);
        }
Exemplo n.º 26
0
        public async Task Ctor_AzureSasCredential()
        {
            // Arrange
            await using DisposingContainer test = await GetTestContainerAsync();

            string sas    = GetContainerSas(test.Container.Name, BlobContainerSasPermissions.All).ToString();
            var    client = test.Container.GetBlobClient(GetNewBlobName());
            await client.UploadAsync(new MemoryStream());

            Uri blobUri = client.Uri;

            // Act
            var            sasClient      = InstrumentClient(new BlobClient(blobUri, new AzureSasCredential(sas), GetOptions()));
            BlobProperties blobProperties = await sasClient.GetPropertiesAsync();

            // Assert
            Assert.IsNotNull(blobProperties);
        }
        /// <summary>
        /// Constructs a web request to create or update a blob by committing a block list.
        /// </summary>
        /// <param name="uri">The absolute URI to the blob.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <param name="properties">The properties to set for the blob.</param>
        /// <param name="accessCondition">The access condition to apply to the request.</param>
        /// <returns>A web request for performing the operation.</returns>
        public static HttpRequestMessage PutBlockList(Uri uri, int?timeout, BlobProperties properties, AccessCondition accessCondition, HttpContent content, OperationContext operationContext)
        {
            UriQueryBuilder builder = new UriQueryBuilder();

            builder.Add(Constants.QueryConstants.Component, "blocklist");

            HttpRequestMessage request = HttpRequestMessageFactory.CreateRequestMessage(HttpMethod.Put, uri, timeout, builder, content, operationContext);

            request.AddOptionalHeader(Constants.HeaderConstants.CacheControlHeader, properties.CacheControl);
            request.AddOptionalHeader(Constants.HeaderConstants.ContentTypeHeader, properties.ContentType);
            request.AddOptionalHeader(Constants.HeaderConstants.BlobContentMD5Header, properties.ContentMD5);
            request.AddOptionalHeader(Constants.HeaderConstants.ContentLanguageHeader, properties.ContentLanguage);
            request.AddOptionalHeader(Constants.HeaderConstants.ContentEncodingHeader, properties.ContentEncoding);

            request.ApplyAccessCondition(accessCondition);

            return(request);
        }
Exemplo n.º 28
0
        public static Blob ToBlob(string containerName, string path, BlobProperties properties)
        {
            string GetFullName(string name) => containerName == null
            ? name
            : StoragePath.Combine(containerName, name);

            var blob = new Blob(GetFullName(path), BlobItemKind.File);

            blob.MD5  = properties.ContentHash.ToHexString();
            blob.Size = properties.ContentLength;
            blob.LastModificationTime = properties.LastModified;

            AddProperties(blob, properties);

            blob.Metadata.MergeRange(properties.Metadata);

            return(blob);
        }
Exemplo n.º 29
0
        public async Task StoreCreateSnapshot()
        {
            var random = new Random();
            var data   = new byte[64];

            random.NextBytes(data);
            var relPath     = Guid.NewGuid().ToString();
            var contentType = Guid.NewGuid().ToString();

            var p = new BlobProperties()
            {
                ContentType = Guid.NewGuid().ToString(),
                ContentMD5  = Guid.NewGuid().ToString(),
            };

            var item = Substitute.For <IStorageItem>();

            item.RelativePath.Returns(relPath);
            item.Data.Returns(data);
            item.ContentType.Returns(contentType);
            item.LoadMD5();
            item.Load();

            var container = Substitute.For <IContainer>();

            container.Save(relPath, data, contentType);
            container.Exists(relPath).Returns(Task.FromResult(true));
            container.Snapshot(relPath);
            container.Properties(relPath).Returns(Task.FromResult(p));

            var w = new BlobWriter(container, true);
            await w.Store(new[] { item });

            var x = item.Received().RelativePath;
            var y = item.Received().Data;
            var z = item.Received().ContentType;

            item.Received().LoadMD5();
            item.Received().Load();
            container.Received().Save(relPath, data, contentType);
            container.Received().Exists(relPath);
            container.Received().Snapshot(relPath);
            container.Received().Properties(relPath);
        }
Exemplo n.º 30
0
        private static async Task CopyBlob()
        {
            blobClient = containerClient.GetBlobClient(fileName);
            if (await blobClient.ExistsAsync())
            {
                BlobLeaseClient leaseClient = blobClient.GetBlobLeaseClient();

                // Specifying -1 for the lease interval creates an infinite lease.
                await leaseClient.AcquireAsync(TimeSpan.FromSeconds(-1));

                // Get the source blob's properties and display the lease state.
                BlobProperties sourceProperties = await blobClient.GetPropertiesAsync();

                Console.WriteLine($"Lease state: {sourceProperties.LeaseState}");

                // Get a BlobClient representing the destination blob with a unique name.
                BlobClient destBlob = containerClient.GetBlobClient(Guid.NewGuid() + "-" + blobClient.Name);

                // Start the copy operation.
                await destBlob.StartCopyFromUriAsync(blobClient.Uri);

                // Get the destination blob's properties and display the copy status.
                BlobProperties destProperties = await destBlob.GetPropertiesAsync();

                Console.WriteLine($"Copy status: {destProperties.CopyStatus}");
                Console.WriteLine($"Copy progress: {destProperties.CopyProgress}");
                Console.WriteLine($"Completion time: {destProperties.CopyCompletedOn}");
                Console.WriteLine($"Total bytes: {destProperties.ContentLength}");

                // Update the source blob's properties.
                sourceProperties = await blobClient.GetPropertiesAsync();

                if (sourceProperties.LeaseState == LeaseState.Leased)
                {
                    // Break the lease on the source blob.
                    await leaseClient.BreakAsync();

                    // Update the source blob's properties to check the lease state.
                    sourceProperties = await blobClient.GetPropertiesAsync();

                    Console.WriteLine($"Lease state: {sourceProperties.LeaseState}");
                }
            }
        }
Exemplo n.º 31
0
        /// <summary>
        /// Gets the blob's properties from the response.
        /// </summary>
        /// <param name="response">The web response.</param>
        /// <returns>The blob's properties.</returns>
        public static BlobProperties GetProperties(HttpWebResponse response)
        {
            BlobProperties properties = new BlobProperties();

            properties.LastModified    = response.LastModified.ToUniversalTime();
            properties.ContentEncoding = response.Headers[HttpResponseHeader.ContentEncoding];
            properties.ContentLanguage = response.Headers[HttpResponseHeader.ContentLanguage];
            properties.ContentMD5      = response.Headers[HttpResponseHeader.ContentMd5];
            properties.ContentType     = response.Headers[HttpResponseHeader.ContentType];
            properties.CacheControl    = response.Headers[HttpResponseHeader.CacheControl];
            properties.ETag            = HttpResponseParsers.GetETag(response);

            string blobType = response.Headers[Constants.HeaderConstants.BlobType];

            // Get blob type
            if (!string.IsNullOrEmpty(blobType))
            {
                properties.BlobType = (BlobType)Enum.Parse(typeof(BlobType), blobType, true);
            }

            // Get lease properties
            properties.LeaseStatus   = GetLeaseStatus(response);
            properties.LeaseState    = GetLeaseState(response);
            properties.LeaseDuration = GetLeaseDuration(response);

            // Get the content length. Prioritize range and x-ms over content length for the special cases.
            string rangeHeader         = response.Headers[HttpResponseHeader.ContentRange];
            string contentLengthHeader = response.Headers[Constants.HeaderConstants.ContentLengthHeader];

            if (!string.IsNullOrEmpty(rangeHeader))
            {
                properties.Length = long.Parse(rangeHeader.Split('/')[1]);
            }
            else if (!string.IsNullOrEmpty(contentLengthHeader))
            {
                properties.Length = long.Parse(contentLengthHeader);
            }
            else
            {
                properties.Length = response.ContentLength;
            }

            return(properties);
        }
        /// <summary>
        /// Constructs a web request to create or update a blob by committing a block list.
        /// </summary>
        /// <param name="uri">The absolute URI to the blob.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <param name="properties">The properties to set for the blob.</param>
        /// <param name="accessCondition">The access condition to apply to the request.</param>
        /// <param name="operationContext">An <see cref="OperationContext"/> object for tracking the current operation.</param>
        /// <returns>A web request for performing the operation.</returns>
        public static HttpWebRequest PutBlockList(Uri uri, int?timeout, BlobProperties properties, AccessCondition accessCondition, OperationContext operationContext)
        {
            CommonUtility.AssertNotNull("properties", properties);

            UriQueryBuilder builder = new UriQueryBuilder();

            builder.Add(Constants.QueryConstants.Component, "blocklist");

            HttpWebRequest request = HttpWebRequestFactory.CreateWebRequest(WebRequestMethods.Http.Put, uri, timeout, builder, operationContext);

            request.AddOptionalHeader(Constants.HeaderConstants.CacheControlHeader, properties.CacheControl);
            request.AddOptionalHeader(Constants.HeaderConstants.ContentTypeHeader, properties.ContentType);
            request.AddOptionalHeader(Constants.HeaderConstants.BlobContentMD5Header, properties.ContentMD5);
            request.AddOptionalHeader(Constants.HeaderConstants.BlobContentLanguageHeader, properties.ContentLanguage);
            request.AddOptionalHeader(Constants.HeaderConstants.ContentEncodingHeader, properties.ContentEncoding);

            request.ApplyAccessCondition(accessCondition);
            return(request);
        }
        /// <summary>
        /// Gets the blob's properties from the response.
        /// </summary>
        /// <param name="response">The web response.</param>
        /// <returns>The blob's properties.</returns>
        public static BlobProperties GetProperties(HttpWebResponse response)
        {
            BlobProperties properties = new BlobProperties();

            properties.LastModified = response.LastModified.ToUniversalTime();
            properties.ContentEncoding = response.Headers[HttpResponseHeader.ContentEncoding];
            properties.ContentLanguage = response.Headers[HttpResponseHeader.ContentLanguage];
            properties.ContentMD5 = response.Headers[HttpResponseHeader.ContentMd5];
            properties.ContentType = response.Headers[HttpResponseHeader.ContentType];
            properties.CacheControl = response.Headers[HttpResponseHeader.CacheControl];
            properties.ETag = HttpResponseParsers.GetETag(response);

            string blobType = response.Headers[Constants.HeaderConstants.BlobType];

            // Get blob type
            if (!string.IsNullOrEmpty(blobType))
            {
                properties.BlobType = (BlobType)Enum.Parse(typeof(BlobType), blobType, true);
            }

            // Get lease properties
            properties.LeaseStatus = GetLeaseStatus(response);
            properties.LeaseState = GetLeaseState(response);
            properties.LeaseDuration = GetLeaseDuration(response);

            // Get the content length. Prioritize range and x-ms over content length for the special cases.
            string rangeHeader = response.Headers[HttpResponseHeader.ContentRange];
            string contentLengthHeader = response.Headers[Constants.HeaderConstants.ContentLengthHeader];
            if (!string.IsNullOrEmpty(rangeHeader))
            {
                properties.Length = long.Parse(rangeHeader.Split('/')[1]);
            }
            else if (!string.IsNullOrEmpty(contentLengthHeader))
            {
                properties.Length = long.Parse(contentLengthHeader);
            }
            else
            {
                properties.Length = response.ContentLength;
            }

            return properties;
        }
Exemplo n.º 34
0
        public static async Task <bool> IsFileIdenticalToBlobAsync(this BlobClient client, string file)
        {
            BlobProperties properties = await client.GetPropertiesAsync();

            if (properties.ContentHash != null)
            {
                var localMD5 = CalculateMD5(file);
                var blobMD5  = Convert.ToBase64String(properties.ContentHash);
                return(blobMD5.Equals(localMD5, StringComparison.OrdinalIgnoreCase));
            }

            int bytesPerMegabyte = 1 * 1024 * 1024;

            using Stream localFileStream = File.OpenRead(file);
            byte[] localBuffer    = new byte[bytesPerMegabyte];
            byte[] remoteBuffer   = new byte[bytesPerMegabyte];
            int    localBytesRead = 0;

            do
            {
                long start = localFileStream.Position;
                localBytesRead = await localFileStream.ReadAsync(localBuffer, 0, bytesPerMegabyte);

                var range = new HttpRange(start, localBytesRead);
                BlobDownloadInfo download = await client.DownloadAsync(range).ConfigureAwait(false);

                if (download.ContentLength != localBytesRead)
                {
                    return(false);
                }

                using (var stream = new MemoryStream(remoteBuffer, true))
                {
                    await download.Content.CopyToAsync(stream).ConfigureAwait(false);
                }
                if (!remoteBuffer.SequenceEqual(localBuffer))
                {
                    return(false);
                }
            }while (localBytesRead > 0);

            return(true);
        }
Exemplo n.º 35
0
        public async void Test_Blob_ContentDisposition_Updated_Async()
        {
            var container = GetRandomContainerName();
            var blobName = GenerateRandomName();
            var filename = "testname.jpg";
            var contentType = "image/jpg";
            var newContentType = "image/png";
            var data = GenerateRandomBlobStream();

            await CreateNewObjectAsync(container, blobName, data, false, contentType);

            var newProps = new BlobProperties
            {
                ContentType = newContentType,
                Security = BlobSecurity.Public,                
            }.WithContentDispositionFilename(filename);

            await _provider.UpdateBlobPropertiesAsync(container, blobName, newProps);

            var meta = await _client.GetObjectMetadataAsync(Bucket, container + "/" + blobName);

            Assert.Equal(newProps.ContentDisposition, meta.Headers.ContentDisposition);
        }
 public void GetBlobScenarioTest(string containerName, string blobName, BlobProperties properties, string leaseId,
     byte[] content, HttpStatusCode? expectedError)
 {
     HttpWebRequest request = BlobTests.GetBlobRequest(BlobContext, containerName, blobName, AccessCondition.GenerateLeaseCondition(leaseId));
     Assert.IsTrue(request != null, "Failed to create HttpWebRequest");
     if (BlobContext.Credentials != null)
     {
         BlobTests.SignRequest(request, BlobContext);
     }
     HttpWebResponse response = BlobTestUtils.GetResponse(request, BlobContext);
     try
     {
         BlobTests.GetBlobResponse(response, BlobContext, properties, content, expectedError);
     }
     finally
     {
         response.Close();
     }
 }
        /// <summary>
        /// Constructs a web request to set system properties for a blob.
        /// </summary>
        /// <param name="uri">The absolute URI to the blob.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <param name="properties">The blob's properties.</param>
        /// <param name="accessCondition">The access condition to apply to the request.</param>
        /// <param name="operationContext">An <see cref="OperationContext"/> object for tracking the current operation.</param>
        /// <returns>A web request to use to perform the operation.</returns>
        public static HttpWebRequest SetProperties(Uri uri, int? timeout, BlobProperties properties, AccessCondition accessCondition, OperationContext operationContext)
        {
            UriQueryBuilder builder = new UriQueryBuilder();
            builder.Add(Constants.QueryConstants.Component, "properties");

            HttpWebRequest request = HttpWebRequestFactory.CreateWebRequest(WebRequestMethods.Http.Put, uri, timeout, builder, operationContext);
            request.ContentLength = 0;

            request.AddOptionalHeader(Constants.HeaderConstants.CacheControlHeader, properties.CacheControl);
            request.AddOptionalHeader(Constants.HeaderConstants.ContentEncodingHeader, properties.ContentEncoding);
            request.AddOptionalHeader(Constants.HeaderConstants.ContentLanguageHeader, properties.ContentLanguage);
            request.AddOptionalHeader(Constants.HeaderConstants.BlobContentMD5Header, properties.ContentMD5);
            request.AddOptionalHeader(Constants.HeaderConstants.ContentTypeHeader, properties.ContentType);

            request.ApplyAccessCondition(accessCondition);
            return request;
        }
        /// <summary>
        /// Constructs a web request to create or update a blob by committing a block list.
        /// </summary>
        /// <param name="uri">A <see cref="System.Uri"/> specifying the absolute URI to the blob.</param>
        /// <param name="timeout">An integer specifying the server timeout interval.</param>
        /// <param name="properties">A <see cref="BlobProperties"/> object specifying the properties to set for the blob.</param>
        /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the condition that must be met in order for the request to proceed.</param>
        /// <param name="useVersionHeader">A boolean value indicating whether to set the <i>x-ms-version</i> HTTP header.</param>
        /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
        /// <returns>A <see cref="System.Net.HttpWebRequest"/> object.</returns>
        public static HttpWebRequest PutBlockList(Uri uri, int? timeout, BlobProperties properties, AccessCondition accessCondition, bool useVersionHeader, OperationContext operationContext)
        {
            CommonUtility.AssertNotNull("properties", properties);

            UriQueryBuilder builder = new UriQueryBuilder();
            builder.Add(Constants.QueryConstants.Component, "blocklist");

            HttpWebRequest request = HttpWebRequestFactory.CreateWebRequest(WebRequestMethods.Http.Put, uri, timeout, builder, useVersionHeader, operationContext);

            if (properties != null)
            {
                request.AddOptionalHeader(Constants.HeaderConstants.BlobCacheControlHeader, properties.CacheControl);
                request.AddOptionalHeader(Constants.HeaderConstants.BlobContentTypeHeader, properties.ContentType);
                request.AddOptionalHeader(Constants.HeaderConstants.BlobContentMD5Header, properties.ContentMD5);
                request.AddOptionalHeader(Constants.HeaderConstants.BlobContentLanguageHeader, properties.ContentLanguage);
                request.AddOptionalHeader(Constants.HeaderConstants.BlobContentEncodingHeader, properties.ContentEncoding);
                request.AddOptionalHeader(Constants.HeaderConstants.BlobContentDispositionRequestHeader, properties.ContentDisposition);
            }

            request.ApplyAccessCondition(accessCondition);
            return request;
        }
        /// <summary>
        /// Constructs a web request to create or update a blob by committing a block list.
        /// </summary>
        /// <param name="uri">The absolute URI to the blob.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <param name="properties">The properties to set for the blob.</param>
        /// <param name="accessCondition">The access condition to apply to the request.</param>
        /// <returns>A web request for performing the operation.</returns>
        public static HttpRequestMessage PutBlockList(Uri uri, int? timeout, BlobProperties properties, AccessCondition accessCondition, HttpContent content, OperationContext operationContext)
        {
            UriQueryBuilder builder = new UriQueryBuilder();
            builder.Add(Constants.QueryConstants.Component, "blocklist");

            HttpRequestMessage request = HttpRequestMessageFactory.CreateRequestMessage(HttpMethod.Put, uri, timeout, builder, content, operationContext);

            request.AddOptionalHeader(Constants.HeaderConstants.CacheControlHeader, properties.CacheControl);
            request.AddOptionalHeader(Constants.HeaderConstants.ContentTypeHeader, properties.ContentType);
            request.AddOptionalHeader(Constants.HeaderConstants.BlobContentMD5Header, properties.ContentMD5);
            request.AddOptionalHeader(Constants.HeaderConstants.BlobContentLanguageHeader, properties.ContentLanguage);
            request.AddOptionalHeader(Constants.HeaderConstants.ContentEncodingHeader, properties.ContentEncoding);

            request.ApplyAccessCondition(accessCondition);

            return request;
        }
        /// <summary>
        /// Constructs a web request to create or update a blob by committing a block list.
        /// </summary>
        /// <param name="uri">The absolute URI to the blob.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <param name="properties">The properties to set for the blob.</param>
        /// <param name="accessCondition">The access condition to apply to the request.</param>
        /// <returns>A web request for performing the operation.</returns>
        public static StorageRequestMessage PutBlockList(Uri uri, int? timeout, BlobProperties properties, AccessCondition accessCondition, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
        {          
            UriQueryBuilder builder = new UriQueryBuilder();
            builder.Add(Constants.QueryConstants.Component, "blocklist");

            StorageRequestMessage request = HttpRequestMessageFactory.CreateRequestMessage(HttpMethod.Put, uri, timeout, builder, content, operationContext, canonicalizer, credentials);

            if (properties != null)
            {
                request.AddOptionalHeader(Constants.HeaderConstants.BlobCacheControlHeader, properties.CacheControl);
                request.AddOptionalHeader(Constants.HeaderConstants.BlobContentTypeHeader, properties.ContentType);
                request.AddOptionalHeader(Constants.HeaderConstants.BlobContentMD5Header, properties.ContentMD5);
                request.AddOptionalHeader(Constants.HeaderConstants.BlobContentLanguageHeader, properties.ContentLanguage);
                request.AddOptionalHeader(Constants.HeaderConstants.BlobContentEncodingHeader, properties.ContentEncoding);
                request.AddOptionalHeader(Constants.HeaderConstants.BlobContentDispositionRequestHeader, properties.ContentDisposition); 
            }

            request.ApplyAccessCondition(accessCondition);

            return request;
        }
        private PutObjectRequest CreateUpload(string containerName, string blobName, Stream source, BlobProperties properties, bool closeStream)
        {
            var putRequest = new PutObjectRequest()
            {
                BucketName = _bucket,
                Key = GenerateKeyName(containerName, blobName),
                InputStream = source,
                ContentType = properties?.ContentType,
                CannedACL = GetCannedACL(properties),
                AutoCloseStream = closeStream,
            };
            putRequest.Headers.ContentDisposition = properties?.ContentDisposition;
            putRequest.Metadata.AddMetadata(properties?.Metadata);

            return putRequest;
        }
 public void PutBlobScenarioTest(string containerName, string blobName, BlobProperties properties, BlobType blobType, byte[] content, HttpStatusCode? expectedError)
 {
     HttpWebRequest request = BlobTests.PutBlobRequest(BlobContext, containerName, blobName, properties, blobType, content, content.Length, null);
     Assert.IsTrue(request != null, "Failed to create HttpWebRequest");
     request.ContentLength = content.Length;
     if (BlobContext.Credentials != null)
     {
         BlobTests.SignRequest(request, BlobContext);
     }
     BlobTestUtils.SetRequest(request, BlobContext, content);
     HttpWebResponse response = BlobTestUtils.GetResponse(request, BlobContext);
     try
     {
         BlobTests.PutBlobResponse(response, BlobContext, expectedError);
     }
     finally
     {
         response.Close();
     }
 }
 public void BlobProtocolPutPageBlobCloudAnonAsync()
 {
     BlobProperties properties = new BlobProperties() { BlobType = BlobType.PageBlob };
     cloudAnonAsync.PutBlobScenarioTest(cloudSetup.ContainerName, Guid.NewGuid().ToString(),
         properties, BlobType.PageBlob, new byte[0], HttpStatusCode.NotFound);
 }
 public void BlobProtocolPutBlockBlobCloudAnonAsync()
 {
     byte[] content = new byte[6000];
     random.NextBytes(content);
     BlobProperties properties = new BlobProperties() { BlobType = BlobType.BlockBlob };
     cloudAnonAsync.PutBlobScenarioTest(cloudSetup.ContainerName, Guid.NewGuid().ToString(),
         properties, BlobType.BlockBlob, content, HttpStatusCode.NotFound);
 }
        //******************************
        //*                            *
        //*  BlobViewProperties_Click  *
        //*                            *
        //******************************
        // Display blob properties for selected blob.

        private void BlobViewProperties_Click(object sender, RoutedEventArgs e)
        {
            // Validate a single blob has been selected.

            if (ContainerListView.SelectedItems.Count != 1)
            {
                MessageBox.Show("In order to view or modify a blob's properties, please select one blob then click the View toolbar button", "Single Selection Requireed");
                return;
            }

            String blobName = (ContainerListView.SelectedItems[0] as BlobItem).Name;

            BlobProperties dlg = new BlobProperties();

            CloudBlobContainer container = blobClient.GetContainerReference(SelectedBlobContainer);

            ICloudBlob blob = container.GetBlobReferenceFromServer(blobName);
            if (blob.BlobType == BlobType.BlockBlob)
            {
                //CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
                CloudBlockBlob blockBlob = container.GetBlobReferenceFromServer(blobName) as CloudBlockBlob;
                Microsoft.WindowsAzure.Storage.Blob.BlobProperties props = blockBlob.Properties;
                dlg.ShowBlockBlob(blockBlob);
            }
            else if (blob.BlobType == BlobType.PageBlob)
            {
                CloudPageBlob pageBlob = container.GetBlobReferenceFromServer(blobName) as CloudPageBlob;
                Microsoft.WindowsAzure.Storage.Blob.BlobProperties props = pageBlob.Properties;
                dlg.ShowPageBlob(pageBlob);
            }

            if (dlg.ShowDialog().Value)
            {
                if (dlg.IsBlobChanged)
                {
                    ShowBlobContainer(SelectedBlobContainer);
                }
            }
        }
 /// <summary>
 /// Constructs a web request to create a new block blob or page blob, or to update the content 
 /// of an existing block blob. 
 /// </summary>
 /// <param name="uri">A <see cref="System.Uri"/> specifying the absolute URI to the blob.</param>
 /// <param name="timeout">An integer specifying the server timeout interval.</param>
 /// <param name="properties">A <see cref="BlobProperties"/> object.</param>
 /// <param name="blobType">A <see cref="BlobType"/> enumeration value.</param>
 /// <param name="pageBlobSize">For a page blob, the size of the blob. This parameter is ignored
 /// for block blobs.</param>
 /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the condition that must be met in order for the request to proceed.</param>
 /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
 /// <returns>A <see cref="System.Net.HttpWebRequest"/> object.</returns>
 public static HttpWebRequest Put(Uri uri, int? timeout, BlobProperties properties, BlobType blobType, long pageBlobSize, AccessCondition accessCondition, OperationContext operationContext)
 {
     return BlobHttpWebRequestFactory.Put(uri, timeout, properties, blobType, pageBlobSize, accessCondition, true /* useVersionHeader */, operationContext);
 }
        public void BlobProtocolPutGetBlockListCloudOwnerSync()
        {
            string blockId1 = Convert.ToBase64String(new byte[] { 99, 100, 101 });
            string blockId2 = Convert.ToBase64String(new byte[] { 102, 103, 104 });

            // use a unique name since temp blocks from previous runs can exist
            string blobName = "blob2" + DateTime.UtcNow.Ticks;
            BlobProperties blobProperties = new BlobProperties();
            List<PutBlockListItem> blocks = new List<PutBlockListItem>();
            PutBlockListItem block1 = new PutBlockListItem(blockId1, BlockSearchMode.Uncommitted);
            blocks.Add(block1);
            PutBlockListItem block2 = new PutBlockListItem(blockId2, BlockSearchMode.Uncommitted);
            blocks.Add(block2);
            try
            {
                cloudOwnerSync.PutBlockScenarioTest(cloudSetup.ContainerName, blobName, blockId1, cloudSetup.LeaseId, cloudSetup.Content, null);
                cloudOwnerSync.GetBlockListScenarioTest(cloudSetup.ContainerName, blobName, BlockListingFilter.All, cloudSetup.LeaseId, null, blockId1);
                cloudOwnerSync.GetBlockListScenarioTest(cloudSetup.ContainerName, blobName, BlockListingFilter.Uncommitted, cloudSetup.LeaseId, null, blockId1);
                cloudOwnerSync.GetBlockListScenarioTest(cloudSetup.ContainerName, blobName, BlockListingFilter.Committed, cloudSetup.LeaseId, null);

                cloudOwnerSync.PutBlockScenarioTest(cloudSetup.ContainerName, blobName, blockId2, cloudSetup.LeaseId, cloudSetup.Content, null);
                cloudOwnerSync.GetBlockListScenarioTest(cloudSetup.ContainerName, blobName, BlockListingFilter.All, cloudSetup.LeaseId, null, blockId1, blockId2);
                cloudOwnerSync.GetBlockListScenarioTest(cloudSetup.ContainerName, blobName, BlockListingFilter.Uncommitted, cloudSetup.LeaseId, null, blockId1, blockId2);
                cloudOwnerSync.GetBlockListScenarioTest(cloudSetup.ContainerName, blobName, BlockListingFilter.Committed, cloudSetup.LeaseId, null);

                cloudOwnerSync.PutBlockListScenarioTest(cloudSetup.ContainerName, blobName, blocks, blobProperties, cloudSetup.LeaseId, null);
                cloudOwnerSync.GetBlockListScenarioTest(cloudSetup.ContainerName, blobName, BlockListingFilter.All, cloudSetup.LeaseId, null, blockId1, blockId2);
                cloudOwnerSync.GetBlockListScenarioTest(cloudSetup.ContainerName, blobName, BlockListingFilter.Uncommitted, cloudSetup.LeaseId, null);
                cloudOwnerSync.GetBlockListScenarioTest(cloudSetup.ContainerName, blobName, BlockListingFilter.Committed, cloudSetup.LeaseId, null, blockId1, blockId2);
            }
            finally
            {
                cloudOwnerSync.DeleteBlob(cloudSetup.ContainerName, blobName);
            }
        }
        /// <summary>
        /// Gets the blob's properties from the response.
        /// </summary>
        /// <param name="response">The web response.</param>
        /// <returns>The blob's properties.</returns>
        public static BlobProperties GetProperties(HttpWebResponse response)
        {
            CommonUtility.AssertNotNull("response", response);

            BlobProperties properties = new BlobProperties();
            properties.ETag = HttpResponseParsers.GetETag(response);

#if WINDOWS_PHONE 
            properties.LastModified = HttpResponseParsers.GetLastModified(response);
            properties.ContentLanguage = response.Headers[Constants.HeaderConstants.ContentLanguageHeader];
#else
            properties.LastModified = response.LastModified.ToUniversalTime();
            properties.ContentLanguage = response.Headers[HttpResponseHeader.ContentLanguage];
#endif

            properties.ContentDisposition = response.Headers[Constants.HeaderConstants.ContentDispositionResponseHeader];
            properties.ContentEncoding = response.Headers[HttpResponseHeader.ContentEncoding];
            properties.ContentMD5 = response.Headers[HttpResponseHeader.ContentMd5];
            properties.ContentType = response.Headers[HttpResponseHeader.ContentType];
            properties.CacheControl = response.Headers[HttpResponseHeader.CacheControl];

            // Get blob type
            string blobType = response.Headers[Constants.HeaderConstants.BlobType];
            if (!string.IsNullOrEmpty(blobType))
            {
                properties.BlobType = (BlobType)Enum.Parse(typeof(BlobType), blobType, true);
            }

            // Get lease properties
            properties.LeaseStatus = GetLeaseStatus(response);
            properties.LeaseState = GetLeaseState(response);
            properties.LeaseDuration = GetLeaseDuration(response);

            // Get the content length. Prioritize range and x-ms over content length for the special cases.
            string rangeHeader = response.Headers[HttpResponseHeader.ContentRange];
            string contentLengthHeader = response.Headers[Constants.HeaderConstants.ContentLengthHeader];
            string blobContentLengthHeader = response.Headers[Constants.HeaderConstants.BlobContentLengthHeader];
            if (!string.IsNullOrEmpty(rangeHeader))
            {
                properties.Length = long.Parse(rangeHeader.Split('/')[1], CultureInfo.InvariantCulture);
            }
            else if (!string.IsNullOrEmpty(blobContentLengthHeader))
            {
                properties.Length = long.Parse(blobContentLengthHeader, CultureInfo.InvariantCulture);
            }
            else if (!string.IsNullOrEmpty(contentLengthHeader))
            {
                // On Windows Phone, ContentLength property is not always same as Content-Length header,
                // so we try to parse the header first.
                properties.Length = long.Parse(contentLengthHeader, CultureInfo.InvariantCulture);
            }
            else
            {
                properties.Length = response.ContentLength;
            }

            // Get sequence number
            string sequenceNumber = response.Headers[Constants.HeaderConstants.BlobSequenceNumber];
            if (!string.IsNullOrEmpty(sequenceNumber))
            {
                properties.PageBlobSequenceNumber = long.Parse(sequenceNumber, CultureInfo.InvariantCulture);
            }

            return properties;
        }
        public void CreateBlob(string containerName, string blobName, bool isPublic)
        {
            Properties = new BlobProperties() { BlobType = BlobType.BlockBlob };
            HttpWebRequest request = BlobTests.PutBlobRequest(BlobContext, containerName, blobName, Properties, BlobType.BlockBlob, Content, 0, null);
            Assert.IsTrue(request != null, "Failed to create HttpWebRequest");

            request.ContentLength = Content.Length;
            request.Timeout = 30000;
            if (BlobContext.Credentials != null)
            {
                BlobTests.SignRequest(request, BlobContext);
            }
            Stream stream = request.GetRequestStream();
            stream.Write(Content, 0, Content.Length);
            stream.Flush();
            stream.Close();
            HttpWebResponse response = BlobTestUtils.GetResponse(request, BlobContext);
            HttpStatusCode statusCode = response.StatusCode;
            string statusDescription = response.StatusDescription;
            response.Close();
            if (statusCode != HttpStatusCode.Created)
            {
                Assert.Fail(string.Format("Failed to create blob: {0}, Status: {1}, Status Description: {2}", containerName, statusCode, statusDescription));
            }
        }
Exemplo n.º 50
0
        /// <summary>
        /// Constructs a web request to set system properties for a blob.
        /// </summary>
        /// <param name="uri">The absolute URI to the blob.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <param name="properties">The blob's properties.</param>
        /// <param name="leaseId">The lease ID, if the blob has an active lease.</param>
        /// <param name="newBlobSize">The new blob size, if the blob is a page blob. Set this parameter to <c>null</c> to keep the existing blob size.</param>
        /// <returns>A web request to use to perform the operation.</returns>
        public static HttpWebRequest SetProperties(Uri uri, int timeout, BlobProperties properties, string leaseId, long? newBlobSize)
        {
            UriQueryBuilder builder = new UriQueryBuilder();
            builder.Add(Constants.QueryConstants.Component, "properties");

            HttpWebRequest request = CreateWebRequest(uri, timeout, builder);

            request.ContentLength = 0;
            request.Method = "PUT";

            Request.AddLeaseId(request, leaseId);

            if (newBlobSize.HasValue)
            {
                request.Headers.Add(Constants.HeaderConstants.Size, newBlobSize.Value.ToString(NumberFormatInfo.InvariantInfo));
                properties.Length = newBlobSize.Value;
            }

            Request.AddOptionalHeader(request, Constants.HeaderConstants.CacheControlHeader, properties.CacheControl);
            Request.AddOptionalHeader(request, Constants.HeaderConstants.ContentEncodingHeader, properties.ContentEncoding);
            Request.AddOptionalHeader(request, Constants.HeaderConstants.ContentLanguageHeader, properties.ContentLanguage);
            Request.AddOptionalHeader(request, Constants.HeaderConstants.BlobContentMD5Header, properties.ContentMD5);
            Request.AddOptionalHeader(request, Constants.HeaderConstants.ContentTypeHeader, properties.ContentType);

            return request;
        }
        public void ClearPageRangeScenarioTest(string containerName, string blobName, HttpStatusCode? expectedError)
        {
            // 1. Create Sparse Page Blob
            int blobSize = 128 * 1024;

            BlobProperties properties = new BlobProperties() { BlobType = BlobType.PageBlob };
            Uri uri = BlobTests.ConstructPutUri(BlobContext.Address, containerName, blobName);
            OperationContext opContext = new OperationContext();
            HttpWebRequest webRequest = BlobHttpWebRequestFactory.Put(uri, BlobContext.Timeout, properties, BlobType.PageBlob, blobSize, null, opContext);

            BlobTests.SignRequest(webRequest, BlobContext);

            using (HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse)
            {
                BlobTests.PutBlobResponse(response, BlobContext, expectedError);
            }

            // 2. Now upload some page ranges
            for (int m = 0; m * 512 * 4 < blobSize; m++)
            {
                int startOffset = 512 * 4 * m;
                int length = 512;
                
                PageRange range = new PageRange(startOffset, startOffset + length - 1);
                opContext = new OperationContext();
                HttpWebRequest pageRequest = BlobHttpWebRequestFactory.PutPage(uri, BlobContext.Timeout, range, PageWrite.Update, null, opContext);
                pageRequest.ContentLength = 512;
                BlobTests.SignRequest(pageRequest, BlobContext);

                Stream outStream = pageRequest.GetRequestStream();

                for (int n = 0; n < 512; n++)
                {
                    outStream.WriteByte((byte)m);
                }

                outStream.Close();
                using (HttpWebResponse pageResponse = pageRequest.GetResponse() as HttpWebResponse)
                {
                }
            }

            // 3. Now do a Get Page Ranges
            List<PageRange> pageRanges = new List<PageRange>();
            opContext = new OperationContext();
            HttpWebRequest pageRangeRequest = BlobHttpWebRequestFactory.GetPageRanges(uri, BlobContext.Timeout, null, null, null, null, opContext);
            BlobTests.SignRequest(pageRangeRequest, BlobContext);
            using (HttpWebResponse pageRangeResponse = pageRangeRequest.GetResponse() as HttpWebResponse)
            {
                GetPageRangesResponse getPageRangesResponse = new GetPageRangesResponse(pageRangeResponse.GetResponseStream());
                pageRanges.AddRange(getPageRangesResponse.PageRanges.ToList());
            }

            // 4. Now Clear some pages
            bool skipFlag = false;
            foreach (PageRange pRange in pageRanges)
            {
                skipFlag = !skipFlag;
                if (skipFlag)
                {
                    continue;
                }

                opContext = new OperationContext();
                HttpWebRequest clearPageRequest = BlobHttpWebRequestFactory.PutPage(uri, BlobContext.Timeout, pRange, PageWrite.Clear, null, opContext);
                clearPageRequest.ContentLength = 0;
                BlobTests.SignRequest(clearPageRequest, BlobContext);
                using (HttpWebResponse clearResponse = clearPageRequest.GetResponse() as HttpWebResponse)
                {
                }
            }

            // 5. Get New Page ranges and verify
            List<PageRange> newPageRanges = new List<PageRange>();

            opContext = new OperationContext();
            HttpWebRequest newPageRangeRequest = BlobHttpWebRequestFactory.GetPageRanges(uri, BlobContext.Timeout, null, null, null, null, opContext);
            BlobTests.SignRequest(newPageRangeRequest, BlobContext);
            using (HttpWebResponse newPageRangeResponse = newPageRangeRequest.GetResponse() as HttpWebResponse)
            {
                GetPageRangesResponse getNewPageRangesResponse = new GetPageRangesResponse(newPageRangeResponse.GetResponseStream());
                newPageRanges.AddRange(getNewPageRangesResponse.PageRanges.ToList());
            }

            Assert.AreEqual(pageRanges.Count(), newPageRanges.Count() * 2);
            for (int l = 0; l < newPageRanges.Count(); l++)
            {
                Assert.AreEqual(pageRanges[2 * l].StartOffset, newPageRanges[l].StartOffset);
                Assert.AreEqual(pageRanges[2 * l].EndOffset, newPageRanges[l].EndOffset);
            }
        }
        /// <summary>
        /// Constructs a web request to create a new block blob or page blob, or to update the content 
        /// of an existing block blob. 
        /// </summary>
        /// <param name="uri">A <see cref="System.Uri"/> specifying the absolute URI to the blob.</param>
        /// <param name="timeout">An integer specifying the server timeout interval.</param>
        /// <param name="properties">A <see cref="BlobProperties"/> object.</param>
        /// <param name="blobType">A <see cref="BlobType"/> enumeration value.</param>
        /// <param name="pageBlobSize">For a page blob, the size of the blob. This parameter is ignored
        /// for block blobs.</param>
        /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the condition that must be met in order for the request to proceed.</param>
        /// <param name="useVersionHeader">A boolean value indicating whether to set the <i>x-ms-version</i> HTTP header.</param>
        /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
        /// <returns>A <see cref="System.Net.HttpWebRequest"/> object.</returns>
        public static HttpWebRequest Put(Uri uri, int? timeout, BlobProperties properties, BlobType blobType, long pageBlobSize, AccessCondition accessCondition, bool useVersionHeader, OperationContext operationContext)
        {
            CommonUtility.AssertNotNull("properties", properties);

            if (blobType == BlobType.Unspecified)
            {
                throw new InvalidOperationException(SR.UndefinedBlobType);
            }

            HttpWebRequest request = HttpWebRequestFactory.CreateWebRequest(WebRequestMethods.Http.Put, uri, timeout, null /* builder */, useVersionHeader, operationContext);

            if (properties.CacheControl != null)
            {
                request.Headers[HttpRequestHeader.CacheControl] = properties.CacheControl;
            }

            if (properties.ContentType != null)
            {
                // Setting it using Headers is an exception
                request.ContentType = properties.ContentType;
            }

            if (properties.ContentMD5 != null)
            {
                request.Headers[HttpRequestHeader.ContentMd5] = properties.ContentMD5;
            }

            if (properties.ContentLanguage != null)
            {
                request.Headers[HttpRequestHeader.ContentLanguage] = properties.ContentLanguage;
            }

            if (properties.ContentEncoding != null)
            {
                request.Headers[HttpRequestHeader.ContentEncoding] = properties.ContentEncoding;
            }

            if (properties.ContentDisposition != null)
            {
                request.Headers[Constants.HeaderConstants.BlobContentDispositionRequestHeader] = properties.ContentDisposition;
            }

            if (blobType == BlobType.PageBlob)
            {
                request.Headers[Constants.HeaderConstants.BlobType] = Constants.HeaderConstants.PageBlob;
                request.Headers[Constants.HeaderConstants.BlobContentLengthHeader] = pageBlobSize.ToString(NumberFormatInfo.InvariantInfo);
                properties.Length = pageBlobSize;
            }
            else if (blobType == BlobType.BlockBlob)
            {
                request.Headers[Constants.HeaderConstants.BlobType] = Constants.HeaderConstants.BlockBlob;
            }
            else
            {
                request.Headers[Constants.HeaderConstants.BlobType] = Constants.HeaderConstants.AppendBlob;
            }

            request.ApplyAccessCondition(accessCondition);

            return request;
        }
 public void PutBlockListScenarioTest(string containerName, string blobName, List<PutBlockListItem> blocks, BlobProperties blobProperties, string leaseId, HttpStatusCode? expectedError)
 {
     HttpWebRequest request = BlobTests.PutBlockListRequest(BlobContext, containerName, blobName, blobProperties, AccessCondition.GenerateLeaseCondition(leaseId));
     Assert.IsTrue(request != null, "Failed to create HttpWebRequest");
     byte[] content;
     using (MemoryStream stream = new MemoryStream())
     {
         BlobRequest.WriteBlockListBody(blocks, stream);
         stream.Seek(0, SeekOrigin.Begin);
         content = new byte[stream.Length];
         stream.Read(content, 0, content.Length);
     }
     request.ContentLength = content.Length;
     if (BlobContext.Credentials != null)
     {
         BlobTests.SignRequest(request, BlobContext);
     }
     BlobTestUtils.SetRequest(request, BlobContext, content);
     HttpWebResponse response = BlobTestUtils.GetResponse(request, BlobContext);
     try
     {
         BlobTests.PutBlockListResponse(response, BlobContext, expectedError);
     }
     finally
     {
         response.Close();
     }
 }
Exemplo n.º 54
0
 public static void GetBlobResponse(HttpWebResponse response, BlobContext context, BlobProperties properties, byte[] content, HttpStatusCode? expectedError)
 {
     Assert.IsNotNull(response);
     if (expectedError == null)
     {
         Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
         BlobTestUtils.LastModifiedHeader(response);
         BlobTestUtils.ContentLengthHeader(response, content.Length);
         BlobTestUtils.ETagHeader(response);
         BlobTestUtils.RequestIdHeader(response);
         BlobTestUtils.Contents(response, content);
     }
     else
     {
         Assert.AreEqual(expectedError, response.StatusCode, response.StatusDescription);
     }
     response.Close();
 }
Exemplo n.º 55
0
        /// <summary>
        /// Constructs a web request to create or update a blob by committing a block list.
        /// </summary>
        /// <param name="uri">The absolute URI to the blob.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <param name="properties">The properties to set for the blob.</param>
        /// <param name="leaseId">The lease ID, if the blob has an active lease.</param>
        /// <returns>A web request for performing the operation.</returns>
        public static HttpWebRequest PutBlockList(
            Uri uri,
            int timeout,
            BlobProperties properties,
            string leaseId)
        {
            UriQueryBuilder builder = new UriQueryBuilder();
            builder.Add(Constants.QueryConstants.Component, "blocklist");

            HttpWebRequest request = CreateWebRequest(uri, timeout, builder);

            request.Method = "PUT";

            Request.AddLeaseId(request, leaseId);

            Request.AddOptionalHeader(request, Constants.HeaderConstants.CacheControlHeader, properties.CacheControl);
            Request.AddOptionalHeader(request, Constants.HeaderConstants.ContentTypeHeader, properties.ContentType);
            Request.AddOptionalHeader(request, Constants.HeaderConstants.BlobContentMD5Header, properties.ContentMD5);
            Request.AddOptionalHeader(request, Constants.HeaderConstants.ContentLanguageHeader, properties.ContentLanguage);
            Request.AddOptionalHeader(request, Constants.HeaderConstants.ContentEncodingHeader, properties.ContentEncoding);

            return request;
        }
Exemplo n.º 56
0
 public static HttpWebRequest PutBlockListRequest(BlobContext context, string containerName, string blobName, BlobProperties blobProperties, AccessCondition accessCondition)
 {
     Uri uri = BlobTests.ConstructPutUri(context.Address, containerName, blobName);
     HttpWebRequest request = null;
     OperationContext opContext = new OperationContext();
     request = BlobHttpWebRequestFactory.PutBlockList(uri, context.Timeout, blobProperties, accessCondition, opContext);
     Assert.IsNotNull(request);
     Assert.IsNotNull(request.Method);
     Assert.AreEqual("PUT", request.Method);
     BlobTestUtils.VersionHeader(request, false);
     BlobTestUtils.ContentLanguageHeader(request, null);
     BlobTestUtils.ContentMd5Header(request, null);
     return request;
 }
        /// <summary>
        /// Gets the blob's properties from the response.
        /// </summary>
        /// <param name="response">The web response.</param>
        /// <returns>The blob's properties.</returns>
        public static BlobProperties GetProperties(HttpResponseMessage response)
        {
            BlobProperties properties = new BlobProperties();

            if (response.Content != null)
            {
                properties.LastModified = response.Content.Headers.LastModified;
                properties.ContentEncoding = HttpWebUtility.CombineHttpHeaderValues(response.Content.Headers.ContentEncoding);
                properties.ContentLanguage = HttpWebUtility.CombineHttpHeaderValues(response.Content.Headers.ContentLanguage);

                if (response.Content.Headers.ContentDisposition != null)
                {
                    properties.ContentDisposition = response.Content.Headers.ContentDisposition.ToString();
                }
                
                if (response.Content.Headers.ContentMD5 != null)
                {
                    properties.ContentMD5 = Convert.ToBase64String(response.Content.Headers.ContentMD5);
                }

                if (response.Content.Headers.ContentType != null)
                {
                    properties.ContentType = response.Content.Headers.ContentType.ToString();
                }

                // Get the content length. Prioritize range and x-ms over content length for the special cases.
                string contentLengthHeader = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.BlobContentLengthHeader);
                if ((response.Content.Headers.ContentRange != null) &&
                    response.Content.Headers.ContentRange.HasLength)
                {
                    properties.Length = response.Content.Headers.ContentRange.Length.Value;
                }
                else if (!string.IsNullOrEmpty(contentLengthHeader))
                {
                    properties.Length = long.Parse(contentLengthHeader);
                }
                else if (response.Content.Headers.ContentLength.HasValue)
                {
                    properties.Length = response.Content.Headers.ContentLength.Value;
                }
            }

            if (response.Headers.CacheControl != null)
            {
                properties.CacheControl = response.Headers.CacheControl.ToString();
            }

            if (response.Headers.ETag != null)
            {
                properties.ETag = response.Headers.ETag.ToString();
            }

            // Get blob type
            string blobType = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.BlobType);
            if (!string.IsNullOrEmpty(blobType))
            {
                properties.BlobType = (BlobType)Enum.Parse(typeof(BlobType), blobType, true);
            }

            // Get lease properties
            properties.LeaseStatus = GetLeaseStatus(response);
            properties.LeaseState = GetLeaseState(response);
            properties.LeaseDuration = GetLeaseDuration(response);

            // Get sequence number
            string sequenceNumber = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.BlobSequenceNumber);
            if (!string.IsNullOrEmpty(sequenceNumber))
            {
                properties.PageBlobSequenceNumber = long.Parse(sequenceNumber);
            }

            return properties;
        }
        /// <summary>
        /// Constructs a web request to create a new block blob or page blob, or to update the content 
        /// of an existing block blob. 
        /// </summary>
        /// <param name="uri">The absolute URI to the blob.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <param name="properties">The properties to set for the blob.</param>
        /// <param name="blobType">The type of the blob.</param>
        /// <param name="pageBlobSize">For a page blob, the size of the blob. This parameter is ignored
        /// for block blobs.</param>
        /// <param name="accessCondition">The access condition to apply to the request.</param>
        /// <param name="operationContext">An <see cref="OperationContext"/> object for tracking the current operation.</param>
        /// <returns>A web request to use to perform the operation.</returns>
        public static HttpWebRequest Put(Uri uri, int? timeout, BlobProperties properties, BlobType blobType, long pageBlobSize, AccessCondition accessCondition, OperationContext operationContext)
        {
            if (blobType == BlobType.Unspecified)
            {
                throw new InvalidOperationException(SR.UndefinedBlobType);
            }

            HttpWebRequest request = HttpWebRequestFactory.CreateWebRequest(WebRequestMethods.Http.Put, uri, timeout, null /* builder */, operationContext);

            if (properties.CacheControl != null)
            {
                request.Headers.Add(HttpRequestHeader.CacheControl, properties.CacheControl);
            }

            if (properties.ContentType != null)
            {
                // Setting it using Headers is an exception
                request.ContentType = properties.ContentType;
            }

            if (properties.ContentMD5 != null)
            {
                request.Headers.Add(HttpRequestHeader.ContentMd5, properties.ContentMD5);
            }

            if (properties.ContentLanguage != null)
            {
                request.Headers.Add(HttpRequestHeader.ContentLanguage, properties.ContentLanguage);
            }

            if (properties.ContentEncoding != null)
            {
                request.Headers.Add(HttpRequestHeader.ContentEncoding, properties.ContentEncoding);
            }

            if (blobType == BlobType.PageBlob)
            {
                request.ContentLength = 0;
                request.Headers.Add(Constants.HeaderConstants.BlobType, Constants.HeaderConstants.PageBlob);
                request.Headers.Add(Constants.HeaderConstants.Size, pageBlobSize.ToString(NumberFormatInfo.InvariantInfo));
                properties.Length = pageBlobSize;
            }
            else
            {
                request.Headers.Add(Constants.HeaderConstants.BlobType, Constants.HeaderConstants.BlockBlob);
            }

            request.ApplyAccessCondition(accessCondition);

            return request;
        }
 /// <summary>
 /// Constructs a web request to create or update a blob by committing a block list.
 /// </summary>
 /// <param name="uri">A <see cref="System.Uri"/> specifying the absolute URI to the blob.</param>
 /// <param name="timeout">An integer specifying the server timeout interval.</param>
 /// <param name="properties">A <see cref="BlobProperties"/> object specifying the properties to set for the blob.</param>
 /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the condition that must be met in order for the request to proceed.</param>
 /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
 /// <returns>A <see cref="System.Net.HttpWebRequest"/> object.</returns>
 public static HttpWebRequest PutBlockList(Uri uri, int? timeout, BlobProperties properties, AccessCondition accessCondition, OperationContext operationContext)
 {
     return BlobHttpWebRequestFactory.PutBlockList(uri, timeout, properties, accessCondition, true /* useVersionHeader */, operationContext);
 }
Exemplo n.º 60
0
        public static bool PutPropertiesValidator(BlobProperties properties)
        {
            if (properties.BlobType == BlobType.Unspecified)
            {
                return false;
            }

            if ((properties.BlobType == BlobType.PageBlob) &&
                (properties.Length % 512 != 0) &&
                (properties.Length > 0))
            {
                return false;
            }

            return true;
        }