Listing the files, which are inside a Blob? - azure

Just started working on with Windows Azure and Mapreduce. I've already created an HDInsight cluster within the Azure Portal and also created a Block Blob using the Azure Explorer named BlobFiles.
What I wanted to know is, is there a way of listing all the files within a Blob which is within a Blob Container? In other words if I'm uploading text files as well as images to the same Blob, how could I list those text files as well as those images programmatically or in another way?
Searched for a solution, but still couldn't come across of one. I'm able to see the content of the file individually using the Azure Explorer, where as um unable to find a way to see or display the files within that particular Blob!
Edited:
This is what I've tried to display the Blobs:
// 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("fyptest1");
// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");
lboxblob.ItemsSource = container.ListBlobs();//lboxblob is the name of my listbox
When I tried the above code it displays something like Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob, and so on for a few number of times.
Any help would be appreciated.

Each Azure storage "blob" is a "file." You want to list the "blobs" in a "blob container," most likely.
To do this, use the CloudBlobContainer.ListBlobs() method.
https://msdn.microsoft.com/en-us/library/azure/dd135734.aspx

That is because the binding would end up calling ToString on every list item returned, which just displays the type name. If you would like to see URIs in your list box instead, please change the last line to:
lboxblob.ItemsSource = container.ListBlobs().Select(b => b.Uri);
But please refer to the other answer for the correct terminology. As others mentioned, blobs do not contain other objects like files.

Related

Error with Azure Blob Storage Starter API for Spring boot in Java

I have a issue trying to delete a "folder" (blob) from Azure Blob Storage. I have a container and inside that container multiple "folders" (blobs) than contain a big amount of "files" blobs inside.
So, for example, if I have a file called test.pdf inside a blob "folder" called 1234 and I go to search the "file" blob /1234/test.pdf, I'm able to find it, download it or delete it. But if I search for the blob "folder" /1234/ I always get an error saying that the blob doesn't exist "404 BlobNotFound The specified blob does not exist".
This is the code I'm using:
BlobURL blobURL = containerUrl.createBlobURL(folderName+"/"+fileName);
blobURL.download().blockingGet();
blobURL.delete().blockingGet(); //It Works
BlobURL blobURL = containerUrl.createBlobURL(folderName+"/");
blobURL.download().blockingGet();
blobURL.delete().blockingGet(); // Did not works
BlobURL blobURL = containerUrl.createBlobURL(folderName);
blobURL.download().blockingGet();
blobURL.delete().blockingGet(); // Did not works
Any advice? Thanks in advance!
If you are using createBlobUrl it should be a valid url of the blob which refers to the actual blob. If you want to access all the blobs inside a contianer, you should use
var blobList= container.ListBlobs(prefix: "folderName/", useFlatBlobListing: true);

Azure Blob Storage Temporary File URL

I have saved pdf files in azure blob storage blob, I want to show these files on my website but when a file render on html its link should be deactivated means no one can use that link to download the file again. Is this possible in azure blob storage?
You can use the blob policy to make it:
CloudStorageAccount account = CloudStorageAccount.Parse("yourStringConnection");
CloudBlobClient serviceClient = account.CreateCloudBlobClient();
var container = serviceClient.GetContainerReference("yourContainerName");
container
.CreateIfNotExistsAsync()
.Wait();
CloudBlockBlob blob = container.GetBlockBlobReference("test/helloworld.txt");
blob.UploadTextAsync("Hello, World!").Wait();
SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy();
// define the expiration time
policy.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(1);
// define the permission
policy.Permissions = SharedAccessBlobPermissions.Read;
// create signature
string signature = blob.GetSharedAccessSignature(policy);
// get full temporary uri
Console.WriteLine(blob.Uri + signature);
If I understand correctly, you're looking for single use links to Azure Blobs. Natively this feature is not available in Azure Storage. You would need to write code to implement something like this where you would keep track of the number of times a link has been used and in case the limit exceeds, you will not process that link.

Azure blob container listBlobs to fetch files with a specific extension in java

I've a java code where I list blobs and filter in for loop to fetch only files with a specific extension. Is there a way to query/request container to return just blobs with specific extension? I don't want to loop through all the blobs in the container to do this.
Is there a way to query/request container to return just blobs with
specific extension?
Unfortunately no. You can't query blob storage to return blobs with a particular extension.
I don't want to loop through all the blobs in the container to do
this.
If you're using Storage REST API, that's the only way to do it. You will need to list blobs in the container and then loop through the blobs and do the filtering based on extension (or any other criteria) on the client side.
Possible Solution
One possible solution would be to use Azure Search Service and have your blob metadata indexed there. Then you should be able to search for a particular extension and get the list of blobs.
You can use ListBlobsOptions() with setPrefix to filter based on prefix as shown below
ListBlobsOptions options=new ListBlobsOptions();
options.setPrefix("<<prefix>>");
Duration fromDays = Duration.of(10, ChronoUnit.MINUTES);
BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient("containerName");
for (BlobItem blobItem : containerClient.listBlobs(options,fromDays)) {
System.out.println("\t" + blobItem.getName());
}
This is all you need
BlobContainer.ListBlobs(prefix, useFlatBlobListing: true, blobListingDetails: BlobListingDetails.None)
.OfType<CloudBlockBlob>()
.Where(x=> x.Name.EndsWith(".json"));
The prefix is just a partial path where search.
The result is an IEnumerable<CloudBlockBlob>.

Check if Blob of unknown Blob type exists

I've inherited a project built using the Azure Storage Client 1.7 and am upgrading it as Microsoft have announced that this will no longer be supported from December this year.
References to the files in Blob storage are stored in a database with the following fields:
FilePath - a string in the form of uploadfiles/xxx/yyy/Image-20140117170146.jpg
FileURI - A string in the form of https://zzz.blob.core.windows.net/uploadfiles/xxx/yyy/Image-20140117170146.jpg
GetBlobReferenceFromServer will throw an exception if the file doesn't exist, so it seems you should use GetBlockBlobReference if you know the container and the Blob type.
So my question(s):
Can I assume any Blobs currently uploaded (using StorageClient 1.7) will be BlockBlobs?
As I need to know the container name to call GetBlockBlobReference can I reliably say that in the examples above my container would always be uploadfiles
Can I assume any Blobs currently uploaded (using StorageClient 1.7)
will be BlockBlobs?
Though you can't be 100% sure that the blobs uploaded via Storage Client library 1.7 are Blob Blobs because 1.7 also supported Page Blobs however you can make some intelligent guesses. For example, if the files are image files and other commonly used files (pdf, document etc.), you can assume that they are block blobs. Typically you would see vhd files uploaded as page blobs. Again if these are uploaded by the users of your application, more than likely they are block blobs.
Having said this, I think you should use GetBlobReferenceFromServer method. What you could do is list all blobs from the database and for each of them call GetBlobReferenceFromServer method. If the blob exists, then you will get the blob type. If the blob doesn't exist, this method will give you an error. This would be the quickest way to identify the blob type of existing entries in the database. If you want, you can store the blob type back in the database along with existing record if you find both block and page blobs when you check the blob type so that if in future you need to decide between creating a CloudBlockBlob or CloudPageBlob reference, you can look at this field.
As I need to know the container name to call GetBlockBlobReference can
I reliably say that in the examples above my container would always be
uploadfiles
Yes. In the examples you listed above, you can say that the blob container is upload files.

Azure - is one 'block blob' seen as one file?

Question background:
This may be a simple question but I cant find an answer to it. I've just started using Azure storage (for storing images) and want to know if one 'blob' holds a maximum of one file?
This is my container called fmfcpics:
Within the container I have a block blob named myBlob and within this I have one image:
Through the following code, if I upload another image file to the myBlob block blob then it overwrites the image already in there:
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");
using (var fileStream = System.IO.File.OpenRead(#"C:\Users\Me\Pictures\Image1.jpg"))
{
blockBlob.UploadFromStream(fileStream);
}
Is this overwriting correct? Or should I be able to store multiple files at the myBlob?
Each blob is a completely separate entity, direct-addressable via uri:
http(s)://storageaccountname.blob.core.windows.net/containername/blobname
If you want to manage multiple entities (such as image jpg's in your case), you would upload each one to a separate blob name (and you're free to store as many as you want within a single container, and you may have as many containers as you want).
Note: These are block blobs. There are also page blobs that have random-access capability, and this is the basis for vhd storage (and in that case, the vhd would have a formatted file system within it, with multiple files).
In blob Azure Documentation you understand how blob service works and the concepts about this storage service.
In some minutes you can use the service easily

Resources