//------------------------------------------------- // Recover a specific blob version //------------------------------------------------- private static async void RecoverSpecificBlobVersion() { var connectionString = Constants.connectionString; BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString); BlobContainerClient container = blobServiceClient.GetBlobContainerClient(Constants.containerName); // Get a specific blob to undelete. BlobClient blockBlob = container.GetBlobClient("logfile.txt"); // <Snippet_RecoverSpecificBlobVersion> // undelete await blockBlob.UndeleteAsync(); // List all blobs and snapshots in the container prefixed by the blob name IEnumerable <BlobItem> allBlobVersions = container.GetBlobs (BlobTraits.None, BlobStates.Snapshots, prefix: blockBlob.Name); // Restore the most recently generated snapshot to the active blob BlobItem copySource = allBlobVersions.First(version => ((BlobItem)version).Snapshot.Length > 0 && ((BlobItem)version).Name == blockBlob.Name) as BlobItem; blockBlob.StartCopyFromUri(container.GetBlockBlobClient(copySource.Name).Uri); // </Snippet_RecoverSpecificBlobVersion> }
private Metadata GetActualMetadata(BlobItem item) { // Pass all custom metadata through the converter var convertedMeta = item.Metadata.ToDictionary(k => k.Key, k => AzureStoreMetadataEncoder.DecodeMetadata(k.Value)); var metadata = new Metadata(convertedMeta); metadata.StoredLastModified = item.Properties.LastModified.Value.UtcDateTime; if (!metadata.LastModified.HasValue) { metadata.LastModified = metadata.StoredLastModified; } metadata.StoredContentLength = item.Properties.ContentLength; metadata.StoredContentType = item.Properties.ContentType; metadata.ETag = item.Properties.ETag.ToString(); // Remove the snapshot key at this point if we have it if (metadata.ContainsKey(InternalSnapshotKey)) { metadata.Remove(InternalSnapshotKey); } // Remove the store key as well... if (metadata.ContainsKey(StoreVersionKey)) { metadata.Remove(StoreVersionKey); } if (_enableSnapshots && !string.IsNullOrEmpty(item.Snapshot)) { metadata.Snapshot = item.Snapshot; } return(metadata); }
private string CreateBlobAndUploadToContainer(Mock <BlobContainerClient> containerMock, List <BlobItem> blobItems, string blobContent = "test", DateTimeOffset lastModified = default) { string blobName = Path.GetRandomFileName().Replace(".", ""); Mock <BlobBaseClient> item = new Mock <BlobBaseClient>(); if (lastModified == default) { lastModified = DateTimeOffset.UtcNow; } var blobProperties = BlobsModelFactory.BlobProperties(lastModified: lastModified); item.Setup(x => x.GetPropertiesAsync(null, It.IsAny <CancellationToken>())).ReturnsAsync(Response.FromValue(blobProperties, null)); item.Setup(x => x.Name).Returns(blobName); BlobItemProperties blobItemProperties = BlobsModelFactory.BlobItemProperties(true, lastModified: lastModified); BlobItem blobItem = BlobsModelFactory.BlobItem( name: blobName, properties: blobItemProperties ); blobItems.Add(blobItem); Mock <BlobClient> blobClientMock = new Mock <BlobClient>(); blobClientMock.Setup(x => x.Name).Returns(blobName); blobClientMock.Setup(x => x.Download(It.IsAny <CancellationToken>())).Returns(() => Response.FromValue(BlobsModelFactory.BlobDownloadInfo(content: new MemoryStream(Encoding.UTF8.GetBytes(blobContent))), null)); blobClientMock.Setup(x => x.GetProperties(It.IsAny <BlobRequestConditions>(), It.IsAny <CancellationToken>())) .Returns(Response.FromValue(blobProperties, null)); containerMock.Setup(x => x.GetBlobClient(blobName)).Returns(blobClientMock.Object); return(blobName); }
private async Task DeleteBlobItem(BlobItem blobItem) { await Task.WhenAll( DeleteTestData(blobItem.Name), DeleteTestRunRecord(blobItem.Name) ); }
public int Add(Stream stream) { BlobItem item = new BlobItem(stream); FList.Add(item); return(FList.Count - 1); }
private static bool FilterBlobTimestamp(SnapshotSelectionCriteria criteria, BlobItem x) { var ticks = FetchBlobTimestamp(x); return(ticks <= criteria.MaxTimeStamp.Ticks && (!criteria.MinTimestamp.HasValue || ticks >= criteria.MinTimestamp.Value.Ticks)); }
private Blob ConstructBlob(BlobItem item) { var metadata = new PropertyBag { }; if (item.Metadata?.Count > 0 == true) { foreach (var prop in item.Metadata) { metadata.Add(prop.Key, prop.Value); } } var result = new Blob { BlobStorage = this, Name = item.Name, Identifier = item.Name, // Uri = item.Uri.ToString(), //Size = item., // Parent = ConstructContainer(new BlobContainerClient(_connectionString, item.BlobContainerName)), Metadata = metadata }; return(result); }
/// <inheritdoc/> public async Task <bool> RestoreBlob(string org, string app, string instanceGuid, string dataGuid, string restoreTimestamp) { string name = $"{org}/{app}/{instanceGuid}/data/{dataGuid}"; BlobContainerClient container = await _clientProvider.GetBlobClient(org, Program.Environment); BlockBlobClient client = container.GetBlockBlobClient(name); if (!await client.ExistsAsync()) { return(false); } await client.UndeleteAsync(); IEnumerable <BlobItem> allSnapshots = container.GetBlobs(BlobTraits.None, BlobStates.Snapshots, prefix: name); BlobItem snapshot = allSnapshots.FirstOrDefault(s => s.Snapshot != null && s.Snapshot.Length > 0 && s.Snapshot.Equals(restoreTimestamp)); if (snapshot != null) { await client.StartCopyFromUriAsync(new Uri($"{client.Uri}?snapshot={restoreTimestamp}")); } await client.DeleteIfExistsAsync(DeleteSnapshotsOption.OnlySnapshots); return(true); }
public void RelativePathNoForwardSlash() { var container = Substitute.For <IContainer>(); var bi = new BlobItem(container, "file.txt"); Assert.AreEqual("file.txt", bi.RelativePath); }
private Dictionary <string, string> ToMetadata(BlobItem blobItem) { return(blobItem.Metadata .Where(pair => pair.Key.Length > MetadataKeyPrefix.Length && pair.Key.StartsWith(MetadataKeyPrefix)) .ToDictionary( pair => pair.Key.Substring(MetadataKeyPrefix.Length), pair => pair.Value)); }
private static async Task HandleDeleteAsync(BlobItem blobItem, BlobContainerClient blobContainerClient) { Console.WriteLine($"Starting: DUPE Deleting {blobItem.Name}"); var blobClient = blobContainerClient.GetBlobClient(blobItem.Name); await blobClient.DeleteAsync(); Console.WriteLine($"Complete: DUPE Deleting {blobItem.Name}"); }
public async Task <string> Create(BlobItem item) { var container = GetContainer(item.Container); var blob = container.GetBlockBlobReference(item.Name); await blob.UploadFromStreamAsync(item.Stream); return(blob.Uri.AbsoluteUri); }
internal static MappingContent BuildMapping(Uri blobUri, BlobItem blobItem, Stream contents) { return(BuildMapping(blobUri, blobItem.Metadata, blobItem.Properties.CreatedOn.Value.LocalDateTime, blobItem.Properties.ContentLength, contents)); }
public async Task DownloadVideoAsync(BlobItem blob, Stream targetStream) { var containerClient = await GetContainerClientAsync(); var blobClient = containerClient.GetBlobClient(blob.Name); var blobDownload = await blobClient.DownloadAsync(); await blobDownload.Value.Content.CopyToAsync(targetStream); }
public async Task <BlobItem> Create(BlobItem item) { var container = GetContainer(item.Container); var blob = container.GetBlockBlobReference(item.Name); await blob.UploadFromStreamAsync(item.Stream); item.Uri = blob.Uri; return(item); }
public BlobItem CopyFile(BlobItem blobItem) { BlobItem copy = (BlobItem)blobItem.Clone(); copy.FileId = this.GenerateId(); _context.BlobItem.Add(copy); //set file in the "db" _context.SaveChanges(); return(copy); }
public static IShareItemInfo Create(ShareItem s, BlobItem b) { return(new IShareItemInfo { ShareId = s.Id, FileName = b.FileName, AvailableUntil = s.ActiveUntil }); }
//[Authorize] public ActionResult CopyFileToAnotherFolder([FromBody] ICopyBlob postData) { BlobItem b = this.handler.CopyFileToAnotherFolder(postData.blobId, postData.folderId); if (b == null) { return(Conflict("Couldn't be created")); } return(Created("File copied", b)); }
public BlobItem GetSingleFile(int id) { BlobItem b = this.model.GetSingleFile(id); if (b == null) { return(null); } return(b); }
public ActionResult Update(BlobItem newFile) { bool r = handler.UpdateBlob(newFile); if (!r) { return(BadRequest("Couldn't update")); } return(Ok()); }
public async Task <string> Create(BlobItem item) { var uploadPath = Path.Combine(_hostingEnv.WebRootPath, item.Container); using (var fileStream = new FileStream(Path.Combine(uploadPath, item.Name), FileMode.Create)) { await item.Stream.CopyToAsync(fileStream); } return($"http://localhost/{item.Container}/{item.Name}"); }
public void GetSingleFile_FileExits_BlobItem() { //arrange BlobItemManager model = new BlobItemManager(); //act BlobItem result = model.GetSingleFile(7); //assert Assert.IsInstanceOfType(result, typeof(BlobItem)); }
public void DeleteShareItem(BlobItem blob) { ShareItem s = _context.ShareItems.Where(s => s.Id == blob.FileId).FirstOrDefault(); if (s == null) { return; } _context.Remove(s); _context.SaveChanges(); }
public static FileModel CreateFile(BlobItem file) { return(new FileModel { Name = file.Name, Path = file.Name, Length = file.Properties.ContentLength, LastModifiedDateTime = file.Properties.LastModified?.DateTime, CreatedDateTime = file.Properties.CreatedOn?.DateTime }); }
private async Task <IFileInfo> GetFileInfoAsync(BlobItem blob, int prefixSections, bool namesOnly, CancellationToken cancellationToken) { var name = string.Join("/", blob.Name.Split("/", StringSplitOptions.RemoveEmptyEntries).Skip(prefixSections)); if (namesOnly) { return(await Task.FromResult(_fileInfoFactory(name, null, null, null, StorageType))); } return(await Task.FromResult(_fileInfoFactory(name, blob.Properties.ContentLength, Convert.ToBase64String(blob.Properties.ContentHash), string.Join("/", name.Split('/').SkipLast(1)), StorageType))); }
public IActionResult CreateBlobItem([FromForm] ICreateBlob postData) { BlobItem b = handler.CreateBlobItem(postData); if (b == null) { return(Conflict("Error! Maybe you did not gave us all the needed info?")); } return(Created("", b)); }
public ActionResult <BlobItem> GetSingleFile([FromQuery] int id) { BlobItem b = handler.GetSingleFile(id); if (b == null) { return(NotFound()); } return(Ok(b)); }
public async Task <ActionResult> EditAsync([Bind("Name,Content")] BlobItem item) { if (ModelState.IsValid) { await storage.UpdateBlobAsync(item.Name, item.Content); return(RedirectToAction("Index")); } return(View(item)); }
public void GetSingleFile_FileNotExits_Null() { //arrange BlobItemManager model = new BlobItemManager(); //act BlobItem result = model.GetSingleFile(99999999); //assert Assert.IsNull(result); }
public async Task Delete() { var container = Substitute.For <IContainer>(); container.Delete("file.txt"); var bi = new BlobItem(container, "/file.txt"); await bi.Delete(); container.Received().Delete("file.txt"); }