Copy css file from one subdirectory to another one in CONTAINER BLOB - azure

Scenario:
I copy .css file from one subdirectory to another in Azure Storage Container. It is done from C# code level in my application. This is css style file for my website. Unfortunately I received error in my browser console during loading page:
Error
Resource interpreted as Stylesheet but transferred with MIME type application/octet-stream:
"SOME_PATH/template/css/styles.css?d=00000000-0000-0000-0000-000000000000".
Knowledge:
I know that it is why my file is sended as octet-stream instead of text/css. What can I do to say Azure to treat this file as text/css?
Edit: My code
string newFileName = fileToCopy.Name;
StorageFile newFile = cmsDirectory.GetStorageFileReference(newFileName);
using (var stream = new MemoryStream())
{
fileToCopy.DownloadToStream(stream);
stream.Seek(0, SeekOrigin.Begin);
newFile.UploadFromStream(stream);
}
where DownloadToStream and UploadToStream are methodes in my class:
CloudBlob.DownloadToStream(target);
and
CloudBlob.DownloadToStream(target);
CloudBlob is CloudBlockBlob type

You can set content type of blob via property ContentType
look at:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.storage.blob.blobproperties.contenttype

Download AzCopy - http://aka.ms/azcopy
If you specify /SetContentType without a value, AzCopy sets each blob or file's content type according to its file extension.
Run this command on Windows
AzCopy /Source:C:\myfolder\ /Dest:https://myaccount.blob.core.windows.net/myContainer/ /DestKey:key /Pattern:ab /SetContentType
More details: https://learn.microsoft.com/en-us/azure/storage/common/storage-use-azcopy?toc=%2fazure%2fstorage%2fblobs%2ftoc.json
Use the Microsoft Azure Storage Explorer to modify the content-type string by hand for already existing file. Right click the blob file in explorer and the Left click on properties, scroll down to change the file format.

Related

In c# using StreamWriter to write to AZURE can i use the URI as the path

I am using nuGet CsvHelper to write a datatable to a new CSV file. It works fine with path of a physical location Ie C:/temp/mydrop box. But I need it to streamwrite to a Azure Storage container.
when the program gets to here
StreamWriter sw = new StreamWriter(strFilePath, false);
I get the following error:
The filename, directory name, or volume label syntax is incorrect. : 'C:\Users\myuser\source\repos\sample\bin\Debug\net6.0\https://mystorage.blob.core.windows.net/myContainer-processedfiles/myfle.csv''
I am passing in https://mystorage.blob.core.windows.net/myContainer-processedfiles/myfle.csv

How to save a file to a subfolder in an Azure blob container?

I'm trying to save an image to our Azure blog storage. Thanks to the helpful response provided via the link below the code snippet, I was able to successfully save the file to our top-level container. Unfortunately, I would like the file to save to a subdirectory in that container. And, for the life of me, I can not get it to work.
Currently, the image is saving to our "images" container. Within that container is a folder, "members". I would like the files to save to that subdirectory. So "images/members". I tried to pass "images/members" to GetBlockBlobReference but then the file just didn't save at all (or, at least I can't find it).
This seems like it should be pretty simple. Thanks in advance.
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("images");
CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);
blockBlob.UploadFromStream(stream);
Top-level container. The image with the Guid is one that I uploaded
The "members" directory. Sorted by most recent; nothing recent appearing
Helpful solution that got me to saving successfully to the top-level container
"images" is the name of your container.
what you need to do is change this line from
CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);
to
CloudBlockBlob blockBlob = container.GetBlockBlobReference("members/" + filename);
Then you can use the Azure Storage Explorer to view your files and folders:
https://azure.microsoft.com/en-us/features/storage-explorer/
Using a slash (/) in the filename will create a 'folder'. I used 'folder' because it's a virtual folder, a trick used to give us humans the idea of folders. There's actually only one level of grouping which is the Container.
Each slash (/) in a filename stands for one 'folder', so creating a blob with filename firstFolder/secondFolder/filename.txt will create a file with that exact name. Which looks like a file with the path firstFolder -> secondFolder. You can ask a container to ListBlobs with useFlatBlob set to true, returning you all blobs in the specific container. So all Blobs in all folders.
You can also ask for all blobs in a virtual folder by getting a DirectoryReference using CloudBlobContainer.GetDirectoryReference and listing the blobs under there.
More info here: Work with Blob resources
You can follow this snip of code to do the same.
output = detected_anomaly.to_csv (encoding = "utf-8", index=False)
blob_path = 'blobfolder1/blobfolder2/'
blob_path_anomalies = blob_path + '<created_blob_folder_name>/demo.csv'
blob_service.create_blob_from_text(container_name, blob_path_anomalies, output)

Azure CDN Blob forces HTML files to download instead of rendering

I tried uploading an HTML file to my azure blob storage, and retrieved the link.
Unfortunately, when entering the URL into a web browser, it does not load the page, it tries to download it.
How can I make HTML files on Azure CDN load as web pages, not downloads?
Thanks
FIXED! Turns out in Azure, I need to edit the properties of the html file, and set the content type to text/html. :)
We need to set it's property Content type through Blob Options class.
PHP :
namespace - use MicrosoftAzure\Storage\Blob\Models\CreateBlobOptions;
//use code where you are creating blob
$opts = new CreateBlobOptions();
//$opts->setCacheControl('test');
$opts->setContentEncoding('UTF-8');
$opts->setContentLanguage('en-us');
//$opts->setContentLength(512);
$opts->setContentMD5(null);
$opts->setContentType($mimeType);
$blobRestProxy->createBlockBlob($containerName, $indexFile, $content,$opts);
It will work in git package : "microsoft/windowsazure": "^0.5"
In C#
entryData.DestinationBlob.Properties.ContentType = "image/jpeg";
entryData.DestinationBlob.SetProperties();

Is it possible to set the content disposition of an existing Azure blob?

Based on stimms answer here:
Azure Storage API ContentDisposition
and the information found here:
Friendly filename when public download Azure blob
I have been able to set the content disposition when uploading new files. But I also would like to be to able to set the content disposition of existing files.
blob.Properties.ContentDisposition = string.Format("attachment;filename=\"{0}\"", friendlyName);
works fine when set before uploading a file, but has no effect if I try it on an existing blob.
Is is just plain impossible to change the content disposition of an existing blob or am I doing something wrong?
Yes. You just have to call SetProperties() method on the blob after you set ContentDisposition. So your code should be:
blob.FetchAttributes();//Fetch properties first so that you don't overwrite existing properties when you call SetProperties
blob.Properties.ContentDisposition = string.Format("attachment;filename=\"{0}\"", friendlyName);
blob.SetProperties();

How to create a sub container in azure storage location

How can I create a sub container in the azure storage location?
Windows Azure doesn't provide the concept of heirarchical containers, but it does provide a mechanism to traverse heirarchy by convention and API. All containers are stored at the same level. You can gain simliar functionality by using naming conventions for your blob names.
For instance, you may create a container named "content" and create blobs with the following names in that container:
content/blue/images/logo.jpg
content/blue/images/icon-start.jpg
content/blue/images/icon-stop.jpg
content/red/images/logo.jpg
content/red/images/icon-start.jpg
content/red/images/icon-stop.jpg
Note that these blobs are a flat list against your "content" container. That said, using the "/" as a conventional delimiter, provides you with the functionality to traverse these in a heirarchical fashion.
protected IEnumerable<IListBlobItem>
GetDirectoryList(string directoryName, string subDirectoryName)
{
CloudStorageAccount account =
CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
CloudBlobClient client =
account.CreateCloudBlobClient();
CloudBlobDirectory directory =
client.GetBlobDirectoryReference(directoryName);
CloudBlobDirectory subDirectory =
directory.GetSubdirectory(subDirectoryName);
return subDirectory.ListBlobs();
}
You can then call this as follows:
GetDirectoryList("content/blue", "images")
Note the use of GetBlobDirectoryReference and GetSubDirectory methods and the CloudBlobDirectory type instead of CloudBlobContainer. These provide the traversal functionality you are likely looking for.
This should help you get started. Let me know if this doesn't answer your question:
[ Thanks to Neil Mackenzie for inspiration ]
Are you referring to blob storage? If so, the hierarchy is simply StorageAccount/Container/BlobName. There are no nested containers.
Having said that, you can use slashes in your blob name to simulate nested containers in the URI. See this article on MSDN for naming details.
I aggree with tobint answer and I want to add something this situation because I also
I need the same way upload my games html to Azure Storage with create this directories :
Games\Beautyshop\index.html
Games\Beautyshop\assets\apple.png
Games\Beautyshop\assets\aromas.png
Games\Beautyshop\customfont.css
Games\Beautyshop\jquery.js
So After your recommends I tried to upload my content with tool which is Azure Storage Explorer and you can download tool and source code with this url : Azure Storage Explorer
First of all I tried to upload via tool but It doesn't allow to hierarchical directory upload because you don't need : How to create sub directory in a blob container
Finally, I debug Azure Storage Explorer source code and I edited Background_UploadBlobs method and UploadFileList field in StorageAccountViewModel.cs file. You can edit it what you wants.I may have made spelling errors :/ I am so sorry but That's only my recommend.
If you are tying to upload files from Azure portal:
To create a sub folder in container, while uploading a file you can go to Advanced options and select upload to a folder, which will create a new folder in the container and upload the file into that.
Kotlin Code
val blobClient = blobContainerClient.getBlobClient("$subDirNameTimeStamp/$fileName$extension");
this will create directory having TimeStamp as name and inside that there will be your Blob File. Notice the use of slash (/) in above code which will nest your blob file by creating folder named as previous string of slash.
It will look like this on portal
Sample code
string myfolder = "<folderName>";
string myfilename = "<fileName>";
string fileName = String.Format("{0}/{1}.csv", myfolder, myfilename);
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);

Resources