I just came across a very strange problem:
<div class="logo-holder">
<img class="sponsorhip-logo-preview" id="LogoPreview"
src="http://cdn.insights.bio/uploads/83cfc94c4a8c4f14b3cd050cd7e1c7aa.svg">
</div>
Using an SVG file as the source of the image results with broken image in chrome
This image is stored on azure CDN. I do not see any error in Chrome console.
Has anyone come across this problem?
In the end, I've managed to figure out thank you to Markus.
During image upload to Azure blob, all files were uploaded with default content-type. This line of code made all the huge difference
if (DoesBlobFileExist(blobName))
blobName = RenameFile(blobName);
CloudBlockBlob blob = container.GetBlockBlobReference(blobName);
if (blobName.EndsWith(".svg"))
blob.Properties.ContentType = "image/svg+xml";
With the azure blob storage java package you can get the file content type and add it to the BlobServiceClient header:
ClientOptions options = new ClientOptions();
MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
String mimeType = fileTypeMap.getContentType(file.getName());
HttpHeader httpHeaders = new HttpHeader("Content-Type", mimeType);
options.setHeaders(Collections.singleton(httpHeaders));
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.connectionString(connectStr)
.clientOptions(options)
.buildClient();
Related
I'm trying to implement a piece of code in Java using Azure Java SDK that allows me to upload a file (jpg, in mycase) to a Azure container. I have written this, but it fails:
String ENDPOINT= "https://myaccount.blob.core.windows.net";
BlobContainerClient blobContainerClient = new BlobContainerClientBuilder().
endpoint(ENDPOINT).
credential(CREDENTIAL).
containerName(CONTAINER_NAME).
buildClient();
// Get a reference to a local image
BlobClient blobClient = blobContainerClient.getBlobClient(myJpgLocalPath + myJpgFile);
// Set headers
blobClient.setHttpHeaders(new BlobHttpHeaders().setContentType("image/jpeg"));
// Upload the blob
blobClient.uploadFromFile(myJpgLocalPath + myJpgFile, false);
Another approach I've done is this:
BlobServiceClient storageClient = new BlobServiceClientBuilder().
endpoint(ENDPOINT).
credential(CREDENTIAL).
buildClient();
BlobContainerClient blobContainerClient = storageClient.getBlobContainerClient(CONTAINER_NAME);
// Get a reference to a local image
BlobClient blobClient = blobContainerClient.getBlobClient(myJpgLocalPath + myJpgFile);
// Set headers
blobClient.setHttpHeaders(new BlobHttpHeaders().setContentType("image/jpeg"));
// Upload the blob
blobClient.uploadFromFile(myJpgLocalPath + myJpgFile, false);
None of these two works at all and fails even building the client.
The errors shown are these two:
java.lang.ClassNotFoundException: com.fasterxml.jackson.dataformat.xml.PackageVersion
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1412)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1220)
at com.azure.core.implementation.jackson.JacksonVersion.<init>(JacksonVersion.java:46)
at com.azure.core.implementation.jackson.JacksonVersion.getInstance(JacksonVersion.java:72)
at com.azure.core.implementation.jackson.ObjectMapperShim.<clinit>(ObjectMapperShim.java:40)
at com.azure.core.util.serializer.JacksonAdapter.<init>(JacksonAdapter.java:81)
at com.azure.core.util.serializer.JacksonAdapter.<init>(JacksonAdapter.java:59)
at com.azure.core.util.serializer.JacksonAdapter$SerializerAdapterHolder.<clinit>(JacksonAdapter.java:114)
at com.azure.core.util.serializer.JacksonAdapter.createDefaultSerializerAdapter(JacksonAdapter.java:123)
at com.azure.storage.blob.implementation.util.ModelHelper.<clinit>(ModelHelper.java:62)
at com.azure.storage.blob.BlobUrlParts.parse(BlobUrlParts.java:371)
at com.azure.storage.blob.BlobContainerClientBuilder.endpoint(BlobContainerClientBuilder.java:181)
...
java.lang.NoClassDefFoundError: Could not initialize class com.azure.storage.blob.implementation.util.ModelHelper
at com.azure.storage.blob.BlobUrlParts.parse(BlobUrlParts.java:371)
at com.azure.storage.blob.BlobContainerClientBuilder.endpoint(BlobContainerClientBuilder.java:181)
...
What I am doing bad?
Thanks!
I am trying to upload a word document to blob storage using C#. The code snippet is as below:
var blobServiceClient = new BlobServiceClient(connectionString);
var containerClient = blobServiceClient.GetBlobContainerClient(container);
containerClient.CreateIfNotExists(PublicAccessType.None);
var blobClient = containerClient.GetBlobClient(documentName);
using (var memoryStream = new MemoryStream({Binary of the File}))
{
var headers = new BlobHttpHeaders() { ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" };
await blobClient.UploadAsync(memoryStream, httpHeaders: headers).ConfigureAwait(false);
}
The document gets uploaded successfully to the Blob Storage. I can see that the content type is also being set properly while looking through Azure Storage Explorer. But, when i try to access this document (using document URL) through browser (chrome), it downloads the file as unknown file.
I tried the same by uploading a word document through Azure Storage Explorer. This file gets downloaded as word document while downloading it through browser.
Any idea what is going wrong?
As you see the figure below, the Content-Type header is stored as a property in the Blob Properties, so it will be set into the headers of the download response and then it will be recognized as a word file while download by browser.
So when you upload a word file, it's necessary to set the ContentType property for a blob via BlobBaseClient.SetHttpHeaders(BlobHttpHeaders, BlobRequestConditions, CancellationToken) Method or BlobBaseClient.SetHttpHeadersAsync(BlobHttpHeaders, BlobRequestConditions, CancellationToken) Method.
And then to write the Content-Typeheader of the headers of a download response with the value of contentType below by yourself on your server app.
Response<BlobProperties> response = await blobClient.GetPropertiesAsync();
var contentType = response.Value.ContentType
Or the download response from a blob url with sas token which headers default include the ContentType property as the Content-Type header.
I want to ask if the Azure Easy Tables does have the row size limit ?
If yes what is the limit ?
Thanks for your answers !!!
We know the back end of easy table in Azure is Azure Sql . And a table can contain a maximum of 8,060 bytes per row in Azure Sql usually. There is also a LOB columns type that the limit is 2G. For better performance, I suggest you better don't reach 8060 bytes. If you have large data to store, you could put a link instead.
Can I save image to Azure blob storage and name, description to Azure Easy Tables
Yes, you could save image to Azure blob directly. You could try the following code:
Code in app.config(storage connection string):
<appSettings>
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=×××;AccountKey=×××" />
</appSettings>
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("container001");
CloudBlockBlob blockBlob = container.GetBlockBlobReference("apple.jpg");
using (var fileStream = System.IO.File.OpenRead(#"D:\apple.jpg"))
{
blockBlob.UploadFromStream(fileStream);
}
In Easy Table, if I convert image to base64 string to store, I also get en error about the request entity is too large. So you could copy the blob image link to store in Easy Table instead. You could copy the link by click '...'>Blob properties>Url.
var client = new MobileServiceClient("https://[your mobile service name].azurewebsites.net");
IMobileServiceTable<SiteTable> todoTable = client.GetTable<SiteTable>();
SiteTable siteTable = new SiteTable(); //SiteTable is model class name
siteTable.MyImage = " blob image link";
await todoTable.InsertAsync(siteTable);
This is how I try to upload an image to Azure blog storage, then upload an empty file located there.
I try to upload this image here:
CloudStorageAccount storageAccount = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(Accountname, KeyValue), true);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference(ContainerValue);
CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);
using (var f = System.IO.File.Open(model.FileToUpload.FileName, FileMode.Create))
{
await blockBlob.UploadFromStreamAsync(f);
}
But if i try to open this image say this.
From danish to english: We can not open this file
As Marco said, FileMode.Create specifies that the operating system should create a new file. If the file already exists, it will be overwritten.
For more details, you could refer to this article.
According to the pictures you provided, your blob size is 0 B. So, you would always couldn't find the file and open it.
FileMode specifies how the operating system should open a file.
So I suggest that you could delete it and use OpenRead to open an existing file for reading. You could refer to the code as below:
using (var f = System.IO.File.OpenRead(model.FileToUpload.FileName))
{
await blockBlob.UploadFromStreamAsync(f);
}
This is how I try to upload an image to Azure blog storage, then upload an empty file located there.
I try to upload this image here:
CloudStorageAccount storageAccount = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(Accountname, KeyValue), true);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference(ContainerValue);
CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);
using (var f = System.IO.File.Open(model.FileToUpload.FileName, FileMode.Create))
{
await blockBlob.UploadFromStreamAsync(f);
}
But if i try to open this image say this.
From danish to english: We can not open this file
As Marco said, FileMode.Create specifies that the operating system should create a new file. If the file already exists, it will be overwritten.
For more details, you could refer to this article.
According to the pictures you provided, your blob size is 0 B. So, you would always couldn't find the file and open it.
FileMode specifies how the operating system should open a file.
So I suggest that you could delete it and use OpenRead to open an existing file for reading. You could refer to the code as below:
using (var f = System.IO.File.OpenRead(model.FileToUpload.FileName))
{
await blockBlob.UploadFromStreamAsync(f);
}