Azure

1. Code Snippets
BlobStorage
public static CloudBlobContainer GetContainer(string folder)
{
var account = CloudStorageAccount.FromConfigurationSetting("storageconnectionstring");
CloudBlobClient blobClient = account.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(folder);
if (container.CreateIfNotExist())
{
var perms = new BlobContainerPermissions();
perms.PublicAccess = BlobContainerPublicAccessType.Container;
container.SetPermissions(perms);
}
return container;
}

internal static string AddFileToBlobStorage(string containerName, string fileName, Stream fileStream)
{
CloudBlockBlob blobReference = DeleteFileFromBlobStorage(containerName, fileName);
blobReference.UploadFromStream(fileStream);
return blobReference.Uri.ToString();
}

internal static CloudBlockBlob DeleteFileFromBlobStorage(string containerName, string fileName)
{
containerName = containerName.ToLower().Replace('_', '-');//storage only supports lowercase, hyphen
fileName = fileName.ToLower().Replace('_', '-');
var container = GetContainer(containerName);
CloudBlockBlob blobReference = container.GetBlockBlobReference(fileName);
blobReference.DeleteIfExists();
return blobReference;
}

Local Storage
var storage = RoleEnvironment.GetLocalResource("reportfiles");
string path = Path.Combine(storage.RootPath, "a.xlsx");
if (!System.IO.File.Exists(path)){...}

2. Errors and solutions

Azure1.3: ServiceChannel, cannot be used for communication because it is in the Faulted state.
Reason: Azure 1.3 tries to overwrite web.config, if the web.config is in source control (readonly) this error will appear.
Solution: Just check out web.config for edit, or remove its readonly property to solve this problem.

Could not find file 'E:\approot\Content\a.xlsx'.
Only .dlls, views and recourse files are included in package, other files are exluded. Thats why the error. Only way to work with files in azure is with local storage. here is an example:
var reportTemplateStorage = RoleEnvironment.GetLocalResource("reportfiles");string reportTemplatePath = Path.Combine(reportTemplateStorage.RootPath, "a.xlsx");if (!System.IO.File.Exists(reportTemplatePath)){...}

One of the request inputs is out of range
CloudBlobContainer GetContainer()
{
var account = CloudStorageAccount.FromConfigurationSetting("StorageConnectionString");
CloudBlobClient blobClient = account.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("Container");
if (container.CreateIfNotExist())
{
var perms = new BlobContainerPermissions();
perms.PublicAccess = BlobContainerPublicAccessType.Container;
container.SetPermissions(perms);
}
return container;
}Containers name can contains only a) lower case letters b) digits c) hyphen
Conclusion: Always use lower case.

Deploy fails
There is a known chrome + file upload issue with azure portal. So use Internet Explorer.

3. How Tos

4. Tutorials

5. Blogs
6. Websites
7. Books

8. Downloads

No comments:

Post a Comment