Upload image to Azure blob storage from Windows Phone. Not creating - c#

I am using Windows Azure to store images in my windows phone application.
The camera takes a photo and the chosen photo stream is then uploaded. However it is throwing NO error but is not uploading?
var blobContainer = CloudStorageContext.Current.Resolver.CreateCloudBlobClient();
var container = blobContainer.GetContainerReference("pics");
var blob = container.GetBlobReference("picture.jpg");
blob.UploadFromStream(e.ChosenPhoto, response => { MessageBox.Show(blob.Uri.ToString()) });
I don't have a clue what is happening. The Resolver contains the correct user, key and urls. The container "pics" does exist, but no image is being uploaded. The message box pops up with a url which does not exist.
UPDATE - There seems to be a simular (well almost identical) question posted here - Uploading a photo stream from camera into azure blob in WP7. However the upper case container name is not an issue here, so that solution did not fix this

I have an application (Windows Phone 8) that uploads an image taken to an Azure webrole, which in turn stores the image in an Azure Storage Blob. The code below is how the server stores the images. Again, this code does not run on the phone, but you can use it as a reference.
string randomGUID = locationID
+ "-"
+ Guid.NewGuid().ToString();
//Retrieve storage account from application settings
CloudStorageAccount storageAccount = GetStorageAccount();
//Create blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//Retrieve reference to images container
CloudBlobContainer container = blobClient.GetContainerReference(
RoleEnvironment.GetConfigurationSettingValue("BlobContainer"));
//Retrieve references to the blob inside the container
CloudBlockBlob blockBlob = container.GetBlockBlobReference(randomGUID);
blockBlob.UploadFromStream(imageToUpload);
The variable imageToUpload is of the type Stream.
As you can see, this is pretty straightforward code. Perhaps your problem has to do with the lambda expression you have in UploadFromStream?

Related

Images stored in Azure Blob don't cache in Browser

I have set the cache-property to "max-age=3600, must-revalidate" when uploading. Looking at the browser developer tool, the network tab, I can see that the header is correct and the cache property is sett.
But still the Photos get fetched from the Azure blob storage every time with 200OK response.
The way I upload is as follows:
-Photo uploaded by user
-GUID added to name of photo and saved to Azure SQL database with user info.
-Blob Reference created using Azure library in C#
-Cache properties set and saved after uploading
var uniqueFileName = Guid.NewGuid().ToString() + "_" + fileName;
var newBlob = container.GetBlockBlobReference(uniqueFileName);
using var fileStream = file.OpenReadStream();
newBlob.UploadFromStreamAsync(fileStream).Wait();
newBlob.Properties.CacheControl = "max-age=3600, must-revalidate";
newBlob.SetPropertiesAsync().Wait();
Blob is fetch by using an URI + SAS Token
I get the name from the SQL database, Look it up in azure Blob then getting the URI and Adding The SAS to give the Client access to the Blob.
var blob = container.GetBlockBlobReference(fileName);
var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
{
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1)
});
var blobUrl = blob.Uri.AbsoluteUri + sasToken;
I found that every time a new SAS is created the image is treated as new version. And I do create a new SAS for every request to create a link that the client use to download image. How can I bypass this behavior. Should I make the container accessible by public for read with no SAS used?
The cache control should work if using blob with SAS token.
If you're using a browser(like Chrome) to visit the blob with SAS token, please make sure you DON'T select the checkbox of "Disable cache" in Develop tool. I can see it uses cache at my side, the screenshot as below:

Copy Azure CloudPageBlob to CloudBlockBlob

Have a load of Azure page blobs in an Azure storage account which need copying to block blobs so they can be set from "cold" to "Archive" storage tier. The page blobs are created by SQL server (pre 2016) BACKUP TO URL.
I spoke to Microsoft about this and they suggested AzCopy with Azure File blob as a stepping stone, e.g. Page blob -> block blob (file storage) -> block blob (Blob storage). This all works fine in AzCopy or GUI based Azure tools but I need to automate it and build in some resilience.
Problem I have is I cannot get a CloudPageBlob to copy to CloudBlockBlob
not matter how I cast it. This is a cut-down example to show (lack of) progress and how I'm going about this. I will wrap this in a Task<> in production but I cannot get the .StartCopy to work. Any ideas?
CredentialEntity ce = Utils.GetBlobCredentials();
StorageCredentials sc = new StorageCredentials(ce.Name, ce.Key);
CloudStorageAccount storageAccount =
new CloudStorageAccount(new StorageCredentials(ce.Name, ce.Key), true);
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer sourceContainer = cloudBlobClient.GetContainerReference(#"archive");
CloudBlobContainer targetContainer = cloudBlobClient.GetContainerReference(#"xfer");
CloudPageBlob source = sourceContainer.GetPageBlobReference("test.bak");
CloudBlockBlob target = targetContainer.GetBlockBlobReference("new.bak");
//target.StartCopy(source);
//target.StartCopyAsync()

c# Azure Cannot set the blob tier

CloudBlockBlob doesn't have any method to set the blob tier to hot/cool/archive. I have also checked the other blob types and they do not have a method that allows this either.
I.E this method: https://learn.microsoft.com/en-us/rest/api/storageservices/set-blob-tier
Is their any way to change the blob tier in code from hot to cold in C# with azure storage?
I think the method is exactly what you need: CloudBlockBlob.SetStandardBlobTier. Maybe you were not checking the latest version of Azure Storage Client Library?
Is their any way to change the blob tier in code from hot to cold in C# with azure storage?
As ZhaoXing Lu mentioned that we could use CloudBlockBlob.SetStandardBlobTier.
Note: The operation is allowed on a page blob in a premium storage account and on a block blob in a blob storage account
The following code works correctly on my side. I use the library WindowsAzure.Storage 9.1.1
var cloudBlobClient = storageAccount.CreateCloudBlobClient();
var container = cloudBlobClient.GetContainerReference("container");
var blob = container.GetBlockBlobReference("blob name");
blob.SetStandardBlobTier(StandardBlobTier.Cool);
blob.FetchAttributes();
var tier = blob.Properties.StandardBlobTier;
Using Azure Blob storage client library v12 for .NET, replace myaccount with the name of your storage account, mycontainer with your container name and myblob with the blob name for which the tier is to be changed:
var sharedKeyCredential = new StorageSharedKeyCredential("myaccount", storageAccountKey);
var baseBlobContainerUrl = string.Format("{0}.blob.core.windows.net", "myaccount");
var blobServiceClient = new BlobServiceClient(new Uri($"https://{baseBlobContainerUrl}"), sharedKeyCredential);
var containerClient = blobServiceClient.GetBlobContainerClient("mycontainer");
BlobClient blobClient = containerClient.GetBlobClient("myblob");
// Set access tier to cool.
await blobClient.SetAccessTierAsync(AccessTier.Cool);
If you are working with Azure Gov, use this url insteadl "{0}.blob.core.usgovcloudapi.net"
Keep in mind, your storage account should support Cool Storage.

Uploading a png file to Azure Blob Storage is losing transparency

We are having some difficulty to figure out how upload png files to Azure Blob Storage without losing image transparency.
We are using the Azure's Storage SDK with the code below:
var storageAccount = CloudStorageAccount.Parse(*blob_connection_string*);
//create client to work with blobs
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//already created container via azure management portal, set container reference
CloudBlobContainer container = blobClient.GetContainerReference("brand");
foreach (string file in httpRequest.Files)
{
var postedFile = httpRequest.Files[file];
CloudBlockBlob blockBlob = container.GetBlockBlobReference(postedFile.FileName);
blockBlob.UploadFromStream(postedFile.InputStream);
}
We've tried force the ContentType property before upload like this:
blockBlob.Properties.ContentType = "image/png";
But doesn't work neither with or without this property.
Well, for those who are experiencing this issue, we've found a solution:
The problem was with using Microsoft .Net Image object. According MSDN this object work only with PNG 32 bits:
https://msdn.microsoft.com/pt-br/library/1kcb3wy4%28v=vs.110%29.aspx?f=255
We've changed the upload routine to get the posted file binary direct from the input and work like a charm!
Thanks again for the ideas who drive us to this solution!
Hugs

How to upload List of images on azure and retrieve addresses of uploaded images

I am doing Mvc asp.net project. i want to upload four images on azure blob storage, i am uploading one image at a time using following method
public static string UploadToBlob(string fileName, byte[] data)
{
MemoryStream file = new MemoryStream(data);
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("ConnectionSetting"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
// Create the container if it doesn't already exist.
container.CreateIfNotExists();
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
blob.Properties.ContentType = "image/jpg";
blob.UploadFromStream(file);
string url = blob.Uri.ToString();
return url;
}
it's takes four call to azure server, so i want do it in one call i.e upload List of four images on azure and retrieve addresses of uploaded images.
You need to make separate service calls to upload separate blobs. However, you should be sharing most of the common code in that method. Namely, everything above actually getting the blob reference. This will save you several service calls by only calling CreateIfNotExists on the container once.

Categories