cannot able to get file name from azure blob container - azure

I have one azure container which is public to all and i want to retrieve filename present in that public container.
Here is My code.
// Retrieve storage account from connection string.
var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["AzureWebJobsStorage"].ToString());
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(sourceurl); //my path from where i have to get filename
//here i got 400 bad request error.
var blobs = container.ListBlobs(null, true, BlobListingDetails.All).Cast<CloudBlockBlob>().ToList();
foreach (var blockBlob in blobs)
{
Console.WriteLine("Name: " + blockBlob.Name);
Console.WriteLine("Size: " + blockBlob.Properties.Length);
Console.WriteLine("Content type: " + blockBlob.Properties.ContentType);
Console.WriteLine("Download location: " + blockBlob.Uri);
Console.WriteLine("=======================================");
}

Based on your comments, please change your code to something like the following:
CloudBlobContainer container = blobClient.GetContainerReference("vcmobilesiteiislogs");
var blobs = container.ListBlobs("VCMOBILESITE/2015/07/10/10/", true, BlobListingDetails.All).Cast<CloudBlockBlob>().ToList();
Essentially vcmobilesiteiislogs is your container and you want to fetch blobs names of which start with VCMOBILESITE/2015/07/10/10/ (or in other words in that sub folder though I must state that blob storage does not have a concept of folders. You just create an illusion of folder hierarchy by prefixing the blob with some name that you want to use as a folder name).
So you would pass VCMOBILESITE/2015/07/10/10/ as blob prefix when you call ListBlobs method on your container.

Related

Delete Directory containing BLOB from Logic App

The task here is to delete the folders inside the storage account from a Logic App. I am seeking a similar action as "Delete Blob" to delete the folders also. For example, directory structure is like
XYZ -> 2021-06-14 -> filename.json
I want to delete the folder itself but unable to find a direct action for the same. Any work arounds are also accepted.
Here are some links where you have some details about deletion of files or folder inside blob using Logic App
1)https://lucavallarelli.altervista.org/blog/gdpr-logic-app-delete-blob/
2)https://sameeraman.wordpress.com/2017/08/25/logic-apps-delete-files-older-than-x-days-from-a-blob-storage/
Azure Blob Storage does not really have the concept of folders. The hierarchy is very simple: storage account > container > blob.
I have 2 ways for this
WAY - 1
we can delete a specific Blob from the container by using delete method as follows:
public void DeleteBlob()
{
var _containerName = "appcontainer";
string _storageConnection = CloudConfigurationManager.GetSetting("StorageConnectionString");
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(_storageConnection);
CloudBlobClient _blobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer _cloudBlobContainer = _blobClient.GetContainerReference(_containerName);
CloudBlockBlob _blockBlob = _cloudBlobContainer.GetBlockBlobReference("f115a610-a899-42c6-bd3f-74711eaef8d5-.jpg");
//delete blob from container
_blockBlob.Delete();
}
Delete Action will be:
public ActionResult DeleteBlob()
{
imageService.DeleteBlob();
return View();
}
WAY - 2
Listing the blobs within the required container, you can then delete them individually
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("your storage account");
CloudBlobContainer container = storageAccount.CreateCloudBlobClient().GetContainerReference("pictures");
foreach (IListBlobItem blob in container.GetDirectoryReference("users").ListBlobs(true))
{
if (blob.GetType() == typeof(CloudBlob) || blob.GetType().BaseType == typeof(CloudBlob))
{
((CloudBlob)blob).DeleteIfExists();
}
}
For further more analysis you can refer the following links link 1 link 2.

Azure Blob Storage using only the file name, not the entire path

This works exactly as it should. It uses the entire file path to save the blobs in Azure. However, I would like only the file names to be saved directly into the "container" (not the entire hierarchical structure). Is there a good way to do this?
string[] returnedPaths = Open_File_Dialog();
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// container
CloudBlobContainer container = blobClient.GetContainerReference(_jobID);
container.CreateIfNotExists();
foreach (var filePath in returnedPaths)
{
CloudBlockBlob blob = container.GetBlockBlobReference(filePath);
blob.UploadFromFile(filePath);
}
This was all that was needed.
foreach (var filePath in returnedPaths)
{
string fileName = Path.GetFileName(filePath);
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
blob.UploadFromFile(filePath);
}

How to copy an Azure File to an Azure Blob?

I've got some files sitting in an Azure File storage.
I'm trying to programatically archive them to Azure Blobs and I'm not sure how to do this efficiently.
I keep seeing code samples about copying from one blob container to another blob container .. but not from a File to a Blob.
Is it possible to do without downloading the entire File content locally and then uploading this content? Maybe use Uri's or something?
More Info:
The File and Blob containers are in the same Storage account.
The storage account is RA-GRS
Here was some sample code I was thinking of doing .. but it just doesn't feel right :( (pseudo code also .. with validation and checks omitted).
var file = await ShareRootDirectory.GetFileReference(fileName);
using (var stream = new MemoryStream())
{
await file.DownloadToStreamAsync(stream);
// Custom method that basically does:
// 1. GetBlockBlobReference
// 2. UploadFromStreamAsync
await cloudBlob.AddItemAsync("some-container", stream);
}
How to copy an Azure File to an Azure Blob?
We also can use CloudBlockBlob.StartCopy(CloudFile). CloudFile type is also can be accepted by the CloudBlockBlob.StartCopy function.
How to copy CloudFile to blob please refer to document. The following demo code is snippet from the document.
// Parse the connection string for the storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
Microsoft.Azure.CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create a CloudFileClient object for credentialed access to File storage.
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
// Create a new file share, if it does not already exist.
CloudFileShare share = fileClient.GetShareReference("sample-share");
share.CreateIfNotExists();
// Create a new file in the root directory.
CloudFile sourceFile = share.GetRootDirectoryReference().GetFileReference("sample-file.txt");
sourceFile.UploadText("A sample file in the root directory.");
// Get a reference to the blob to which the file will be copied.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("sample-container");
container.CreateIfNotExists();
CloudBlockBlob destBlob = container.GetBlockBlobReference("sample-blob.txt");
// Create a SAS for the file that's valid for 24 hours.
// Note that when you are copying a file to a blob, or a blob to a file, you must use a SAS
// to authenticate access to the source object, even if you are copying within the same
// storage account.
string fileSas = sourceFile.GetSharedAccessSignature(new SharedAccessFilePolicy()
{
// Only read permissions are required for the source file.
Permissions = SharedAccessFilePermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24)
});
// Construct the URI to the source file, including the SAS token.
Uri fileSasUri = new Uri(sourceFile.StorageUri.PrimaryUri.ToString() + fileSas);
// Copy the file to the blob.
destBlob.StartCopy(fileSasUri);
Note:
If you are copying a blob to a file, or a file to a blob, you must use a shared access signature (SAS) to authenticate the source object, even if you are copying within the same storage account.
Use the Transfer Manager:
https://msdn.microsoft.com/en-us/library/azure/microsoft.windowsazure.storage.datamovement.transfermanager_methods.aspx
There are methods to copy from CloudFile to CloudBlob.
Add the "Microsoft.Azure.Storage.DataMovement" nuget package
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.File;
using Microsoft.WindowsAzure.Storage.DataMovement;
private string _storageConnectionString = "your_connection_string_here";
public async Task CopyFileToBlob(string blobContainer, string blobPath, string fileShare, string fileName)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_connectionString);
CloudFileShare cloudFileShare = storageAccount.CreateCloudFileClient().GetShareReference(fileShare);
CloudFile source = cloudFileShare.GetRootDirectoryReference().GetFileReference(fileName);
CloudBlobContainer blobContainer = storageAccount.CreateCloudBlobClient().GetContainerReference(blobContainer);
CloudBlockBlob target = blobContainer.GetBlockBlobReference(blobPath);
await TransferManager.CopyAsync(source, target, true);
}

Deleting entire directories using azure blob storage api

I am using the Azure object store in the following manner:
I have one container, and beneath it many blobs in a directory structure.
I am using Azure Blob Storage api to manage it.
Is there a way to delete an entire directory?
Do I really need to list all the blobs under it and then delete them one by one?
Is there a workaround like deleting all blobs with the same uri prefix (again, without listing them and then deleting them one by one)?
I don't know if there is a new solution, but we did that using https://msdn.microsoft.com/library/microsoft.windowsazure.storage.blob.cloudblobcontainer.listblobs.aspx - if we see what is going on with Fiddler, there are only prefix-ed blobs returned. Please see if that will work for you:
static void GetBlobsByPrefix(string Container, string Prefix)
{
if (!string.IsNullOrEmpty(Prefix))
{
var _Container = GetBlobContainer(Container);
var _Blobs = _Container.ListBlobs(Prefix, true);
foreach (IListBlobItem blob in _Blobs)
{
....
}
}
}
static CloudBlobContainer GetBlobContainer(string container)
{
CloudStorageAccount _StorageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("rus_AzureStorageConnectionString"));
CloudBlobClient _BlobClient = _StorageAccount.CreateCloudBlobClient();
CloudBlobContainer _Container = _BlobClient.GetContainerReference(container);
return _Container;
}
You can delete the container and all your blobs will be deleted. Containers on Azure Storage act as a "folder" for your blobs.

Delete "subpath" from Azure Storage

I know Azure doesn't have actual subpaths, but if I have for example container/projectID/iterationNumber/filename.jpg and I delete a project, how can I delete from ProjectID? Is it possible through coding?
I don't want to use the azure application as I am creating a web app.
Thanks in Advance
EDIT:
This is the code provided by Microsoft to target on specific item:
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
// Retrieve reference to a blob named "myblob.txt".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob.txt");
// Delete the blob.
blockBlob.Delete();
SystemDesignModel
public static SystemDesign returnImageURL(IListBlobItem item)
{
if (item is CloudBlockBlob)
{
var blob = (CloudBlockBlob)item;
return new SystemDesign
{
URL = blob.Uri.ToString(),
};
}
return null;
}
}
As you know, blob storage does not have the concept of subfolders. It has just 2 level hierarchy - container & blobs. So in essence, a subfolder is just a prefix that you attach to blob name. In your example, the actual file you uploaded is filename.jpg but its name from blob storage perspective is projectID/iterationNumber/filename.jpg.
Since there is no concept of subfolder, you just can't delete it like we do on our local computer. However there's a way. Blob storage provides a way to search for blobs starting with a certain blob prefix. So what you have to do is first list all blobs that start with certain prefix (projectID in your case) and then delete the blobs one at a time returned as a result of listing operations.
Take a look at sample code below:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
var container = storageAccount.CreateCloudBlobClient().GetContainerReference("container");
BlobContinuationToken token = null;
do
{
var listingResult = container.ListBlobsSegmented("blob-prefix (projectID in your case)", true, BlobListingDetails.None, 5000, token, null, null);
token = listingResult.ContinuationToken;
var blobs = listingResult.Results;
foreach (var blob in blobs)
{
(blob as ICloudBlob).DeleteIfExists();
Console.WriteLine(blob.Uri.AbsoluteUri + " deleted.");
}
}
while (token != null);

Resources