/// <summary>
 /// Fills the meta.
 /// </summary>
 /// <param name="meta">The meta.</param>
 /// <param name="blobProperties">The BLOB properties.</param>
 public static void FillMeta(this BinaryStorageMetaData meta, BlobProperties blobProperties)
 {
     if (meta != null && blobProperties != null)
     {
         meta.Hash = blobProperties.ContentMD5;
         meta.Mime = blobProperties.ContentType;
         meta.Name = blobProperties.ContentDisposition.ConvertContentDispositionToName();
     }
 }
예제 #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BlobProperties"/> class based on an existing instance.
 /// </summary>
 /// <param name="other">The set of properties to clone.</param>
 public BlobProperties(BlobProperties other)
 {
     this.BlobType = other.BlobType;
     this.ContentEncoding = other.ContentEncoding;
     this.ContentLanguage = other.ContentLanguage;
     this.Length = other.Length;
     this.ContentType = other.ContentType;
     this.ETag = other.ETag;
     this.LastModified = other.LastModified;
     this.LeaseStatus = other.LeaseStatus;
 }
예제 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BlobProperties"/> class based on an existing instance.
        /// </summary>
        /// <param name="other">The set of properties to clone.</param>
        /// <remarks>Lease-related properties will not be cloned, because a lease associated with the base blob is not copied to the snapshot.</remarks>
        public BlobProperties(BlobProperties other)
        {
            CommonUtility.AssertNotNull("other", other);

            this.BlobType = other.BlobType;
            this.ContentType = other.ContentType;
            this.ContentEncoding = other.ContentEncoding;
            this.ContentLanguage = other.ContentLanguage;
            this.CacheControl = other.CacheControl;
            this.ContentMD5 = other.ContentMD5;
            this.Length = other.Length;
            this.ETag = other.ETag;
            this.LastModified = other.LastModified;
            this.PageBlobSequenceNumber = other.PageBlobSequenceNumber;
        }
예제 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BlobProperties"/> class based on an existing instance.
        /// </summary>
        /// <param name="other">The set of properties to clone.</param>
        /// <remarks>Lease-related properties will not be cloned, because a lease associated with the base blob is not copied to the snapshot.</remarks>
        public BlobProperties(BlobProperties other)
        {
            CommonUtility.AssertNotNull("other", other);

            this.BlobType           = other.BlobType;
            this.ContentType        = other.ContentType;
            this.ContentDisposition = other.ContentDisposition;
            this.ContentEncoding    = other.ContentEncoding;
            this.ContentLanguage    = other.ContentLanguage;
            this.CacheControl       = other.CacheControl;
            this.ContentMD5         = other.ContentMD5;
            this.Length             = other.Length;
            this.ETag                   = other.ETag;
            this.LastModified           = other.LastModified;
            this.PageBlobSequenceNumber = other.PageBlobSequenceNumber;
        }
예제 #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BlobProperties"/> class based on an existing instance.
        /// </summary>
        /// <param name="other">A <see cref="BlobProperties"/> object.</param>
        /// <remarks>Lease-related properties will not be cloned, because a lease associated with the base blob is not copied to the snapshot.</remarks>
        public BlobProperties(BlobProperties other)
        {
            CommonUtility.AssertNotNull("other", other);

            this.BlobType = other.BlobType;
            this.ContentType = other.ContentType;
            this.ContentDisposition = other.ContentDisposition;
            this.ContentEncoding = other.ContentEncoding;
            this.ContentLanguage = other.ContentLanguage;
            this.CacheControl = other.CacheControl;
            this.ContentMD5 = other.ContentMD5;
            this.Length = other.Length;
            this.ETag = other.ETag;
            this.LastModified = other.LastModified;
            this.PageBlobSequenceNumber = other.PageBlobSequenceNumber;
            this.AppendBlobCommittedBlockCount = other.AppendBlobCommittedBlockCount;
            this.IsServerEncrypted = other.IsServerEncrypted;
        }
        /// <summary>
        /// Initializes a new instance of the BlobReadStreamBase class.
        /// </summary>
        /// <param name="blob">Blob reference to read from</param>
        /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the condition that must be met in order for the request to proceed. If <c>null</c>, no condition is used.</param>
        /// <param name="options">A <see cref="BlobRequestOptions"/> object that specifies additional options for the request.</param>
        /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
        protected BlobReadStreamBase(CloudBlob blob, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
        {
            if (options.UseTransactionalMD5.Value)
            {
                CommonUtility.AssertInBounds("StreamMinimumReadSizeInBytes", blob.StreamMinimumReadSizeInBytes, 1, Constants.MaxRangeGetContentMD5Size);
            }

            this.blob = blob;
            this.blobProperties = new BlobProperties(blob.Properties);
            this.currentOffset = 0;
            this.streamMinimumReadSizeInBytes = this.blob.StreamMinimumReadSizeInBytes;
            this.internalBuffer = new MultiBufferMemoryStream(blob.ServiceClient.BufferManager);
            this.accessCondition = accessCondition;
            this.options = options;
            this.operationContext = operationContext;
            this.blobMD5 = (this.options.DisableContentMD5Validation.Value || string.IsNullOrEmpty(this.blobProperties.ContentMD5)) ? null : new MD5Wrapper();
            this.lastException = null;
        }
예제 #7
0
        /// <summary>
        /// Initializes a new instance of the BlobReadStreamBase class.
        /// </summary>
        /// <param name="blob">Blob reference to read from</param>
        /// <param name="accessCondition">An object that represents the access conditions for the blob. If null, no condition is used.</param>
        /// <param name="options">An object that specifies additional options for the request.</param>
        /// <param name="operationContext">An <see cref="OperationContext"/> object for tracking the current operation.</param>
        protected BlobReadStreamBase(ICloudBlob blob, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
        {
            if (options.UseTransactionalMD5.Value)
            {
                CommonUtility.AssertInBounds("StreamMinimumReadSizeInBytes", blob.StreamMinimumReadSizeInBytes, 1, Constants.MaxRangeGetContentMD5Size);
            }

            this.blob           = blob;
            this.blobProperties = new BlobProperties(blob.Properties);
            this.currentOffset  = 0;
            this.streamMinimumReadSizeInBytes = this.blob.StreamMinimumReadSizeInBytes;
            this.internalBuffer   = new MemoryStream(this.streamMinimumReadSizeInBytes);
            this.accessCondition  = accessCondition;
            this.options          = options;
            this.operationContext = operationContext;
            this.blobMD5          = (this.options.DisableContentMD5Validation.Value || string.IsNullOrEmpty(this.blobProperties.ContentMD5)) ? null : new MD5Wrapper();
            this.lastException    = null;
        }
예제 #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BlobProperties"/> class based on an existing instance.
        /// </summary>
        /// <param name="other">A <see cref="BlobProperties"/> object.</param>
        /// <remarks>Lease-related properties will not be cloned, because a lease associated with the base blob is not copied to the snapshot.</remarks>
        public BlobProperties(BlobProperties other)
        {
            CommonUtility.AssertNotNull("other", other);

            this.BlobType           = other.BlobType;
            this.ContentType        = other.ContentType;
            this.ContentDisposition = other.ContentDisposition;
            this.ContentEncoding    = other.ContentEncoding;
            this.ContentLanguage    = other.ContentLanguage;
            this.CacheControl       = other.CacheControl;
            this.ContentMD5         = other.ContentMD5;
            this.Length             = other.Length;
            this.ETag                          = other.ETag;
            this.LastModified                  = other.LastModified;
            this.PageBlobSequenceNumber        = other.PageBlobSequenceNumber;
            this.AppendBlobCommittedBlockCount = other.AppendBlobCommittedBlockCount;
            this.IsServerEncrypted             = other.IsServerEncrypted;
            this.IsIncrementalCopy             = other.IsIncrementalCopy;
        }
 public static void AssertAreEqual(BlobProperties prop1, BlobProperties prop2)
 {
     if (prop1 == null)
     {
         Assert.IsNull(prop2);
     }
     else
     {
         Assert.IsNotNull(prop2);
         Assert.AreEqual(prop1.CacheControl, prop2.CacheControl);
         Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding);
         Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage);
         Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5);
         Assert.AreEqual(prop1.ContentType, prop2.ContentType);
         Assert.AreEqual(prop1.ETag, prop2.ETag);
         Assert.AreEqual(prop1.LastModified, prop2.LastModified);
         Assert.AreEqual(prop1.Length, prop2.Length);
     }
 }
예제 #10
0
 public static void AssertAreEqual(BlobProperties expected, BlobProperties actual)
 {
     if (expected == null)
     {
         Assert.IsNull(actual);
     }
     else
     {
         Assert.IsNotNull(actual);
         Assert.AreEqual(expected.CacheControl, actual.CacheControl);
         Assert.AreEqual(expected.ContentDisposition, actual.ContentDisposition);
         Assert.AreEqual(expected.ContentEncoding, actual.ContentEncoding);
         Assert.AreEqual(expected.ContentLanguage, actual.ContentLanguage);
         Assert.AreEqual(expected.ContentMD5, actual.ContentMD5);
         Assert.AreEqual(expected.ContentType, actual.ContentType);
         Assert.AreEqual(expected.ETag, actual.ETag);
         Assert.AreEqual(expected.LastModified, actual.LastModified);
         Assert.AreEqual(expected.Length, actual.Length);
     }
 }
        /// <summary>
        /// Updates this blob with the given attributes a the end of a fetch attributes operation.
        /// </summary>
        /// <param name="attributes">The new attributes.</param>
        /// <param name="response">The response.</param>
        /// <param name="ignoreMD5">if set to <c>true</c>, blob's MD5 will not be updated.</param>
        /// <exception cref="System.InvalidOperationException"></exception>
        internal static void UpdateAfterFetchAttributes(BlobAttributes attributes, HttpWebResponse response, bool ignoreMD5)
        {
            BlobProperties properties = BlobHttpResponseParsers.GetProperties(response);

            // If BlobType is specified and the value returned from cloud is different,
            // then it's a client error and we need to throw.
            if (attributes.Properties.BlobType != BlobType.Unspecified && attributes.Properties.BlobType != properties.BlobType)
            {
                throw new InvalidOperationException(SR.BlobTypeMismatchExceptionMessage);
            }

            if (ignoreMD5)
            {
                properties.ContentMD5 = attributes.Properties.ContentMD5;
            }

            attributes.Properties = properties;
            attributes.Metadata   = BlobHttpResponseParsers.GetMetadata(response);
            attributes.CopyState  = BlobHttpResponseParsers.GetCopyAttributes(response);
        }
 public static void AssertAreEqual(BlobProperties expected, BlobProperties actual)
 {
     if (expected == null)
     {
         Assert.IsNull(actual);
     }
     else
     {
         Assert.IsNotNull(actual);
         Assert.AreEqual(expected.CacheControl, actual.CacheControl);
         Assert.AreEqual(expected.ContentDisposition, actual.ContentDisposition);
         Assert.AreEqual(expected.ContentEncoding, actual.ContentEncoding);
         Assert.AreEqual(expected.ContentLanguage, actual.ContentLanguage);
         Assert.AreEqual(expected.ContentMD5, actual.ContentMD5);
         Assert.AreEqual(expected.ContentType, actual.ContentType);
         Assert.AreEqual(expected.ETag, actual.ETag);
         Assert.AreEqual(expected.LastModified, actual.LastModified);
         Assert.AreEqual(expected.Length, actual.Length);
     }
 }
예제 #13
0
        public async Task Load()
        {
            var random = new Random();
            var bytes = new byte[64];
            random.NextBytes(bytes);
            var p = new BlobProperties()
            {
                ContentType = Guid.NewGuid().ToString(),
                ContentMD5 = Guid.NewGuid().ToString(),
            };

            var container = Substitute.For<IContainer>();
            container.Get("file.txt").Returns(Task.FromResult(bytes));

            var bi = new BlobItem(container, "/file.txt");
            await bi.Load();

            Assert.AreEqual(bytes, bi.Data);

            container.Received().Get("file.txt");
        }
예제 #14
0
        /// <summary>
        /// Downloads the BLOB by BLOB URI.
        /// </summary>
        /// <param name="blobUri">The BLOB URI.</param>
        /// <param name="fetchAttribute">if set to <c>true</c> [fetch attribute].</param>
        /// <param name="blobProperties">The BLOB properties.</param>
        /// <param name="metaData">The meta data.</param>
        /// <returns>System.Byte[].</returns>
        public static byte[] DownloadBlobByBlobUri(string blobUri, bool fetchAttribute, out BlobProperties blobProperties, out IDictionary<string, string> metaData)
        {
            try
            {
                blobUri.CheckEmptyString("blobUri");

                var blob = new CloudBlockBlob(new Uri(blobUri));
                blobProperties = blob.Properties;

                return DownloadBlob(blob, fetchAttribute, out metaData);
            }
            catch (Exception ex)
            {
                throw ex.Handle("UploadBlobByBlobUri", blobUri);
            }
        }
        public async Task UpdateBlobPropertiesAsync(string containerName, string blobName, BlobProperties properties)
        {
            var container = _blobClient.GetContainerReference(containerName);
            var blob = container.GetBlockBlobReference(blobName);

            try
            {
                await blob.FetchAttributesAsync(null, _requestOptions, _context).ConfigureAwait(false);
                blob.Properties.ContentType = properties.ContentType;
                blob.Properties.ContentDisposition = properties.ContentDisposition;

                await blob.SetPropertiesAsync(null, _requestOptions, _context).ConfigureAwait(false);

                if (properties.Metadata != null)
                {
                    blob.Metadata.SetMetadata(properties.Metadata);
                    await blob.SetMetadataAsync();
                }
                
                var perms = await container.GetPermissionsAsync(null, _requestOptions, _context).ConfigureAwait(false);

                // Elevate container permissions if necessary.
                if (properties.Security == BlobSecurity.Public && perms.PublicAccess == BlobContainerPublicAccessType.Off)
                {
                    await container.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }, null, _requestOptions, _context).ConfigureAwait(false);
                }
            }
            catch (Exception e)
            {
                if (e.IsAzureStorageException())
                {
                    throw e.Convert();
                }
                throw;
            }
        }
        public async Task SaveBlobStreamAsync(string containerName, string blobName, Stream source, BlobProperties properties = null, bool closeStream = true)
        {
            var container = _blobClient.GetContainerReference(containerName);
            var props = properties ?? BlobProperties.Empty;
            var security = props.Security == BlobSecurity.Public ? BlobContainerPublicAccessType.Blob : BlobContainerPublicAccessType.Off;

            try
            {
                var created = await container.CreateIfNotExistsAsync(security, _requestOptions, _context).ConfigureAwait(false);

                var blob = container.GetBlockBlobReference(blobName);
                blob.Properties.ContentType = props.ContentType;                
                blob.Properties.ContentDisposition = props.ContentDisposition;                

                if (props.Metadata != null)
                {
                    blob.Metadata.SetMetadata(props.Metadata);
                }

                await blob.UploadFromStreamAsync(source, null, _requestOptions, _context).ConfigureAwait(false);

                // Hack to deal with issue https://github.com/Azure/azure-storage-net/issues/353
                if (!string.IsNullOrEmpty(props.ContentDisposition))
                {
                    await blob.SetPropertiesAsync(null, _requestOptions, _context);
                }

                // Check if container permission elevation is necessary
                if (!created)
                {
                    var perms = await container.GetPermissionsAsync(null, _requestOptions, _context).ConfigureAwait(false);

                    if (properties != null && properties.Security == BlobSecurity.Public && perms.PublicAccess == BlobContainerPublicAccessType.Off)
                    {
                        await container.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = security }, null, _requestOptions, _context).ConfigureAwait(false);
                    }
                }
            }
            catch (Exception e)
            {
                if (e.IsAzureStorageException())
                {
                    throw e.Convert();
                }
                throw;
            }
            finally
            {
                if (closeStream)
                {
                    source.Dispose();                    
                }
            }
        }
예제 #17
0
파일: BlobFile2.cs 프로젝트: usbuild/azuren
 public BlobProperties Properties()
 {
     ValidateFile();
     if (_properties == null)
     {
         if (_blob == null) GetBlob();
         _blob.FetchAttributes();
         _properties = _blob.Properties;
     }
     return _properties;
 }
 public HostedAzureBlobProperties(BlobProperties blobProperties)
 {
     _blobProperties = blobProperties;
 }
예제 #19
0
    protected ICloudBlob StubACloudBlob(DateTimeOffset? lastModified = default(DateTimeOffset?))
    {
        var cloudBlobProperties = new BlobProperties();
        var property = typeof(BlobProperties).GetProperty("LastModified");
        property.SetValue(cloudBlobProperties, lastModified, BindingFlags.NonPublic, null, null, null);


        var cloudBlob = MockRepository.GenerateStub<ICloudBlob>();
        cloudBlob.Stub(x => x.Metadata).Return(new Dictionary<string, string>());
        cloudBlob.Stub(x => x.SetMetadata());
        cloudBlob.Stub(x => x.Properties).Return(cloudBlobProperties);
        return cloudBlob;
    }
예제 #20
0
        public async Task CloudPageBlobCopyFromSnapshotTestAsync()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                await container.CreateAsync();

                CloudPageBlob source = container.GetPageBlobReference("source");

                string data = new string('a', 512);
                await UploadTextAsync(source, data, Encoding.UTF8);

                source.Metadata["Test"] = "value";
                await source.SetMetadataAsync();

                CloudPageBlob snapshot = await source.CreateSnapshotAsync();

                //Modify source
                string newData = new string('b', 512);
                source.Metadata["Test"] = "newvalue";
                await source.SetMetadataAsync();

                source.Properties.ContentMD5 = null;
                await UploadTextAsync(source, newData, Encoding.UTF8);

                Assert.AreEqual(newData, await DownloadTextAsync(source, Encoding.UTF8), "Source is modified correctly");
                Assert.AreEqual(data, await DownloadTextAsync(snapshot, Encoding.UTF8), "Modifying source blob should not modify snapshot");

                await source.FetchAttributesAsync();

                await snapshot.FetchAttributesAsync();

                Assert.AreNotEqual(source.Metadata["Test"], snapshot.Metadata["Test"], "Source and snapshot metadata should be independent");

                CloudPageBlob copy = container.GetPageBlobReference("copy");
                await copy.StartCopyFromBlobAsync(TestHelper.Defiddler(snapshot));
                await WaitForCopyAsync(copy);

                Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status);
                Assert.AreEqual(data, await DownloadTextAsync(copy, Encoding.UTF8), "Data inside copy of blob not similar");

                await copy.FetchAttributesAsync();

                BlobProperties prop1 = copy.Properties;
                BlobProperties prop2 = snapshot.Properties;

                Assert.AreEqual(prop1.CacheControl, prop2.CacheControl);
                Assert.AreEqual(prop1.ContentDisposition, prop2.ContentDisposition);
                Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding);
                Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage);
                Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5);
                Assert.AreEqual(prop1.ContentType, prop2.ContentType);

                Assert.AreEqual("value", copy.Metadata["Test"], false, "Copied metadata not same");

                await copy.DeleteAsync();
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
        /// <summary>
        /// Get blob properties
        /// </summary>
        /// <returns>Return true on success, false if not found, throw exception on error</returns>
        public bool GetBlobProperties(string containerName, string blobName, out BlobProperties properties)
        {
            properties = null; 

            try
            {
                CloudBlobContainer container = _blobClient.GetContainerReference(containerName);
                ICloudBlob blob = container.GetBlobReferenceFromServer(blobName);
                blob.FetchAttributes();
                properties = blob.Properties;

                return true;
            }
            catch (StorageException ex)
            {
                if (ex.RequestInformation.HttpStatusCode == 404)
                {
                    return false;
                }

                throw;
            }
        }
예제 #22
0
        private static async Task CloudBlockBlobCopyFromCloudFileImpl(Func <CloudFile, CloudBlockBlob, string> copyFunc)
        {
            CloudFileClient fileClient = GenerateCloudFileClient();

            string         name  = GetRandomContainerName();
            CloudFileShare share = fileClient.GetShareReference(name);

            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                try
                {
                    await container.CreateAsync();

                    await share.CreateAsync();

                    CloudFile source = share.GetRootDirectoryReference().GetFileReference("source");
                    byte[]    data   = GetRandomBuffer(1024);

                    await source.UploadFromByteArrayAsync(data, 0, data.Length);

                    source.Metadata["Test"] = "value";
                    source.SetMetadataAsync().Wait();

                    var sasToken = source.GetSharedAccessSignature(new SharedAccessFilePolicy
                    {
                        Permissions            = SharedAccessFilePermissions.Read,
                        SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24)
                    });

                    Uri fileSasUri = new Uri(source.StorageUri.PrimaryUri.ToString() + sasToken);

                    source = new CloudFile(fileSasUri);

                    CloudBlockBlob copy   = container.GetBlockBlobReference("copy");
                    string         copyId = copyFunc(source, copy);
                    Assert.AreEqual(BlobType.BlockBlob, copy.BlobType);

                    try
                    {
                        await WaitForCopyAsync(copy);

                        Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status);
                        Assert.AreEqual(source.Uri.AbsolutePath, copy.CopyState.Source.AbsolutePath);
                        Assert.AreEqual(data.Length, copy.CopyState.TotalBytes);
                        Assert.AreEqual(data.Length, copy.CopyState.BytesCopied);
                        Assert.AreEqual(copyId, copy.CopyState.CopyId);
                        Assert.IsFalse(copy.Properties.IsIncrementalCopy);
                        Assert.IsTrue(copy.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));
                    }
                    catch (NullReferenceException)
                    {
                        // potential null ref in the WaitForCopyTask and CopyState check implementation
                    }

                    OperationContext opContext = new OperationContext();
                    await TestHelper.ExpectedExceptionAsync(
                        async() => await copy.AbortCopyAsync(copyId, null, null, opContext),
                        opContext,
                        "Aborting a copy operation after completion should fail",
                        HttpStatusCode.Conflict,
                        "NoPendingCopyOperation");

                    source.FetchAttributesAsync().Wait();
                    Assert.IsNotNull(copy.Properties.ETag);
                    Assert.AreNotEqual(source.Properties.ETag, copy.Properties.ETag);
                    Assert.IsTrue(copy.Properties.LastModified > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                    byte[] copyData = new byte[source.Properties.Length];

                    await copy.DownloadToByteArrayAsync(copyData, 0);

                    Assert.IsTrue(data.SequenceEqual(copyData), "Data inside copy of blob not similar");

                    copy.FetchAttributesAsync().Wait();
                    BlobProperties prop1 = copy.Properties;
                    FileProperties prop2 = source.Properties;

                    Assert.AreEqual(prop1.CacheControl, prop2.CacheControl);
                    Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding);
                    Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage);
                    Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5);
                    Assert.AreEqual(prop1.ContentType, prop2.ContentType);

                    Assert.AreEqual("value", copy.Metadata["Test"], false, "Copied metadata not same");

                    copy.DeleteIfExistsAsync().Wait();
                }
                finally
                {
                    share.DeleteIfExistsAsync().Wait();
                }
            }
            finally
            {
                container.DeleteIfExistsAsync().Wait();
            }
        }
 public static void AssertAreEqual(BlobProperties expected, BlobProperties actual)
 {
     AssertAreEqual(expected, actual, true);
 }
 public BlobProperties(BlobProperties other)
 {
     throw new System.NotImplementedException();
 }
        public async void Test_Blob_Created_ContentDisposition_Set_Async()
        {
            var container = GetRandomContainerName();
            var blobName = GenerateRandomName();
            var filename = "testFile.jpg";
            var dataLength = 256;            
            var data = GenerateRandomBlobStream(dataLength);
            var prop = new BlobProperties().WithContentDispositionFilename(filename);
            
            await _provider.SaveBlobStreamAsync(container, blobName, data, prop);

            var blob = _client.GetContainerReference(container)
                .GetBlockBlobReference(blobName);

            await blob.FetchAttributesAsync();

            Assert.Equal(dataLength, blob.Properties.Length);
            Assert.Equal($"attachment; filename=\"{filename}\"", blob.Properties.ContentDisposition);
        }
 public static void AssertAreEqual(BlobProperties prop1, BlobProperties prop2)
 {
     if (prop1 == null)
     {
         Assert.IsNull(prop2);
     }
     else
     {
         Assert.IsNotNull(prop2);
         Assert.AreEqual(prop1.CacheControl, prop2.CacheControl);
         Assert.AreEqual(prop1.ContentDisposition, prop2.ContentDisposition);
         Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding);
         Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage);
         Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5);
         Assert.AreEqual(prop1.ContentType, prop2.ContentType);
         Assert.AreEqual(prop1.ETag, prop2.ETag);
         Assert.AreEqual(prop1.LastModified, prop2.LastModified);
         Assert.AreEqual(prop1.Length, prop2.Length);
     }
 }
예제 #27
0
        public void CloudBlockBlobCopyTestAPM()
        {
            CloudBlobContainer container = GetRandomContainerReference();
            try
            {
                container.Create();

                CloudBlockBlob source = container.GetBlockBlobReference("source");

                string data = "String data";
                UploadText(source, data, Encoding.UTF8);

                source.Metadata["Test"] = "value";
                source.SetMetadata();

                CloudBlockBlob copy = container.GetBlockBlobReference("copy");
                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    IAsyncResult result = copy.BeginStartCopyFromBlob(TestHelper.Defiddler(source),
                        ar => waitHandle.Set(),
                        null);
                    waitHandle.WaitOne();
                    string copyId = copy.EndStartCopyFromBlob(result);
                    WaitForCopy(copy);
                    Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status);
                    Assert.AreEqual(source.Uri.AbsolutePath, copy.CopyState.Source.AbsolutePath);
                    Assert.AreEqual(data.Length, copy.CopyState.TotalBytes);
                    Assert.AreEqual(data.Length, copy.CopyState.BytesCopied);
                    Assert.AreEqual(copyId, copy.CopyState.CopyId);
                    Assert.IsTrue(copy.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                    result = copy.BeginAbortCopy(copyId,
                        ar => waitHandle.Set(),
                        null);
                    waitHandle.WaitOne();
                    TestHelper.ExpectedException(
                        () => copy.EndAbortCopy(result),
                        "Aborting a copy operation after completion should fail",
                        HttpStatusCode.Conflict,
                        "NoPendingCopyOperation");
                }

                source.FetchAttributes();
                Assert.IsNotNull(copy.Properties.ETag);
                Assert.AreNotEqual(source.Properties.ETag, copy.Properties.ETag);
                Assert.IsTrue(copy.Properties.LastModified > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                string copyData = DownloadText(copy, Encoding.UTF8);
                Assert.AreEqual(data, copyData, "Data inside copy of blob not similar");

                copy.FetchAttributes();
                BlobProperties prop1 = copy.Properties;
                BlobProperties prop2 = source.Properties;

                Assert.AreEqual(prop1.CacheControl, prop2.CacheControl);
                Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding);
                Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage);
                Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5);
                Assert.AreEqual(prop1.ContentType, prop2.ContentType);

                Assert.AreEqual("value", copy.Metadata["Test"], false, "Copied metadata not same");

                copy.Delete();
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
예제 #28
0
 /// <summary>
 /// Downloads the BLOB by BLOB URI.
 /// </summary>
 /// <param name="blobUri">The BLOB URI.</param>
 /// <param name="blobProperties">The BLOB properties.</param>
 /// <param name="metaData">The meta data.</param>
 /// <returns>System.Byte[].</returns>
 public static byte[] DownloadBlobByBlobUri(string blobUri, out BlobProperties blobProperties, out IDictionary<string, string> metaData)
 {
     return DownloadBlobByBlobUri(blobUri, true, out blobProperties, out metaData);
 }
예제 #29
0
 /// <summary>
 /// Downloads the BLOB by BLOB URI.
 /// </summary>
 /// <param name="blobUri">The BLOB URI.</param>
 /// <param name="blobProperties">The BLOB properties.</param>
 /// <returns>System.Byte[].</returns>
 public static byte[] DownloadBlobByBlobUri(string blobUri, out BlobProperties blobProperties)
 {
     IDictionary<string, string> metaData = null;
     return DownloadBlobByBlobUri(blobUri, false, out blobProperties, out metaData);
 }
예제 #30
0
        private static void CloudBlockBlobCopy(bool sourceIsSas, bool destinationIsSas)
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                // Create Source on server
                CloudBlockBlob source = container.GetBlockBlobReference("source");

                string data = "String data";
                UploadText(source, data, Encoding.UTF8);

                source.Metadata["Test"] = "value";
                source.SetMetadata();

                // Create Destination on server
                CloudBlockBlob destination = container.GetBlockBlobReference("destination");
                destination.PutBlockList(new string[] { });

                CloudBlockBlob copySource      = source;
                CloudBlockBlob copyDestination = destination;

                if (sourceIsSas)
                {
                    // Source SAS must have read permissions
                    SharedAccessBlobPermissions permissions = SharedAccessBlobPermissions.Read;
                    SharedAccessBlobPolicy      policy      = new SharedAccessBlobPolicy()
                    {
                        SharedAccessStartTime  = DateTimeOffset.UtcNow.AddMinutes(-5),
                        SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                        Permissions            = permissions,
                    };
                    string sasToken = source.GetSharedAccessSignature(policy);

                    // Get source
                    StorageCredentials credentials = new StorageCredentials(sasToken);
                    copySource = new CloudBlockBlob(credentials.TransformUri(source.Uri));
                }

                if (destinationIsSas)
                {
                    if (!sourceIsSas)
                    {
                        // Source container must be public if source is not SAS
                        BlobContainerPermissions containerPermissions = new BlobContainerPermissions
                        {
                            PublicAccess = BlobContainerPublicAccessType.Blob
                        };
                        container.SetPermissions(containerPermissions);
                    }

                    // Destination SAS must have write permissions
                    SharedAccessBlobPermissions permissions = SharedAccessBlobPermissions.Write;
                    SharedAccessBlobPolicy      policy      = new SharedAccessBlobPolicy()
                    {
                        SharedAccessStartTime  = DateTimeOffset.UtcNow.AddMinutes(-5),
                        SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                        Permissions            = permissions,
                    };
                    string sasToken = destination.GetSharedAccessSignature(policy);

                    // Get destination
                    StorageCredentials credentials = new StorageCredentials(sasToken);
                    copyDestination = new CloudBlockBlob(credentials.TransformUri(destination.Uri));
                }

                // Start copy and wait for completion
                string copyId = copyDestination.StartCopyFromBlob(TestHelper.Defiddler(copySource));
                WaitForCopy(destination);

                // Check original blob references for equality
                Assert.AreEqual(CopyStatus.Success, destination.CopyState.Status);
                Assert.AreEqual(source.Uri.AbsolutePath, destination.CopyState.Source.AbsolutePath);
                Assert.AreEqual(data.Length, destination.CopyState.TotalBytes);
                Assert.AreEqual(data.Length, destination.CopyState.BytesCopied);
                Assert.AreEqual(copyId, destination.CopyState.CopyId);
                Assert.IsTrue(destination.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                if (!destinationIsSas)
                {
                    // Abort Copy is not supported for SAS destination
                    TestHelper.ExpectedException(
                        () => copyDestination.AbortCopy(copyId),
                        "Aborting a copy operation after completion should fail",
                        HttpStatusCode.Conflict,
                        "NoPendingCopyOperation");
                }

                source.FetchAttributes();
                Assert.IsNotNull(destination.Properties.ETag);
                Assert.AreNotEqual(source.Properties.ETag, destination.Properties.ETag);
                Assert.IsTrue(destination.Properties.LastModified > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                string copyData = DownloadText(destination, Encoding.UTF8);
                Assert.AreEqual(data, copyData, "Data inside copy of blob not equal.");

                destination.FetchAttributes();
                BlobProperties prop1 = destination.Properties;
                BlobProperties prop2 = source.Properties;

                Assert.AreEqual(prop1.CacheControl, prop2.CacheControl);
                Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding);
                Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage);
                Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5);
                Assert.AreEqual(prop1.ContentType, prop2.ContentType);

                Assert.AreEqual("value", destination.Metadata["Test"], false, "Copied metadata not same");

                destination.Delete();
                source.Delete();
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
예제 #31
0
        public async Task CloudPageBlobCopyTestAsync()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                await container.CreateAsync();

                CloudPageBlob source = container.GetPageBlobReference("source");

                string data = new string('a', 512);
                await UploadTextAsync(source, data, Encoding.UTF8);

                source.Metadata["Test"] = "value";
                await source.SetMetadataAsync();

                CloudPageBlob copy   = container.GetPageBlobReference("copy");
                string        copyId = await copy.StartCopyFromBlobAsync(TestHelper.Defiddler(source));
                await WaitForCopyAsync(copy);

                Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status);
                Assert.AreEqual(source.Uri.AbsolutePath, copy.CopyState.Source.AbsolutePath);
                Assert.AreEqual(data.Length, copy.CopyState.TotalBytes);
                Assert.AreEqual(data.Length, copy.CopyState.BytesCopied);
                Assert.AreEqual(copyId, copy.CopyState.CopyId);
                Assert.IsTrue(copy.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                OperationContext opContext = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(
                    async() => await copy.AbortCopyAsync(copyId, null, null, opContext),
                    opContext,
                    "Aborting a copy operation after completion should fail",
                    HttpStatusCode.Conflict,
                    "NoPendingCopyOperation");

                await source.FetchAttributesAsync();

                Assert.IsNotNull(copy.Properties.ETag);
                Assert.AreNotEqual(source.Properties.ETag, copy.Properties.ETag);
                Assert.IsTrue(copy.Properties.LastModified > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                string copyData = await DownloadTextAsync(copy, Encoding.UTF8);

                Assert.AreEqual(data, copyData, "Data inside copy of blob not similar");

                await copy.FetchAttributesAsync();

                BlobProperties prop1 = copy.Properties;
                BlobProperties prop2 = source.Properties;

                Assert.AreEqual(prop1.CacheControl, prop2.CacheControl);
                Assert.AreEqual(prop1.ContentDisposition, prop2.ContentDisposition);
                Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding);
                Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage);
                Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5);
                Assert.AreEqual(prop1.ContentType, prop2.ContentType);

                Assert.AreEqual("value", copy.Metadata["Test"], false, "Copied metadata not same");

                await copy.DeleteAsync();
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
예제 #32
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);
        }
 public static void AssertAreEqual(BlobProperties expected, BlobProperties actual)
 {
     AssertAreEqual(expected, actual, true);
 }
        /// <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 
        /// <list type="number">
        /// <item>CacheControl</item>
        /// <item>ContentEncoding</item>
        /// <item>ContentLanguage</item>
        /// <item>ContentMD5</item>
        /// <item>ContentType</item>
        /// </list>
        /// </summary>
        /// <param name="containerName"></param>
        /// <param name="blobName"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        public bool SetBlobProperties(string containerName, string blobName, BlobProperties properties)
        {
            try
            {
                CloudBlobContainer container = _blobClient.GetContainerReference(containerName);
                ICloudBlob blob = container.GetBlobReferenceFromServer(blobName);
                blob.FetchAttributes();
                //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;
 
                blob.SetProperties();

                return true;
            }
            catch (StorageException ex)
            {
                if (ex.RequestInformation.HttpStatusCode == 404)
                {
                    return false;
                }

                throw;
            }
        }