Azure Storage Emulator - How to specify hierarchical path? - azure

I'm using Azure Storage Emulator 4.1.0.0 and VS 2013.
How can I upload a file from a dev box and specify the hierarchical path on Azure Blob storage?
I'm developing locally with VS 2013 & Azure Storage Emulator 4.1.0.0.
I'd like to upload a file but specify the Blob's hierarchical path to the file.
const string ImageToUpload = #"C:\Users\Me\Documents\Visual Studio 2013\Projects\AzureCloudService1\DataBlobStorage1\HelloWorld2.png";
CloudBlockBlob blockBlob = container.GetBlockBlobReference(ImageToUpload);
await blockBlob.UploadFromFileAsync(ImageToUpload, FileMode.Open).ConfigureAwait(configureawait);
This code does upload the file, but winds up storing the source's physical path to the file. How do I specify to Azure Blob storage the desired hierarchical path?
Thx!

Imagine you want the path to be AzureCloudService1\DataBlobStorage1\HelloWorld2.png in your container, this is what you would have to do:
CloudBlockBlob blockBlob = container.GetBlockBlobReference("AzureCloudService1/DataBlobStorage1/HelloWorld2.png");

Ah... GetBlockBlobReference is what sets the blob name, and it can be named as desired. The actual file can be read into a fileStream from some other physical location and uploaded into the blob, like so:
string fileName = HttpContext.Current.Server.MapPath("~/App_Data/Clients/Clientname/" + "HelloWorld.png");
CloudBlockBlob blockBlob = container.GetBlockBlobReference(#"Clients\ClientName");
using (var fileStream = File.OpenRead(fileName))
{
await blockBlob.UploadFromStreamAsync(fileStream);
}

Related

how to do Bulk download of all files from azure blob as zip folder (We should not use Azure storage explorer)

Azure blob - Bulk download of all files (It could be images/videos) as zip folder programatically/any other azure features. (We should not use Azure storage explorer)
We have tried azure data factory - we could achieve 90%, but we should trigger event to client once zip folder is downloaded. In azure data factory, we could not trigger event.
Please suggest any other solution approach if someone accomplished this activity.
There are 2 approaches, I followed the below approach and it worked for me.
Approach 1
Uploaded the files to the container.
Using the below code downloaded all the files from container and converted into a zip folder.
string storageAccount_connectionString ="ConnectionString";
CloudStorageAccount mycloudStorageAccount = CloudStorageAccount.Parse(storageAccount_connectionString);
CloudBlobClient blobClient = mycloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(azure_ContainerName);
var blobs = container.ListBlobs().OfType<CloudBlockBlob>().ToList();
foreach (CloudBlockBlob blob in blobs)
{
string fileName = blob.Name;
CloudBlockBlob blobFile = container.GetBlockBlobReference(fileName);
blobFile.DownloadToFile(#"C:\Tools\Files\" + fileName, System.IO.FileMode.Create);
}
Console.WriteLine("Download completed!");
string sourceFilePath= #"C:\Tools\Files\", path = #"C:\New folder";
string startPath = sourceFilePath;
string zipPath = path + #"\result.zip";
ZipFile.CreateFromDirectory(startPath, zipPath);
All blobs to a zip
Approach 2 is StreamSaver approach

How to download a file (inside an other folder) from Azure Blob Storage?

I need to download a file from Azure Blob storage.
But the file inside another folder. For example;
Blob Containers
---myBlobContainer
-----TestFolder1
-------TestFolder2
-----------aaa.jpeg
I want to download aaa.jpeg file. I can download it when the situation like below;
Blob Containers
---myBlobContainer
------aaa.jpeg
How can I navigate through the folders, I cant give a path to the file.
Thanks
There's no folders when using Azure Storage Account. You have a container, and all the rest is part of the blob name.
e.g.
myBlobContainer/TestFolder1/TestFolder2/aaa.jpeg
myBlobContainer is the container name
/TestFolder1/TestFolder2/aaa.jpeg
is the blob name
more info: https://learn.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata#blob-names
An easy way to double check this, just loop over all the blobs inside a container, you can confirm the 'sublevels' are part of the blob name
BlobContainerClient blobContainerClient = new BlobContainerClient(connectionString, container);
blobContainerClient.CreateIfNotExists();
Console.WriteLine("Listing blobs...");
// List all blobs in the container
var blobs = blobContainerClient.GetBlobs();
foreach (BlobItem blobItem in blobs)
{
Console.WriteLine("\t" + blobItem.Name);
}

Is there a default image type for Azure Blob Storage?

When I upload an jpg image to Azure Blob Storage, then pull it back out, it always comes back as a png. Is this a safe to assume default? I could not find any documentation. I can easily convert it back to a jpg in my byte conversion as I store the extension when I pull it out of the container, but can I safely assume any image type that is stored in Azure Blob Storage is done as a png?
but can I safely assume any image type that is stored in Azure Blob Storage is done as a png
As far as I know, blob storage doesn't have the default file type. It contains a content-type which could tell the user the file's content type is. If you set the blob name is xxx.jpg. Then it will store the xxx.jpg in the blob storage.
Here is an example.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connection string");
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
// Create the container if it doesn't already exist.
container.CreateIfNotExists();
CloudBlockBlob blockBlob = container.GetBlockBlobReference("brandotest.jpg");
// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(#"D:\1.PNG"))
{
blockBlob.UploadFromStream(fileStream);
}
Result:
By using storage explorer, you could find the image's type in the blob storage. It is brandotest.jpg.
I guess you set the download image file's type when you use codes to download it.
Like this(I set the download file's path is myfile.png):
CloudBlockBlob blockBlob = container.GetBlockBlobReference("brandotest.jpg");
// Save blob contents to a file.
using (var fileStream = System.IO.File.OpenWrite(#"D:\authserver\myfile.png"))
{
blockBlob.DownloadToStream(fileStream);
}
The result will be myfile.png.
All in all, the file's type you have download to local or upload to the blob is according to your set when you using codes to get or upload it.
It is best not to assume that png is the default. I think it depends on your upload mechanism and the name you give for the file during the upload process.

How to download files with white space on name on Azure Blob Storage?

I'm trying to download a file from this URL:
https://renatoleite.blob.core.windows.net/mycontainer/documents/Test Document.pdf
The browser is changing the URL to this:
https://renatoleite.blob.core.windows.net/mycontainer/documents/Test%20Document.pdf
My file in the blob storage has the name: Test Document.pdf
So, when I clicks to download, the Azure say that file not exist:
The specified resource does not exist.
Probably because the browser is trying to get the file with "%20" in the name.
How I can solve this?
As far as I know, if you want to upload the file space name by using azure storage api, it will auto encoded the name(replace the space with %20) when uploading it.
You could see below example:
I uploaded the Test Document.pdf to the blob storage.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference("brando");
// Create the container if it doesn't already exist.
container.CreateIfNotExists();
// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("Test Document.pdf");
// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(#"D:\Test Document.pdf"))
{
blockBlob.UploadFromStream(fileStream);
}
Then I suggest you could use storage explorer(right click the properties to see its url) or azure portal to see its url from the blob's property.
The url like this:
You could find it replace the space with %20.

Upload 2GB file to Azure blob

I have a zipped 2GB file that I need to upload to Windows Azure through my home cable connection, how long should I expect that this file gets uploaded? Has anyone averaged their upload time for their files?
Also when I do this to create a blob for uploading a file
CloudBlob _blob = _container.GetBlobReference("file1");
is this creating CloudBlockBlob or CloudPageBlob by default? I have been using the above code to upload files and it has been quite slow.
CloudBlob _blob = _container.GetBlobReference("file1");
It does not create CloudBlockBlob or CloudPageBlob by default.
If you want to use CloudBlockBlob (Azure SDK v2.0):
// Retrieve reference to a blob named "myblob".
CloudBlockBlob blob = container.GetBlockBlobReference("myblob");
now split your file to a small pieces (4MB max), and upload each piece like this:
blob.PutBlock(blockId, memoryStream, null);
where: blockId is a base64-encoded block ID that identifies the block.
and memoryStream A stream that provides the data for the block.
MSDN

Resources