/// <summary> /// returns the blob name from IListBlobItem /// </summary> public static string GetBlobName(IListBlobItem blobItem) { string blobName = string.Empty; if (blobItem is CloudBlockBlob) { blobName = ((CloudBlockBlob)blobItem).Name; } else if (blobItem is CloudPageBlob) { blobName = ((CloudPageBlob)blobItem).Name; } else if (blobItem is CloudBlobDirectory) { blobName = ((CloudBlobDirectory) blobItem).Uri.ToString(); } return blobName; }
public AzureBlobFileInfo(IListBlobItem blob) { switch (blob) { case CloudBlobDirectory d: Exists = true; IsDirectory = true; Name = ((CloudBlobDirectory)blob).Prefix.TrimEnd('/'); PhysicalPath = d.StorageUri.PrimaryUri.ToString(); break; case CloudBlockBlob b: _blockBlob = b; Name = !string.IsNullOrEmpty(b.Parent.Prefix) ? b.Name.Replace(b.Parent.Prefix, "") : b.Name; Exists = b.Exists(); if (Exists) { b.FetchAttributes(); Length = b.Properties.Length; PhysicalPath = b.Uri.ToString(); LastModified = b.Properties.LastModified ?? DateTimeOffset.MinValue; } else { Length = -1; // IFileInfo.PhysicalPath docs say: Return null if the file is not directly accessible. // (PhysicalPath should maybe also be null for blobs that do exist but that would be a potentially breaking change.) PhysicalPath = null; } break; } }
public static AzureBlob GetAzureBlob(this IListBlobItem item) { var directory = item as CloudBlobDirectory; if (directory != null) { return(new AzureBlob { Name = directory.Prefix, IsDirectory = true, Url = directory.Uri.ToString() }); } var blob = item as CloudBlockBlob; if (blob != null) { return(new AzureBlob { Name = blob.Name.Substring(blob.Name.LastIndexOf('/') == 0 ? 0 : blob.Name.LastIndexOf('/') + 1), IsBlob = true, LastModified = blob.Properties?.LastModified?.UtcDateTime ?? DateTime.UtcNow, Etag = blob.Properties?.ETag, BlobType = blob.Properties?.BlobType.ToString(), ContentType = blob.Properties?.ContentType, Size = blob.Properties?.Length ?? 0, Status = blob.Properties?.LeaseStatus.ToString(), Url = blob.Uri.ToString() }); } return(null); }
} // for testing /// <summary> /// Initializes a new instance of the <see cref="BlobItem"/> class. /// </summary> /// <param name="item">The <see cref="IListBlobItem"/> to build properties from.</param> public BlobItem([NotNull] IListBlobItem item) { Tag = item; RootFolder = item.Container.Name; Path = item.Uri.AbsolutePath; UniqueLeaseName = Guid.NewGuid().ToString(); var index = Path.LastIndexOf("/", StringComparison.InvariantCulture); FileName = Path.Substring(index + 1, Path.Length - index - 1); FileNameWithoutExtension = FileName; index = Path.LastIndexOf(".", StringComparison.InvariantCulture) + 1; if (index > 0) { FileExtension = Path.Substring(index, Path.Length - index).ToLower(CultureInfo.InvariantCulture); FileNameWithoutExtension = FileName.Replace(FileExtension, string.Empty); var extIndex = FileNameWithoutExtension.LastIndexOf(".", StringComparison.InvariantCulture); FileNameWithoutExtension = extIndex > 0 ? FileNameWithoutExtension.Substring(0, extIndex) : FileNameWithoutExtension; } Path = $"{RootFolder}/{FileName}"; }
public static IBlob ToBlob(this IListBlobItem blob) { var blockBlob = blob as CloudBlockBlob; var directoryBlob = blob as CloudBlobDirectory; if (blockBlob != null) { return(new SimpleBlob() { Id = blockBlob.Name, IsVirtualFolder = false, ETag = blockBlob.Properties.ETag, LastModified = blockBlob.Properties.LastModified, UnderlyingBlob = blockBlob, Metadata = null }); } else if (directoryBlob != null) { return(new SimpleBlob() { IsVirtualFolder = true, Id = directoryBlob.Prefix, Body = new MemoryStream(), ETag = null, LastModified = null, Metadata = new Dictionary <string, string>(), UnderlyingBlob = directoryBlob }); } else { throw new NotSupportedException("blob list item not supported"); } }
/// <summary> /// check for specific file names that are not media files that needs to be processed. This file types are specific to Aspera File upload process. return /// true if found in the exemtion list. /// </summary> /// <param name="myBlob">this this is blob reference</param> /// <returns>true if the name found in the exeption list</returns> private static bool checktFilter(IListBlobItem myBlob) { bool filterTrigger = false; string[] uSegments = myBlob.Uri.Segments; string blobName = uSegments[uSegments.Length - 1]; string filterList = MediaButler.Common.Configuration.GetConfigurationValue("FilterPatterns", "MediaButler.Workflow.WorkerRole"); //fix whenyou don't have a filter pattern if (!string.IsNullOrEmpty(filterList)) { string[] list = filterList.Split(','); foreach (var item in list) { if (blobName.IndexOf(item) > -1) { filterTrigger = true; break; } } } return(filterTrigger); }
/// <summary> /// Exctract's a blob item's last modified date. /// </summary> /// <param name="blobItem"> /// The blob item, for which to extract a last modified date. /// </param> /// <returns> /// blobItem's last modified date, or null, of such could not be /// extracted. /// </returns> private DateTime?ExtractBlobItemDate(IListBlobItem blobItem) { if (blobItem == null) { throw new ArgumentNullException("blobItem"); } BlobProperties blobProperties; CloudBlockBlob blockBlob; CloudPageBlob pageBlob; if ((blockBlob = blobItem as CloudBlockBlob) != null) { blobProperties = blockBlob.Properties; } else if ((pageBlob = blobItem as CloudPageBlob) != null) { blobProperties = pageBlob.Properties; } else { blobProperties = null; } if ((blobProperties != null) && blobProperties.LastModified.HasValue) { return(blobProperties.LastModified.Value.DateTime); } return(null); }
private BlobId ToBlobId(IListBlobItem blob, bool attachMetadata) { BlobId id; if (blob is CloudBlockBlob blockBlob) { id = new BlobId(blockBlob.Name, BlobItemKind.File); } else if (blob is CloudAppendBlob appendBlob) { id = new BlobId(appendBlob.Name, BlobItemKind.File); } else if (blob is CloudBlobDirectory dirBlob) { id = new BlobId(dirBlob.Prefix, BlobItemKind.Folder); } else { throw new InvalidOperationException($"unknown item type {blob.GetType()}"); } //attach metadata if we can if (attachMetadata && blob is CloudBlob cloudBlob) { id.Meta = AzureUniversalBlobStorageProvider.GetblobMeta(cloudBlob); } return(id); }
public Node AddNode(IListBlobItem content) { Node newNode = new Node(content); _roots.Add(newNode); return(newNode); }
/// <summary> /// Load's a blob listing's items. /// </summary> /// <param name="segmentLoader"> /// A func for getting the blob listing's next segment. /// </param> /// <returns> /// A concattenation of all the blob listing's resulting segments. /// </returns> private async Task <IEnumerable <IListBlobItem> > LoadBlobItemsAsync( Func <BlobContinuationToken, Task <BlobResultSegment> > segmentLoader) { if (segmentLoader == null) { throw new ArgumentNullException("segmentLoader"); } IEnumerable <IListBlobItem> blobItems = new IListBlobItem[0]; var segment = await segmentLoader(null); while ((segment != null) && (segment.Results != null)) { blobItems = blobItems.Concat(segment.Results); if (segment.ContinuationToken == null) { break; } segment = await segmentLoader(segment.ContinuationToken); } return(blobItems); }
public static Blob ToBlob(string containerName, IListBlobItem nativeBlob) { string GetFullName(string name) => containerName == null ? name : StoragePath.Combine(containerName, name); Blob blob = nativeBlob switch { CloudBlockBlob blockBlob => new Blob(GetFullName(blockBlob.Name), BlobItemKind.File), CloudAppendBlob appendBlob => new Blob(GetFullName(appendBlob.Name), BlobItemKind.File), CloudBlob cloudBlob => new Blob(GetFullName(cloudBlob.Name), BlobItemKind.File), CloudBlobDirectory dirBlob => new Blob(GetFullName(dirBlob.Prefix), BlobItemKind.Folder), _ => throw new InvalidOperationException($"unknown item type {nativeBlob.GetType()}") }; //attach metadata if we can if (nativeBlob is CloudBlob metaBlob) { //no need to fetch attributes, parent request includes the details //await cloudBlob.FetchAttributesAsync().ConfigureAwait(false); AzConvert.AttachBlobMeta(blob, metaBlob); } return(blob); }
private async Task <List <string> > RecursiveListFile(IListBlobItem listBlobItem) { switch (listBlobItem) { case CloudBlockBlob blob: return(new List <string> { blob.Name }); case CloudBlobDirectory directory: BlobContinuationToken continuationToken = null; var files = new List <string>(); do { var response = await directory.ListBlobsSegmentedAsync(continuationToken); continuationToken = response.ContinuationToken; foreach (var result in response.Results) { files.AddRange(await RecursiveListFile(result)); } } while (continuationToken != null); return(files); default: return(new List <string>()); } }
public void CloudBlobDirectoryFlatListingWithPrefix() { foreach (String delimiter in Delimiters) { CloudBlobClient client = GenerateCloudBlobClient(); client.DefaultDelimiter = delimiter; string name = GetRandomContainerName(); CloudBlobContainer container = client.GetContainerReference(name); try { container.Create(); if (CloudBlobDirectorySetupWithDelimiter(container, delimiter)) { BlobContinuationToken token = null; CloudBlobDirectory directory = container.GetDirectoryReference("TopDir1"); List <IListBlobItem> list1 = new List <IListBlobItem>(); do { BlobResultSegment result1 = directory.ListBlobsSegmented(token); token = result1.ContinuationToken; list1.AddRange(result1.Results); }while (token != null); Assert.IsTrue(list1.Count == 3); IListBlobItem item11 = list1.ElementAt(0); Assert.IsTrue(item11.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "Blob1")); IListBlobItem item12 = list1.ElementAt(1); Assert.IsTrue(item12.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter)); IListBlobItem item13 = list1.ElementAt(2); Assert.IsTrue(item13.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter)); CloudBlobDirectory midDir2 = (CloudBlobDirectory)item13; List <IListBlobItem> list2 = new List <IListBlobItem>(); do { BlobResultSegment result2 = midDir2.ListBlobsSegmented(true, BlobListingDetails.None, null, token, null, null); token = result2.ContinuationToken; list2.AddRange(result2.Results); }while (token != null); Assert.IsTrue(list2.Count == 2); IListBlobItem item41 = list2.ElementAt(0); Assert.IsTrue(item41.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir1" + delimiter + "EndBlob1")); IListBlobItem item42 = list2.ElementAt(1); Assert.IsTrue(item42.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir2" + delimiter + "EndBlob2")); } } finally { container.DeleteIfExists(); } } }
public static Models.CloudBlob ToModel(this IListBlobItem blobItem, string containerName, string accountName) { var blob = blobItem as Microsoft.WindowsAzure.StorageClient.CloudBlob; var sas = blob.GetSharedAccessSignature(new SharedAccessPolicy(), "readonly"); var uriBuilder = new UriBuilder(blob.Uri) { Query = sas.TrimStart('?') }; var blobUri = uriBuilder.Uri; var blobName = blobUri.LocalPath; // When using the Storage Emulator the first segment is the dev account, not the container. var devaccount = CloudStorageAccount.DevelopmentStorageAccount.Credentials.AccountName; if (accountName.Equals(devaccount, StringComparison.OrdinalIgnoreCase) && blobName.StartsWith(string.Format(CultureInfo.InvariantCulture, "/{0}", devaccount), StringComparison.OrdinalIgnoreCase)) { blobName = blobName.Remove(0, devaccount.Length + 1); } return(new Models.CloudBlob { // Remove container name. Name = blobName.Remove(0, containerName.Length + 1).TrimStart('/'), Uri = blobUri }); }
public static CloudFile CreateFromIListBlobItem(IListBlobItem item, string containerName) { if (item is CloudBlockBlob) { var blob = (CloudBlockBlob)item; blob.FetchAttributesAsync().Wait(5000); //Gets the properties & metadata for the blob. string UploadedBy; var result = blob.Metadata.TryGetValue("UploadedBy", out UploadedBy); if (!result) { return(null); } string CreatedAt; var result2 = blob.Metadata.TryGetValue("CreatedAt", out CreatedAt); if (!result2) { return(null); } return(new CloudFile { FileName = blob.Name, URL = blob.Uri.ToString(), Size = blob.Properties.Length, ContentType = blob.Properties.ContentType, CreatedAt = DateTimeOffset.Parse(CreatedAt), UploadedBy = UploadedBy, ContainerName = containerName }); } return(null); }
public async Task CreateTextFile() { // arrange CloudBlobClient client = _azureStorageResource.CreateBlobClient(); string containerName = Guid.NewGuid().ToString("N"); await client.GetContainerReference(containerName).CreateIfNotExistsAsync(); var storage = new FileStorage(client, containerName); IFileContainer container = await storage.CreateContainerAsync("abc"); // act await container.CreateTextFileAsync("def", "ghi"); // assert IListBlobItem item = client.GetContainerReference(containerName) .GetDirectoryReference("abc") .ListBlobs() .SingleOrDefault(); Assert.NotNull(item); Assert.Equal( $"/devstoreaccount1/{containerName}/abc/def", item.Uri.LocalPath); byte[] buffer = new byte[12]; int buffered = await client.GetContainerReference(containerName) .GetDirectoryReference("abc") .GetBlobReference("def") .DownloadToByteArrayAsync(buffer, 0); Assert.Equal("ghi", Encoding.UTF8.GetString(buffer, 0, buffered)); }
/// <summary> /// Exctract's a blob item's last modified date. /// </summary> /// <param name="blobItem"> /// The blob item, for which to extract a last modified date. /// </param> /// <returns> /// blobItem's last modified date, or null, of such could not be /// extracted. /// </returns> public static DateTime? ExtractBlobItemDate(IListBlobItem blobItem) { if (blobItem == null) { throw new ArgumentNullException("blobItem"); } BlobProperties blobProperties; CloudBlockBlob blockBlob; CloudPageBlob pageBlob; if ((blockBlob = blobItem as CloudBlockBlob) != null) { blobProperties = blockBlob.Properties; } else if ((pageBlob = blobItem as CloudPageBlob) != null) { blobProperties = pageBlob.Properties; } else { blobProperties = null; } if ((blobProperties != null) && blobProperties.LastModified.HasValue) { return blobProperties.LastModified.Value.DateTime; } return null; }
internal string GetName(IListBlobItem item) { string name = item.Uri.ToString(); string parent = item.Parent.Uri.ToString(); if (parent.EndsWith("/")) { name = name.Substring(parent.Length); } else { name = name.Substring((parent.Length) + 1); } if (!(item.GetType() == typeof(CloudBlob))) { if (name.EndsWith("/")) { name = name.Substring(0, name.Length - 1); } else { name = name.Substring(0, name.Length); } } return(name); }
public async Task DeleteContainer() { // arrange CloudBlobClient client = _azureStorageResource.CreateBlobClient(); string containerName = Guid.NewGuid().ToString("N"); await client.GetContainerReference(containerName).CreateIfNotExistsAsync(); var storage = new FileStorage(client, containerName); IFileContainer container = await storage.CreateContainerAsync("abc"); await container.CreateTextFileAsync("def", "ghi"); IListBlobItem item = client.GetContainerReference(containerName) .GetDirectoryReference("abc") .ListBlobs() .SingleOrDefault(); Assert.NotNull(item); // act await container.DeleteAsync(); // assert Assert.False(client.GetContainerReference(containerName) .GetDirectoryReference("abc") .ListBlobs() .Any()); }
public void CloudBlobDirectoryFlatListing() { foreach (String delimiter in Delimiters) { CloudBlobClient client = GenerateCloudBlobClient(); client.DefaultDelimiter = delimiter; string name = GetRandomContainerName(); CloudBlobContainer container = client.GetContainerReference(name); try { container.Create(); if (CloudBlobDirectorySetupWithDelimiter(container, delimiter)) { IEnumerable <IListBlobItem> list1 = container.ListBlobs("TopDir1" + delimiter, false, BlobListingDetails.None, null, null); List <IListBlobItem> simpleList1 = list1.ToList(); ////Check if for 3 because if there were more than 3, the previous assert would have failed. ////So the only thing we need to make sure is that it is not less than 3. Assert.IsTrue(simpleList1.Count == 3); IListBlobItem item11 = simpleList1.ElementAt(0); Assert.IsTrue(item11.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "Blob1")); IListBlobItem item12 = simpleList1.ElementAt(1); Assert.IsTrue(item12.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter)); IListBlobItem item13 = simpleList1.ElementAt(2); Assert.IsTrue(item13.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter)); IEnumerable <IListBlobItem> list2 = container.ListBlobs("TopDir1" + delimiter + "MidDir1", true, BlobListingDetails.None, null, null); List <IListBlobItem> simpleList2 = list2.ToList(); Assert.IsTrue(simpleList2.Count == 2); IListBlobItem item21 = simpleList2.ElementAt(0); Assert.IsTrue(item21.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir1" + delimiter + "EndBlob1")); IListBlobItem item22 = simpleList2.ElementAt(1); Assert.IsTrue(item22.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir2" + delimiter + "EndBlob2")); IEnumerable <IListBlobItem> list3 = container.ListBlobs("TopDir1" + delimiter + "MidDir1" + delimiter, false, BlobListingDetails.None, null, null); List <IListBlobItem> simpleList3 = list3.ToList(); Assert.IsTrue(simpleList3.Count == 2); IListBlobItem item31 = simpleList3.ElementAt(0); Assert.IsTrue(item31.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir1" + delimiter)); IListBlobItem item32 = simpleList3.ElementAt(1); Assert.IsTrue(item32.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir2" + delimiter)); } } finally { container.DeleteIfExists(); } } }
private async Task<Node> CreateNode(IListBlobItem listBlobItem) { var blob = (CloudBlockBlob) listBlobItem; var memStream = new MemoryStream(); await blob.DownloadToStreamAsync(memStream).ConfigureAwait(false); var node = this.nodeSerializer.Deserialize(memStream.ToArray()); return node; }
public NamespaceListWrapper(IListBlobItem srcItem) { this.SourceItem = srcItem; if (srcItem is CloudBlockBlob) { this.NamespaceBlob = new NamespaceBlob((CloudBlockBlob)srcItem); } }
private static BlobDetailDto GetBlobDetailFromBlob(IListBlobItem cloudItem, ICloudBlob blob) => new BlobDetailDto { FileName = blob.Name, ContentType = blob.Properties.ContentType, CreatedTime = blob.Properties.Created, LastModified = blob.Properties.LastModified, DownloadUrl = cloudItem.Uri.AbsoluteUri };
/// <summary> /// Is it a standalone file? Versus a subdirectory? /// </summary> /// <param name="b">the item to check</param> /// <returns>true if the item resides at /Incoming - so a stand-alone asset</returns> public static bool isSimpleFile(IListBlobItem b) { // true if item is not in a first-level subdirectory //TODO: should be more stringent check - e.g. check filetype var ret = b.Parent.Uri.ToString().EndsWith(parentSuffix, StringComparison.InvariantCultureIgnoreCase); return(ret); }
/// <summary> /// Extract the path portion for the Storage URI -- e.g. from /// https://myaccount.blob.core.windows.net/myContainer/myFolder/myblob.dat /// to /// /myFolder/myblob.dat /// </summary> /// <param name="b"></param> /// <returns></returns> public static string ExtractBlobPath(IListBlobItem b) { // remove the storage prefix + the container from the URI var parts = b.Uri.LocalPath.Split('/'); // parts[1] container parts[2] folder parts[>2] blobfile var pathPortion = String.Join("/", parts, 2, parts.Length - 2); return(pathPortion); }
/// <summary> /// Extract the path portion for the Storage URI -- e.g. from /// https://myaccount.blob.core.windows.net/myContainer/myFolder/myblob.dat /// to /// /myFolder/myblob.dat /// </summary> /// <param name="b"></param> /// <returns></returns> public static string ExtractBlobPath(IListBlobItem b) { // remove the storage prefix + the container from the URI var parts = b.Uri.LocalPath.Split('/'); // parts[1] container parts[2] folder parts[>2] blobfile var pathPortion = String.Join("/", parts, 2, parts.Length - 2); return pathPortion; }
private static BlobItem GetBlobItem(IListBlobItem blobItem) { if (!(blobItem is CloudBlockBlob blob)) { return(null); } return(new BlobItem(blob)); }
public AzureLocation(AzureDriveInfo driveInfo, Path path, IListBlobItem cloudItem) { _driveInfo = driveInfo; Path = path; Path.Validate(); if (cloudItem != null) { _cloudItem = new AsyncLazy<IListBlobItem>(() => { if (cloudItem is CloudBlockBlob) { (cloudItem as CloudBlockBlob).FetchAttributes(); } return cloudItem; }); } else { if (IsRootNamespace || IsAccount || IsContainer) { // azure namespace mount. _cloudItem = new AsyncLazy<IListBlobItem>(() => null); return; } _cloudItem = new AsyncLazy<IListBlobItem>(() => { if (CloudContainer == null) { return null; } // not sure if it's a file or a directory. if (path.EndsWithSlash) { // can't be a file! CloudContainer.GetDirectoryReference(Path.SubPath); } // check to see if it's a file. ICloudBlob blobRef = null; try { blobRef = CloudContainer.GetBlobReferenceFromServer(Path.SubPath); if (blobRef != null && blobRef.BlobType == BlobType.BlockBlob) { blobRef.FetchAttributes(); return blobRef; } } catch { } // well, we know it's not a file, container, or account. // it could be a directory (but the only way to really know that is to see if there is any files that have this as a parent path) var dirRef = CloudContainer.GetDirectoryReference(Path.SubPath); if (dirRef.ListBlobs().Any()) { return dirRef; } blobRef = CloudContainer.GetBlockBlobReference(Path.SubPath); if (blobRef != null && blobRef.BlobType == BlobType.BlockBlob) { return blobRef; } // it really didn't match anything, we'll return the reference to the blob in case we want to write to it. return blobRef; }); _cloudItem.InitializeAsync(); } }
private void CopyBlobItem(IListBlobItem blobItem, CloudBlobContainer sourceBlobContainer, CloudBlobContainer destinationBlobContainer) { if (blobItem.GetBlobType() == Extensions.BlobType.Directory) { CopyBlobDirectory(sourceBlobContainer, destinationBlobContainer, blobItem as CloudBlobDirectory); } else { var blobName = blobItem.Uri.Segments[^ 1];
/// <summary> /// Adjust the path portion (between the container and the basename /// parts of the name) to the new path. E.g. from /// https://myaccount.blob.core.windows.net/myContainer/myFolder/myblob.dat /// to /// /myFolderNew/myblob.dat /// </summary> /// <param name="b">The source blob</param> /// <param name="newPath">The new path portion</param> /// <returns>The adjusted URI string.</returns> public static string AdjustPath(IListBlobItem b, string newPath) { // TODO: add timestamp adding management // remove the storage prefix + the container from the URI var parts = b.Uri.LocalPath.Split('/'); // parts[1] container parts[2] folder parts[>2] blobfile var pathPortion = newPath + "/" + String.Join("/", parts, 3, parts.Length - 3); return(pathPortion); }
/// <summary> /// Adjust the path portion (between the container and the basename /// parts of the name) to the new path. E.g. from /// https://myaccount.blob.core.windows.net/myContainer/myFolder/myblob.dat /// to /// /myFolderNew/myblob.dat /// </summary> /// <param name="b">The source blob</param> /// <param name="newPath">The new path portion</param> /// <returns>The adjusted URI string.</returns> public static string AdjustPath(IListBlobItem b, string newPath) { // TODO: add timestamp adding management // remove the storage prefix + the container from the URI var parts = b.Uri.LocalPath.Split('/'); // parts[1] container parts[2] folder parts[>2] blobfile var pathPortion = newPath + "/" + String.Join("/", parts, 3, parts.Length - 3); return pathPortion; }
public BlobBlockTextContentReader(IListBlobItem item, ContentReaderDynamicParameters contentReaderDynamicParameters) { _contentReaderDynamicParameters = contentReaderDynamicParameters; var blob = (CloudBlockBlob)item; _reader = new StreamReader(blob.OpenRead()); }
private void DeleteIfAged(DateTime olderThanUtc, IListBlobItem blobDefinition) { CloudBlockBlob blob = LoadBlob(blobDefinition.Uri.ToString()); if (IsBlobAged(blob, olderThanUtc)) { DeleteIfExists(blob); } }
private bool ShouldDeleteBlob(IListBlobItem blobInterface, DateTime cutoffTime) { // We delete the blob if it is old enough ICloudBlob blob = (ICloudBlob)blobInterface; return(((false == blob.Properties.LastModified.HasValue) || (blob.Properties.LastModified.Value.CompareTo(cutoffTime) > 0)) ? false : true); }
private IListBlobItem NewBlobItem(string uri) { IListBlobItem blobItem = Substitute.For <IListBlobItem>(); blobItem .Uri .Returns(new Uri(new Uri(@"http://baseUri"), new Uri(uri, UriKind.Relative))); return(blobItem); }
public static string RelativePath(this IListBlobItem item) { var filterUrl = item.Container.Uri.AbsoluteUri; if (!filterUrl.EndsWith("/")) { filterUrl = filterUrl + "/"; } return(item.Uri.AbsoluteUri.Replace(filterUrl, string.Empty)); }
protected BlobUrl CreateBlobFolder(IListBlobItem blob) { var urlSegments = UrlSegments(blob.Uri.LocalPath); BlobUrl blobUrl = new BlobUrl { Name = urlSegments.Last(), Url = string.Join("/", urlSegments.Skip(1).Take(urlSegments.Count - 1)) }; return blobUrl; }
public static CloudFile CreateFromIListBlobItem(IListBlobItem item) { if (item is CloudBlockBlob) { var blob = (CloudBlockBlob)item; return new CloudFile { FileName = blob.Name, URL = blob.Uri.ToString(), Size = blob.Properties.Length }; } return null; }
public static IAsyncListBlobItem FromIListBlobItem(IListBlobItem result) { var blob = result as ICloudBlob; if (blob != null) { return AsyncCloudBlobHelpers.FromICloudBlob(blob); } var dir = result as CloudBlobDirectory; if (dir != null) { return new AsyncCloudBlobDirectory(dir); } // TODO: maybe better to throw an exception return new AsyncListBlobItemBase<IListBlobItem>(result); }
public static DocumentProperties GetDocFromBlob(IListBlobItem blob, ICloudBlob cloudBlob, string blobType) { if (cloudBlob.Properties != null) { return new DocumentProperties() { id = string.Format("id{0}", blob.Uri.AbsoluteUri.GetHashCode()), url = blob.Uri.AbsoluteUri, name = cloudBlob.Name, blob_type = blobType, content_type = cloudBlob.Properties.ContentType, size = cloudBlob.Properties.Length, last_modified = cloudBlob.Properties.LastModified.Value, container = blob.Container.Name, downloads_counter = 1 }; } else return null; }
/// <summary> /// Is it a standalone file? Versus a subdirectory? /// </summary> /// <param name="b">the item to check</param> /// <returns>true if the item resides at /Incoming - so a stand-alone asset</returns> public static bool isSimpleFile(IListBlobItem b) { // true if item is not in a first-level subdirectory //TODO: should be more stringent check - e.g. check filetype var ret = b.Parent.Uri.ToString().EndsWith(parentSuffix, StringComparison.InvariantCultureIgnoreCase); return ret; }
/// <summary> /// Load's a blob listing's items. /// </summary> /// <param name="segmentLoader"> /// A func for getting the blob listing's next segment. /// </param> /// <returns> /// A concattenation of all the blob listing's resulting segments. /// </returns> public static async Task<IEnumerable<IListBlobItem>> LoadBlobItemsAsync( Func<BlobContinuationToken, Task<BlobResultSegment>> segmentLoader) { if (segmentLoader == null) { throw new ArgumentNullException("segmentLoader"); } IEnumerable<IListBlobItem> blobItems = new IListBlobItem[0]; BlobResultSegment segment = await segmentLoader(null); while ((segment != null) && (segment.Results != null)) { blobItems = blobItems.Concat(segment.Results); if (segment.ContinuationToken == null) { break; } segment = await segmentLoader(segment.ContinuationToken); } return blobItems; }
private bool FilterLessThanTime(IListBlobItem blobItem, DateTime minTime) { CloudBlockBlob blockBlob; if ((blockBlob = blobItem as CloudBlockBlob) != null) { if (blockBlob.Properties?.LastModified != null && (blockBlob.Properties.LastModified.Value.LocalDateTime >= minTime)) { return true; } } return false; }
private static string GetRawMarkerForBlob(IListBlobItem blob) { if (blob is ICloudBlob && ((ICloudBlob)blob).IsSnapshot) { return blob.Uri.AbsolutePath + '\n' + ((ICloudBlob)blob).SnapshotTime.Value.ToString("o"); } else { // Append this suffix to ensure that non-snapshots are ordered AFTER snapshot blobs return blob.Uri.AbsolutePath + "\nzzzz"; } }
/// <summary> /// Is it a standalone file? Versus a subdirectory? /// </summary> /// <param name="b"></param> /// <returns></returns> public static bool isControlFile(IListBlobItem b) { return b.Uri.ToString().EndsWith(Configuration.ControlFileSuffix, StringComparison.InvariantCultureIgnoreCase); // true if matches the name pattern }
/// <summary> /// Extract the path portion for the Storage URI -- e.g. from /// https://htestseq.blob.core.windows.net/hli-0008-vhd-backups/myFolder/myblob.dat /// to /// /myFlder/myblob.dat /// </summary> /// <param name="b"></param> /// <returns></returns> public static string ExtractBlobPath(IListBlobItem b) { return BlobUtilities.ExtractBlobPath(b); /* // remove the storage prefix + the container from the URI var parts = b.Uri.LocalPath.Split('/'); // remove the container in [0] var pathPortion = String.Join("/", parts, 2, parts.Length - 2); return pathPortion; */ }
/// <summary> /// Adjust the path portion (between the container and the basename /// parts of the name) to the new path. /// e.g., /// </summary> /// <param name="b">The source blob</param> /// <param name="newPath">The new path portion</param> /// <returns>The adjusted URI string.</returns> public static string AdjustPath(IListBlobItem b, string newPath) { return BlobUtilities.AdjustPath(b, newPath); /* // remove the storage prefix + the container from the URI var parts = b.Uri.LocalPath.Split('/'); // remove the container in [0] var pathPortion = newPath + "/" + String.Join("/", parts, 3, parts.Length - 3); return pathPortion; */ }
public bool Blob_Has_SubDirectories(IListBlobItem item) { return item.GetType() == typeof(CloudBlobDirectory) ? true : false; }
private StorageListItem GetStorageListItem(IListBlobItem listBlobItem) { var lastModified = (listBlobItem as CloudBlockBlob)?.Properties.LastModified?.UtcDateTime; return new StorageListItem(listBlobItem.Uri, lastModified); }
public static string GetMarkerForBlob(IListBlobItem blob) { return GetMarker(GetRawMarkerForBlob(blob)); }
internal static IStorageListBlobItem ToStorageListBlobItem(IStorageBlobContainer parent, IListBlobItem sdkItem) { if (sdkItem == null) { return null; } ICloudBlob sdkBlob = sdkItem as ICloudBlob; if (sdkBlob != null) { return ToStorageBlob(parent, sdkBlob); } else { return new StorageBlobDirectory(parent, (CloudBlobDirectory)sdkItem); } }
internal static void MoveBlob(IListBlobItem blob, string topath = "", string currentpath = "") { // Retrieve stroage account from connection string CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString")); // Create the blob client CloudBlobClient client = storageAccount.CreateCloudBlobClient(); CloudBlobDirectory toFolder = null; CloudBlobContainer parentContainer = null; string filepath = ""; if (blob.GetType() == typeof(CloudPageBlob)) { CloudPageBlob bblob = (CloudPageBlob)blob; filepath = bblob.Name.Substring(bblob.Name.IndexOf("/") + 1); } else { CloudBlockBlob bblob = (CloudBlockBlob)blob; filepath = bblob.Name.Substring(bblob.Name.IndexOf("/") + 1); } if (topath.Contains('/')) { List<string> paths = topath.Split('/').ToList(); string parent = paths.FirstOrDefault(); paths.RemoveAt(0); string folder = string.Join("/", paths.ToArray()); parentContainer = client.GetContainerReference(parent); toFolder = parentContainer.GetDirectoryReference(folder); if(blob.GetType() == typeof(CloudPageBlob)) { CloudPageBlob pblob = (CloudPageBlob)blob; CloudPageBlob newblob = toFolder.GetPageBlobReference(filepath); newblob.StartCopyFromBlob(pblob); pblob.DeleteIfExists(); } else { CloudBlockBlob bblob = (CloudBlockBlob)blob; CloudBlockBlob newblob = toFolder.GetBlockBlobReference(filepath); newblob.StartCopyFromBlob(bblob); bblob.DeleteIfExists(); } } else { parentContainer = client.GetContainerReference(topath); if (blob.GetType() == typeof(CloudPageBlob)) { CloudPageBlob pblob = (CloudPageBlob)blob; filepath = pblob.Name.Substring(pblob.Name.IndexOf("/") + 1); CloudPageBlob newblob = parentContainer.GetPageBlobReference(filepath); newblob.StartCopyFromBlob(pblob); pblob.DeleteIfExists(); } else { CloudBlockBlob bblob = (CloudBlockBlob)blob; filepath = bblob.Name.Substring(bblob.Name.IndexOf("/") + 1); CloudBlockBlob newblob = parentContainer.GetBlockBlobReference(filepath); newblob.StartCopyFromBlob(bblob); bblob.DeleteIfExists(); } } }