How to convert Blob to base64 to image - base64

how to convert Blob to base64 to image using javascript

Related

Create text file in memory and upload to azure blob storage using python

So I want to create a python function that creates an inmemory text file that I can pass a string, or multiple strings to and upload to azure blob storage. Doing it with a normal text file is a novel task, but I can't seem to get it happen using the io module.
Please check out this https://pypi.org/project/azure-storage-blob/ which given in detail upload a file to azure blob storage by doing the following.
You can easily pass the string to upload_blob method using python as below:
blob = BlobClient.from_connection_string(conn_str="<connection_string>", container_name="my_container", blob_name="my_blob")
data = "test"
blob.upload_blob(data)

How can I directly upload a string to azure storage blob using the python sdk?

What is the best way to upload a JSON string (created in memory, not read from a file) to azure blob storage using the python sdk?
I understand from the docs at https://pypi.org/project/azure-storage-blob/ that I can upload a file to azure blob storage by doing the following.
from azure.storage.blob import BlobClient
blob = BlobClient.from_connection_string(conn_str="<connection_string>", container_name="my_container", blob_name="my_blob")
with open("./SampleSource.txt", "rb") as data:
blob.upload_blob(data)
but I am struggling to find any examples where a string is uploaded directly.
Is it possible to upload a string directly? If so, how is it done in python?
Thanks.
You can simply pass the string to upload_blob method. No need to do anything special.
For example, code below should work just fine.
blob = BlobClient.from_connection_string(conn_str="<connection_string>", container_name="my_container", blob_name="my_blob")
data = "This is a test"
blob.upload_blob(data)
If you're trying to upload a json object and not a json formatted string, you'll first need to turn the json obj into a string so that BlobClient can upload. e.g. if getting a json obj from an api...
response = requests.get("<some endpoint>")
blob = BlobClient.from_connection_string(conn_str="<connection_string>", container_name="my_container", blob_name="my_blob")
data = json.dumps(response.json())
blob.upload_blob(data)

SAVE and GET PDF to/from Microsoft Azure Blob Storage container using JsPdf Library in Javascript

I want to save the pdf to Azure Blob storage container and retrieve it. How can I do this?
I tried this to save the PDF to Local Drive but I want to store this directly to BLOB storage without saving it on Local Drive.
let doc = new jsPDF('p','pt','a4');
doc.addHTML(document.body,function() {
doc.save('html.pdf');
});
**Can any suggest me a solution?
How to send the PDF content to Azure Blob storage?**

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.

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