protected String GetTitle(Uri blobURI) { //returns the title of the file from the Blob metadata CloudBlockBlob blob = new CloudBlockBlob(blobURI); blob.FetchAttributes(); return blob.Metadata["Title"]; }
public void revertFromSnapshot(CloudBlockBlob blobRef, CloudBlockBlob snapshot) { try { blobRef.StartCopyFromBlob(snapshot); DateTime timestamp = DateTime.Now; blobRef.FetchAttributes(); snapshot.FetchAttributes(); string time = snapshot.Metadata["timestamp"]; blobRef.Metadata["timestamp"] = timestamp.ToUniversalTime().ToString("MM/dd/yyyy HH:mm:ss"); blobRef.SetMetadata(); blobRef.CreateSnapshot(); //System.Windows.Forms.MessageBox.Show("revert success"); Program.ClientForm.addtoConsole("Successfully Reverted with time: " + time); Program.ClientForm.ballon("Successfully Reverted! "); } catch (Exception e) { Program.ClientForm.addtoConsole("Exception:" + e.Message); //System.Windows.Forms.MessageBox.Show(e.ToString()); } }
protected String GetInstanceIndex(Uri blobURI) { //returns the Worker role's index from the Blob metadata CloudBlockBlob blob = new CloudBlockBlob(blobURI); blob.FetchAttributes(); return blob.Metadata["InstanceNo"]; }
/// <summary> /// Creates an archived copy of the original package blob if it doesn't already exist. /// </summary> private void ArchiveOriginalPackageBlob(CloudBlockBlob originalPackageBlob, CloudBlockBlob latestPackageBlob) { // Copy the blob to backup only if it isn't already successfully copied if ((!originalPackageBlob.Exists()) || (originalPackageBlob.CopyState != null && originalPackageBlob.CopyState.Status != CopyStatus.Success)) { if (!WhatIf) { Log.Info("Backing up blob: {0} to {1}", latestPackageBlob.Name, originalPackageBlob.Name); originalPackageBlob.StartCopyFromBlob(latestPackageBlob); CopyState state = originalPackageBlob.CopyState; for (int i = 0; (state == null || state.Status == CopyStatus.Pending) && i < SleepTimes.Length; i++) { Log.Info("(sleeping for a copy completion)"); Thread.Sleep(SleepTimes[i]); originalPackageBlob.FetchAttributes(); // To get a refreshed CopyState //refresh state state = originalPackageBlob.CopyState; } if (state.Status != CopyStatus.Success) { string msg = string.Format("Blob copy failed: CopyState={0}", state.StatusDescription); Log.Error("(error) " + msg); throw new BlobBackupFailedException(msg); } } } }
public static bool Exists(CloudBlockBlob blob) { try { blob.FetchAttributes(); return true; } catch (StorageException) { return false; } }
public void CloudBlockBlobCopyTestWithMetadataOverride() { 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"); copy.Metadata["Test2"] = "value2"; string copyId = copy.StartCopyFromBlob(TestHelper.Defiddler(source)); 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))); string copyData = DownloadText(copy, Encoding.UTF8); Assert.AreEqual(data, copyData, "Data inside copy of blob not similar"); copy.FetchAttributes(); source.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("value2", copy.Metadata["Test2"], false, "Copied metadata not same"); Assert.IsFalse(copy.Metadata.ContainsKey("Test"), "Source Metadata should not appear in destination blob"); copy.Delete(); } finally { container.DeleteIfExists(); } }
public void BlockBlobWriteStreamOneByteTest() { byte buffer = 127; MD5 hasher = MD5.Create(); CloudBlobContainer container = GetRandomContainerReference(); container.ServiceClient.ParallelOperationThreadCount = 2; try { container.Create(); CloudBlockBlob blob = container.GetBlockBlobReference("blob1"); blob.StreamWriteSizeInBytes = 16 * 1024; using (MemoryStream wholeBlob = new MemoryStream()) { BlobRequestOptions options = new BlobRequestOptions() { StoreBlobContentMD5 = true, }; using (Stream blobStream = blob.OpenWrite(null, options)) { for (int i = 0; i < 1 * 1024 * 1024; i++) { blobStream.WriteByte(buffer); wholeBlob.WriteByte(buffer); Assert.AreEqual(wholeBlob.Position, blobStream.Position); } } wholeBlob.Seek(0, SeekOrigin.Begin); string md5 = Convert.ToBase64String(hasher.ComputeHash(wholeBlob)); blob.FetchAttributes(); Assert.AreEqual(md5, blob.Properties.ContentMD5); using (MemoryStream downloadedBlob = new MemoryStream()) { blob.DownloadToStream(downloadedBlob); TestHelper.AssertStreamsAreEqual(wholeBlob, downloadedBlob); } } } finally { container.DeleteIfExists(); } }
private void PrefixEscapingTest(string prefix, string blobName) { CloudBlobClient service = GenerateCloudBlobClient(); CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); string text = Guid.NewGuid().ToString(); // Create from CloudBlobContainer. CloudBlockBlob originalBlob = container.GetBlockBlobReference(prefix + "/" + blobName); originalBlob.PutBlockList(new string[] { }); // List blobs from container. IListBlobItem blobFromContainerListingBlobs = container.ListBlobs(null, true).First(); Assert.AreEqual(originalBlob.Uri, blobFromContainerListingBlobs.Uri); CloudBlockBlob uriBlob = new CloudBlockBlob(originalBlob.Uri, service.Credentials); // Check Name Assert.AreEqual <string>(prefix + "/" + blobName, originalBlob.Name); Assert.AreEqual <string>(prefix + "/" + blobName, uriBlob.Name); // Absolute URI access from CloudBlockBlob CloudBlockBlob blobInfo = new CloudBlockBlob(originalBlob.Uri, service.Credentials); blobInfo.FetchAttributes(); // Access from CloudBlobDirectory CloudBlobDirectory cloudBlobDirectory = container.GetDirectoryReference(prefix); CloudBlockBlob blobFromCloudBlobDirectory = cloudBlobDirectory.GetBlockBlobReference(blobName); Assert.AreEqual(blobInfo.Uri, blobFromCloudBlobDirectory.Uri); // Copy blob verification. CloudBlockBlob copyBlob = container.GetBlockBlobReference(prefix + "/" + blobName + "copy"); copyBlob.StartCopyFromBlob(blobInfo.Uri); copyBlob.FetchAttributes(); } finally { container.Delete(); } }
public void CloudBlobSASUnknownParams() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); // Create Source on server CloudBlockBlob blob = container.GetBlockBlobReference("source"); string data = "String data"; UploadText(blob, data, Encoding.UTF8); UriBuilder blobURIBuilder = new UriBuilder(blob.Uri); blobURIBuilder.Query = "MyQuery=value&YOURQUERY=value2"; // Add the query params unknown to the service // 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 = blob.GetSharedAccessSignature(policy); // Replace one of the SAS keys with a capitalized version to ensure we are case-insensitive on expected parameter keys as well StorageCredentials credentials = new StorageCredentials(sasToken); StringBuilder sasString = new StringBuilder(credentials.TransformUri(blobURIBuilder.Uri).ToString()); sasString.Replace("sp=", "SP="); CloudBlockBlob sasBlob = new CloudBlockBlob(new Uri(sasString.ToString())); // Validate that we can fetch the attributes on the blob (no exception thrown) sasBlob.FetchAttributes(); } finally { container.DeleteIfExists(); } }
public void BlobWriteStreamOpenAndClose() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1"); using (Stream blobStream = blockBlob.OpenWrite()) { } CloudBlockBlob blockBlob2 = container.GetBlockBlobReference("blob1"); blockBlob2.FetchAttributes(); Assert.AreEqual(0, blockBlob2.Properties.Length); Assert.AreEqual(BlobType.BlockBlob, blockBlob2.Properties.BlobType); CloudPageBlob pageBlob = container.GetPageBlobReference("blob2"); TestHelper.ExpectedException( () => pageBlob.OpenWrite(null), "Opening a page blob stream with no size should fail on a blob that does not exist", HttpStatusCode.NotFound); using (Stream blobStream = pageBlob.OpenWrite(1024)) { } using (Stream blobStream = pageBlob.OpenWrite(null)) { } CloudPageBlob pageBlob2 = container.GetPageBlobReference("blob2"); pageBlob2.FetchAttributes(); Assert.AreEqual(1024, pageBlob2.Properties.Length); Assert.AreEqual(BlobType.PageBlob, pageBlob2.Properties.BlobType); } finally { container.Delete(); } }
/// <summary> /// Test blob writing, expecting success. /// </summary> /// <param name="testBlob">The blob to test.</param> /// <param name="sourceBlob">A blob to use as the source of a copy.</param> /// <param name="testAccessCondition">The access condition to use.</param> private void BlobWriteExpectLeaseSuccess(CloudBlockBlob testBlob, CloudBlob sourceBlob, AccessCondition testAccessCondition) { testBlob.SetMetadata(testAccessCondition, null /* options */); testBlob.SetProperties(testAccessCondition, null /* options */); UploadText(testBlob, "No Problem", Encoding.UTF8, testAccessCondition, null /* options */); testBlob.StartCopy(TestHelper.Defiddler(sourceBlob.Uri), null /* source access condition */, testAccessCondition, null /* options */); while (testBlob.CopyState.Status == CopyStatus.Pending) { Thread.Sleep(1000); testBlob.FetchAttributes(); } Stream stream = testBlob.OpenWrite(testAccessCondition, null /* options */); stream.WriteByte(0); stream.Flush(); testBlob.Delete(DeleteSnapshotsOption.None, testAccessCondition, null /* options */); }
public void BlockBlobWriteStreamBasicTestAPM() { byte[] buffer = GetRandomBuffer(1024 * 1024); MD5 hasher = MD5.Create(); CloudBlobClient blobClient = GenerateCloudBlobClient(); blobClient.ParallelOperationThreadCount = 4; string name = GetRandomContainerName(); CloudBlobContainer container = blobClient.GetContainerReference(name); try { container.Create(); CloudBlockBlob blob = container.GetBlockBlobReference("blob1"); blob.StreamWriteSizeInBytes = buffer.Length; using (MemoryStream wholeBlob = new MemoryStream()) { BlobRequestOptions options = new BlobRequestOptions() { StoreBlobContentMD5 = true, }; using (Stream blobStream = blob.OpenWrite(null, options)) { IAsyncResult[] results = new IAsyncResult[blobClient.ParallelOperationThreadCount * 2]; for (int i = 0; i < results.Length; i++) { results[i] = blobStream.BeginWrite(buffer, 0, buffer.Length, null, null); wholeBlob.Write(buffer, 0, buffer.Length); Assert.AreEqual(wholeBlob.Position, blobStream.Position); } for (int i = 0; i < blobClient.ParallelOperationThreadCount; i++) { Assert.IsTrue(results[i].IsCompleted); } for (int i = blobClient.ParallelOperationThreadCount; i < results.Length; i++) { Assert.IsFalse(results[i].IsCompleted); } for (int i = 0; i < results.Length; i++) { blobStream.EndWrite(results[i]); } } wholeBlob.Seek(0, SeekOrigin.Begin); string md5 = Convert.ToBase64String(hasher.ComputeHash(wholeBlob)); blob.FetchAttributes(); Assert.AreEqual(md5, blob.Properties.ContentMD5); using (MemoryStream downloadedBlob = new MemoryStream()) { blob.DownloadToStream(downloadedBlob); TestHelper.AssertStreamsAreEqual(wholeBlob, downloadedBlob); } } } finally { container.DeleteIfExists(); } }
public AzurePackage ReadFromMetadata(CloudBlockBlob blob) { blob.FetchAttributes(); var package = new AzurePackage(); package.Id = blob.Metadata["Id"]; package.Version = new SemanticVersion(blob.Metadata["Version"]); if (blob.Metadata.ContainsKey("Title")) { package.Title = blob.Metadata["Title"]; } if (blob.Metadata.ContainsKey("Authors")) { package.Authors = blob.Metadata["Authors"].Split(','); } if (blob.Metadata.ContainsKey("Owners")) { package.Owners = blob.Metadata["Owners"].Split(','); } if (blob.Metadata.ContainsKey("IconUrl")) { package.IconUrl = new Uri(blob.Metadata["IconUrl"]); } if (blob.Metadata.ContainsKey("LicenseUrl")) { package.LicenseUrl = new Uri(blob.Metadata["LicenseUrl"]); } if (blob.Metadata.ContainsKey("ProjectUrl")) { package.ProjectUrl = new Uri(blob.Metadata["ProjectUrl"]); } package.RequireLicenseAcceptance = blob.Metadata["RequireLicenseAcceptance"].ToBool(); package.DevelopmentDependency = blob.Metadata["DevelopmentDependency"].ToBool(); if (blob.Metadata.ContainsKey("Description")) { package.Description = blob.Metadata["Description"]; } if (blob.Metadata.ContainsKey("Summary")) { package.Summary = blob.Metadata["Summary"]; } if (blob.Metadata.ContainsKey("ReleaseNotes")) { package.ReleaseNotes = blob.Metadata["ReleaseNotes"]; } if (blob.Metadata.ContainsKey("Tags")) { package.Tags = blob.Metadata["Tags"]; } var dependencySetContent = blob.Metadata["Dependencies"]; dependencySetContent = this.Base64Decode(dependencySetContent); package.DependencySets = dependencySetContent .FromJson<IEnumerable<AzurePackageDependencySet>>() .Select(x => new PackageDependencySet(x.TargetFramework, x.Dependencies)); package.IsAbsoluteLatestVersion = blob.Metadata["IsAbsoluteLatestVersion"].ToBool(); package.IsLatestVersion = blob.Metadata["IsLatestVersion"].ToBool(); if (blob.Metadata.ContainsKey("MinClientVersion")) { package.MinClientVersion = new Version(blob.Metadata["MinClientVersion"]); } package.Listed = blob.Metadata["Listed"].ToBool(); return package; }
private void DownloadBlobInParallel(CloudBlockBlob blob, Stream stream) { try { blob.FetchAttributes(); blob.ServiceClient.ParallelOperationThreadCount = NumberOfIOThreads; blob.DownloadToStream(stream); stream.Seek(0, SeekOrigin.Begin); } catch (StorageException ex) { logger.Warn(ex.Message); } }
public AzurePackage ReadFromMetadata(CloudBlockBlob blob) { blob.FetchAttributes(); var package = new AzurePackage(); package.Id = blob.Metadata[PkgConsts.Id]; package.Version = new SemanticVersion(blob.Metadata[PkgConsts.Version]); if (blob.Metadata.ContainsKey(PkgConsts.Title)) { package.Title = blob.Metadata[PkgConsts.Title]; } if (blob.Metadata.ContainsKey(PkgConsts.Authors)) { package.Authors = blob.Metadata[PkgConsts.Authors].Split(','); } if (blob.Metadata.ContainsKey(PkgConsts.Owners)) { package.Owners = blob.Metadata[PkgConsts.Owners].Split(','); } if (blob.Metadata.ContainsKey(PkgConsts.IconUrl)) { package.IconUrl = new Uri(blob.Metadata[PkgConsts.IconUrl]); } if (blob.Metadata.ContainsKey(PkgConsts.LicenseUrl)) { package.LicenseUrl = new Uri(blob.Metadata[PkgConsts.LicenseUrl]); } if (blob.Metadata.ContainsKey(PkgConsts.ProjectUrl)) { package.ProjectUrl = new Uri(blob.Metadata[PkgConsts.ProjectUrl]); } if (blob.Metadata.ContainsKey(PkgConsts.Language)) { package.Language = blob.Metadata[PkgConsts.Language]; } if (blob.Metadata.ContainsKey(PkgConsts.Copyright)) { package.Copyright = Base64Decode(blob.Metadata[PkgConsts.Copyright]); } if (blob.Metadata.ContainsKey(PkgConsts.ReportAbuseUrl)) { package.ReportAbuseUrl = new Uri(blob.Metadata[PkgConsts.ReportAbuseUrl]); } package.DownloadCount = Convert.ToInt32(blob.Metadata[PkgConsts.DownloadCount]); package.RequireLicenseAcceptance = blob.Metadata[PkgConsts.RequireLicenseAcceptance].ToBool(); package.DevelopmentDependency = blob.Metadata[PkgConsts.DevelopmentDependency].ToBool(); if (blob.Metadata.ContainsKey(PkgConsts.Description)) { package.Description = Base64Decode(blob.Metadata[PkgConsts.Description]); } if (blob.Metadata.ContainsKey(PkgConsts.Summary)) { package.Summary = Base64Decode(blob.Metadata[PkgConsts.Summary]); } if (blob.Metadata.ContainsKey(PkgConsts.ReleaseNotes)) { package.ReleaseNotes = Base64Decode(blob.Metadata[PkgConsts.ReleaseNotes]); } if (blob.Metadata.ContainsKey(PkgConsts.Tags)) { package.Tags = blob.Metadata[PkgConsts.Tags]; } var dependencySetContent = Base64Decode(blob.Metadata[PkgConsts.Dependencies]); package.DependencySets = dependencySetContent .FromJson<IEnumerable<AzurePackageDependencySet>>() .Select(x => new PackageDependencySet(x.TargetFramework, x.Dependencies)); package.FrameworkAssemblies = blob.Metadata[PkgConsts.FrameworkAssemblies] .FromJson<IEnumerable<AzureFrameworkAssemblyReference>>() .Select(x => x.GetFrameworkAssemblyReference()) .ToList(); package.PackageAssemblyReferences = blob.Metadata[PkgConsts.PackageAssemblyReferences] .FromJson<Collection<AzurePackageReferenceSet>>() .Select(x => x.GetReferenceSet()) .ToList(); package.AssemblyReferences = blob.Metadata[PkgConsts.AssemblyReferences] .FromJson<IEnumerable<AzureDtoAssemblyReference>>() .Select(x => x.GetAzureAssemblyReference()) .ToList(); package.IsAbsoluteLatestVersion = blob.Metadata[PkgConsts.IsAbsoluteLatestVersion].ToBool(); package.IsLatestVersion = blob.Metadata[PkgConsts.IsLatestVersion].ToBool(); if (blob.Metadata.ContainsKey(PkgConsts.MinClientVersion)) { package.MinClientVersion = new Version(blob.Metadata[PkgConsts.MinClientVersion]); } if (blob.Metadata.ContainsKey(PkgConsts.Published)) { package.Published = DateTimeOffset.Parse(blob.Metadata[PkgConsts.Published]); } package.Listed = blob.Metadata[PkgConsts.Listed].ToBool(); return package; }
public AzureBlobFileStorage(CloudBlockBlob blob, string rootPath, bool fetchAttributes = false) { _blob = blob; if(fetchAttributes) _blob.FetchAttributes(); _rootPath = rootPath; }
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(); } }
/// <summary> /// Test blob reads, expecting lease failure. /// </summary> /// <param name="testBlob">The blob to test.</param> /// <param name="targetBlob">The blob to use for the target of copy operations.</param> /// <param name="testAccessCondition">The failing access condition to use.</param> /// <param name="expectedErrorCode">The expected error code.</param> /// <param name="description">The reason why these calls should fail.</param> private void BlobReadExpectLeaseFailure(CloudBlockBlob testBlob, CloudBlockBlob targetBlob, AccessCondition testAccessCondition, HttpStatusCode expectedStatusCode, string expectedErrorCode, string description) { // FetchAttributes is a HEAD request with no extended error info, so it returns with the generic ConditionFailed error code. TestHelper.ExpectedException( () => testBlob.FetchAttributes(testAccessCondition, null /* options */), description + "(Fetch Attributes)", HttpStatusCode.PreconditionFailed); TestHelper.ExpectedException( () => testBlob.Snapshot(null /* metadata */, testAccessCondition, null /* options */), description + " (Create Snapshot)", expectedStatusCode, expectedErrorCode); TestHelper.ExpectedException( () => DownloadText(testBlob, Encoding.UTF8, testAccessCondition, null /* options */), description + " (Download Text)", expectedStatusCode, expectedErrorCode); TestHelper.ExpectedException( () => testBlob.OpenRead(testAccessCondition, null /* options */), description + " (Read Stream)", expectedStatusCode/*, expectedErrorCode*/); }
private static bool IsBlobAged(CloudBlockBlob blob, DateTime olderThanUtc) { try { blob.FetchAttributes(); return blob.Properties.LastModified < olderThanUtc; } catch (StorageException e) { throw new BlobException(string.Format(CultureInfo.InvariantCulture, "Could not FetchAttributes or examine LastModified from properties for blob named '{0}' to see if it is aged older than '{1}' (UTC). See inner exception for details.", blob.Name, olderThanUtc.ToString()), e); } }
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(); } }
/// <summary> /// Downloads the BLOB. /// </summary> /// <param name="blob">The BLOB.</param> /// <param name="fetchAttribute">if set to <c>true</c> [fetch attribute].</param> /// <param name="metaData">The meta data.</param> /// <returns>System.Byte[].</returns> public static byte[] DownloadBlob(CloudBlockBlob blob, bool fetchAttribute, out IDictionary<string, string> metaData) { try { blob.CheckNullObject("blob"); if (fetchAttribute) { blob.FetchAttributes(); metaData = blob.Metadata; } else { metaData = null; } var msRead = new MemoryStream { Position = 0 }; using (msRead) { blob.DownloadToStream(msRead); return msRead.ToBytes(); } } catch (Exception ex) { throw ex.Handle("DownloadBlob", blob.Uri.ToString()); } }
/// <summary> /// Test blob reads, expecting success. /// </summary> /// <param name="testBlob">The blob to test.</param> /// <param name="testAccessCondition">The access condition to use.</param> private void BlobReadExpectLeaseSuccess(CloudBlockBlob testBlob, AccessCondition testAccessCondition) { testBlob.FetchAttributes(testAccessCondition, null /* options */); testBlob.Snapshot(null /* metadata */, testAccessCondition, null /* options */).Delete(); DownloadText(testBlob, Encoding.UTF8, testAccessCondition, null /* options */); Stream stream = testBlob.OpenRead(testAccessCondition, null /* options */); stream.ReadByte(); }
void DownloadBlobInParallel(CloudBlockBlob blob, Stream stream) { blob.FetchAttributes(); blob.ServiceClient.ParallelOperationThreadCount = NumberOfIOThreads; container.ServiceClient.RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(BackOffInterval), MaxRetries); blob.DownloadToStream(stream); stream.Seek(0, SeekOrigin.Begin); }
public void CloudBlockBlobSnapshot() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024)); CloudBlockBlob blob = container.GetBlockBlobReference("blob1"); blob.UploadFromStream(originalData); Assert.IsFalse(blob.IsSnapshot); Assert.IsNull(blob.SnapshotTime, "Root blob has SnapshotTime set"); Assert.IsFalse(blob.SnapshotQualifiedUri.Query.Contains("snapshot")); Assert.AreEqual(blob.Uri, blob.SnapshotQualifiedUri); CloudBlockBlob snapshot1 = blob.CreateSnapshot(); Assert.AreEqual(blob.Properties.ETag, snapshot1.Properties.ETag); Assert.AreEqual(blob.Properties.LastModified, snapshot1.Properties.LastModified); Assert.IsTrue(snapshot1.IsSnapshot); Assert.IsNotNull(snapshot1.SnapshotTime, "Snapshot does not have SnapshotTime set"); Assert.AreEqual(blob.Uri, snapshot1.Uri); Assert.AreNotEqual(blob.SnapshotQualifiedUri, snapshot1.SnapshotQualifiedUri); Assert.AreNotEqual(snapshot1.Uri, snapshot1.SnapshotQualifiedUri); Assert.IsTrue(snapshot1.SnapshotQualifiedUri.Query.Contains("snapshot")); CloudBlockBlob snapshot2 = blob.CreateSnapshot(); Assert.IsTrue(snapshot2.SnapshotTime.Value > snapshot1.SnapshotTime.Value); snapshot1.FetchAttributes(); snapshot2.FetchAttributes(); blob.FetchAttributes(); AssertAreEqual(snapshot1.Properties, blob.Properties); CloudBlockBlob snapshot1Clone = new CloudBlockBlob(new Uri(blob.Uri + "?snapshot=" + snapshot1.SnapshotTime.Value.ToString("O")), blob.ServiceClient.Credentials); Assert.IsNotNull(snapshot1Clone.SnapshotTime, "Snapshot clone does not have SnapshotTime set"); Assert.AreEqual(snapshot1.SnapshotTime.Value, snapshot1Clone.SnapshotTime.Value); snapshot1Clone.FetchAttributes(); AssertAreEqual(snapshot1.Properties, snapshot1Clone.Properties); CloudBlockBlob snapshotCopy = container.GetBlockBlobReference("blob2"); snapshotCopy.StartCopyFromBlob(TestHelper.Defiddler(snapshot1.Uri)); WaitForCopy(snapshotCopy); Assert.AreEqual(CopyStatus.Success, snapshotCopy.CopyState.Status); TestHelper.ExpectedException<InvalidOperationException>( () => snapshot1.OpenWrite(), "Trying to write to a blob snapshot should fail"); using (Stream snapshotStream = snapshot1.OpenRead()) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } blob.PutBlockList(new List<string>()); blob.FetchAttributes(); using (Stream snapshotStream = snapshot1.OpenRead()) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } List<IListBlobItem> blobs = container.ListBlobs(null, true, BlobListingDetails.All, null, null).ToList(); Assert.AreEqual(4, blobs.Count); AssertAreEqual(snapshot1, (ICloudBlob)blobs[0]); AssertAreEqual(snapshot2, (ICloudBlob)blobs[1]); AssertAreEqual(blob, (ICloudBlob)blobs[2]); AssertAreEqual(snapshotCopy, (ICloudBlob)blobs[3]); } finally { container.DeleteIfExists(); } }
public static void Run() { #region Obtain a client from the connection string CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); Console.WriteLine("Parsed connection string and created blob client."); #endregion #region Obtain reference to images container CloudBlobContainer docContainer = blobClient.GetContainerReference("docs"); Console.WriteLine("Obtained reference to docs containers."); #endregion #region Create a snapshot CloudBlockBlob blockBlob = docContainer.GetBlockBlobReference(@"WhatIsBlobStorage.txt"); CloudBlockBlob blobSnapshot1 = blockBlob.CreateSnapshot(); var snapshotTime = blobSnapshot1.SnapshotTime.Value; Console.WriteLine("Created snapshot '{0}' for WhatIsBlobStorage.txt in docs container.", snapshotTime.ToString("o")); Thread.Sleep(TimeSpan.FromMilliseconds(100)); CloudBlockBlob blobSnapshot2 = blockBlob.CreateSnapshot(); Thread.Sleep(TimeSpan.FromMilliseconds(100)); CloudBlockBlob blobSnapshot3 = blockBlob.CreateSnapshot(); #endregion #region List snapshots Console.WriteLine("\nListing snapshots"); var snapshots = docContainer .ListBlobs(useFlatBlobListing: true, blobListingDetails: BlobListingDetails.Snapshots) .Where(blob => ((CloudBlockBlob) blob).SnapshotTime.HasValue); foreach (var snapshot in snapshots) { CloudBlockBlob blob = new CloudBlockBlob(blockBlob.Uri, ((CloudBlockBlob)snapshot).SnapshotTime, blobClient.Credentials); blob.FetchAttributes(); Console.WriteLine(" {0} - {1}", blob.Uri.AbsoluteUri, blob.SnapshotTime); } #endregion #region Modify original blob using (var stringStream = new MemoryStream(Encoding.UTF8.GetBytes(@"Replace contents with this random string"))) { blockBlob.UploadFromStream(stringStream); } Console.WriteLine("\nReplaced contents of WhatIsBlobStorage.txt with random string."); Console.WriteLine("Press any key to restore from snapshot ..."); Console.ReadLine(); #endregion #region Restore from snapshot CloudBlockBlob backupSnapshot = new CloudBlockBlob(blockBlob.Uri, snapshotTime, blobClient.Credentials); blockBlob.StartCopyFromBlob(backupSnapshot); Console.WriteLine("Restored contents of WhatIsBlobStorage.txt from original snapshot."); #endregion }
private void ArchiveOldReport(string reportPath, string reportName, string fileExt, CloudBlockBlob report) { if (!report.Exists()) return; report.FetchAttributes(); string archiveDirString = (archiveBlobPath + ( (archiveBlobPath.EndsWith("/") && reportPath.StartsWith("/")) ? reportPath.Substring(1) : reportPath )).ToLower(); if (!archiveDirString.EndsWith("/")) archiveDirString = archiveDirString + "/"; CloudBlobDirectory archiveDir = container.GetDirectoryReference(archiveDirString); //if the archive path exists, see if we need to delete the oldest blob in the path IEnumerable<IListBlobItem> blobs = archiveDir.ListBlobs(true); Dictionary<DateTimeOffset, string> oldReports = new Dictionary<DateTimeOffset, string>(); DateTimeOffset oldest = DateTimeOffset.MaxValue; ICloudBlob oldestBlob = null; int count = 0; foreach (ICloudBlob blob in blobs) { ++count; if (blob.Properties.LastModified.Value < oldest) { oldest = blob.Properties.LastModified.Value; oldestBlob = blob; } } if (count >= maxNumOfReportsArchived) oldestBlob.Delete(); //copy report to archive string newUri = (archiveDirString + reportName + "_" + report.Properties.LastModified.Value.ToString("u") + fileExt).ToLower(); CloudBlockBlob newReport = container.GetBlockBlobReference(newUri); newReport.StartCopyFromBlob(report); int retries = 3; while (retries > 0) { if (newReport.CopyState.Status == CopyStatus.Success) { Trace.TraceInformation("Blob archiving succeeded: " + newUri); break; } else if (newReport.CopyState.Status == CopyStatus.Aborted || newReport.CopyState.Status == CopyStatus.Failed || newReport.CopyState.Status == CopyStatus.Invalid) { Trace.TraceError("Blob archiving failed: " + newUri); break; } else { Thread.Sleep(1000); --retries; if (retries == 0) Trace.TraceError("Blob archiving timed out: " + newUri); } } }
private void PrefixEscapingTest(string prefix, string blobName) { CloudBlobClient service = GenerateCloudBlobClient(); CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); string text = Guid.NewGuid().ToString(); // Create from CloudBlobContainer. CloudBlockBlob originalBlob = container.GetBlockBlobReference(prefix + "/" + blobName); originalBlob.PutBlockList(new string[] { }); // List blobs from container. IListBlobItem blobFromContainerListingBlobs = container.ListBlobs(null, true).First(); Assert.AreEqual(originalBlob.Uri, blobFromContainerListingBlobs.Uri); // Check Name Assert.AreEqual<string>(prefix + "/" + blobName, originalBlob.Name); // Absolute URI access from CloudBlockBlob CloudBlockBlob blobInfo = new CloudBlockBlob(originalBlob.Uri, service.Credentials); blobInfo.FetchAttributes(); // Access from CloudBlobDirectory CloudBlobDirectory cloudBlobDirectory = container.GetDirectoryReference(prefix); CloudBlockBlob blobFromCloudBlobDirectory = cloudBlobDirectory.GetBlockBlobReference(blobName); Assert.AreEqual(blobInfo.Uri, blobFromCloudBlobDirectory.Uri); // Copy blob verification. CloudBlockBlob copyBlob = container.GetBlockBlobReference(prefix + "/" + blobName + "copy"); copyBlob.StartCopyFromBlob(blobInfo.Uri); copyBlob.FetchAttributes(); } finally { container.Delete(); } }
public byte[] GetBlob(string containerName, string blobName) { CloudBlobContainer container = _blobClient.GetContainerReference(containerName); _blockBlob = container.GetBlockBlobReference(blobName); _blockBlob.FetchAttributes(); long fileByteLength = _blockBlob.Properties.Length; byte[] fileContents = new byte[fileByteLength]; _blockBlob.DownloadToByteArray(fileContents, 0); return fileContents; }