protected void SignButton_Click(object sender, EventArgs e) { if (this.FileUpload1.HasFile) { this.InitializeStorage(); // upload the image to blob storage string uniqueBlobName = string.Format("guestbookpics/image_{0}{1}", Guid.NewGuid().ToString(), Path.GetExtension(this.FileUpload1.FileName)); CloudBlockBlob blob = blobStorage.GetBlockBlobReference(uniqueBlobName); blob.Properties.ContentType = this.FileUpload1.PostedFile.ContentType; blob.UploadFromStream(this.FileUpload1.FileContent); System.Diagnostics.Trace.TraceInformation("Uploaded image '{0}' to blob storage as '{1}'", this.FileUpload1.FileName, uniqueBlobName); // create a new entry in table storage GuestBookEntry entry = new GuestBookEntry() { GuestName = this.NameTextBox.Text, Message = this.MessageTextBox.Text, PhotoUrl = blob.Uri.ToString(), ThumbnailUrl = blob.Uri.ToString() }; GuestBookDataSource ds = new GuestBookDataSource(); ds.AddGuestBookEntry(entry); System.Diagnostics.Trace.TraceInformation("Added entry {0}-{1} in table storage for guest '{2}'", entry.PartitionKey, entry.RowKey, entry.GuestName); // queue a message to process the image var queue = queueStorage.GetQueueReference("guestthumbs"); var message = new CloudQueueMessage(string.Format("{0},{1},{2}", blob.Uri.ToString(), entry.PartitionKey, entry.RowKey)); queue.AddMessage(message); System.Diagnostics.Trace.TraceInformation("Queued message to process blob '{0}'", uniqueBlobName); } this.NameTextBox.Text = string.Empty; this.MessageTextBox.Text = string.Empty; this.DataList1.DataBind(); }
protected void ButtonSave_Click(object sender, EventArgs e) { if (FileUploadImage.HasFiles & Page.IsValid) { string uniqueBobName = string.Format("{0}/funnyimage_{1}{2}", Utils.CloudBlobKey, Guid.NewGuid().ToString(), Path.GetExtension(FileUploadImage.FileName)); CloudBlockBlob blob = _blobClient.GetBlockBlobReference(uniqueBobName); blob.Properties.ContentType = FileUploadImage.PostedFile.ContentType; blob.UploadFromStream(FileUploadImage.FileContent); FunnyAppRepository <Post> postRepository = new FunnyAppRepository <Post>(); FunnyAppRepository <Tag> tagRepository = new FunnyAppRepository <Tag>(); MembershipUser user = Membership.GetUser(Page.User.Identity.Name); if (user != null) { Post post = new Post { PostContent = TextBoxDescription.Text, PostTitle = TextBoxTitle.Text, State = false, UserId = user.ProviderUserKey.ToString() }; string[] tags = TextBoxTag.Text.Split(';'); foreach (string tag in tags) { if (!string.IsNullOrEmpty(tag)) { tagRepository.Create(new Tag() { PostRowKey = post.RowKey, PostPartitionKey = post.PartitionKey, TagName = tag, }); tagRepository.SubmitChange(); } } postRepository.Create(post); postRepository.SubmitChange(); CloudQueue queue = _queueClient.GetQueueReference(Utils.CloudQueueKey); CloudQueueMessage message = new CloudQueueMessage(string.Format("{0},{1},{2}", blob.Uri, post.PartitionKey, post.RowKey)); queue.AddMessage(message); LabelResult.Text = "Uploaded"; } else { LabelResult.Text = "Failed"; } } }
public static void DeleteBlob(string account, string key, string blobUrl) { if (string.IsNullOrEmpty(blobUrl)) { return; } CloudBlobClient blobClient = Client.GetBlobClient(account, key); CloudBlockBlob blockBlob = blobClient.GetBlockBlobReference(blobUrl); blockBlob.Delete(); }
public void PutLargeBlob(string filename, string blobName) { cloudBlobClient.WriteBlockSizeInBytes = Settings.WriteBlockSizeInBytes(); Trace.TraceInformation("UploadingBlob.."); CloudBlockBlob blob = cloudBlobClient.GetBlockBlobReference(blobName); blob.Properties.ContentType = MimeTypeHelper.GetMimeType(filename); blob.ParallelUpload(filename, null); Trace.TraceInformation("Done"); }
public static string GetBlob(string account, string key, string blobUrl) { if (string.IsNullOrEmpty(blobUrl)) { return(null); } CloudBlobClient blobClient = Client.GetBlobClient(account, key); CloudBlockBlob blockBlob = blobClient.GetBlockBlobReference(blobUrl); string tmpPath = Path.GetTempFileName(); blockBlob.DownloadToFile(tmpPath); return(tmpPath); }
/// <summary> /// Save file in Azure Storage /// </summary> /// <param name="filename"></param> /// <param name="content"></param> /// <returns></returns> public static string SaveFileToBlob(string filename, MemoryStream content) { // Inizializzo lo storage... InitAzureStorage(); string fileblobname = string.Format("{0}/{1}", FilesFolder, filename); CloudBlockBlob blob = blobClient.GetBlockBlobReference(fileblobname); blob.Properties.ContentType = System.IO.Path.GetExtension(filename); // Attenzione: devo riavvolgere lo stream.. // perchè quando è stato scritto lo stream è avanzato per tutta la lunghezza dei dati. content.Seek(0, SeekOrigin.Begin); blob.UploadFromStream(content); return(blob.Uri.ToString()); }
public IEnumerable <CloudBlob> Translate(IExpression expression, CloudBlobClient blobClient, MediaFolder mediaFolder) { this.Visite(expression); if (!string.IsNullOrEmpty(fileName)) { var blob = blobClient.GetBlockBlobReference(mediaFolder.GetMediaFolderItemPath(fileName)); if (!blob.Exists()) { return(new CloudBlob[] { }); } blob.FetchAttributes(); return(new[] { blob }); } else { var maxResult = 1000; if (Take.HasValue) { maxResult = Take.Value; } var take = maxResult; var skip = 0; if (Skip.HasValue) { skip = Skip.Value; maxResult += skip; } var blobPrefix = mediaFolder.GetMediaFolderItemPath(prefix); if (string.IsNullOrEmpty(prefix)) { blobPrefix += "/"; } return(blobClient.ListBlobsWithPrefixSegmented(blobPrefix, maxResult, null, new BlobRequestOptions() { BlobListingDetails = Microsoft.WindowsAzure.StorageClient.BlobListingDetails.Metadata, UseFlatBlobListing = false }) .Results.Skip(skip).Select(it => it as CloudBlob).Take(take)); } }
private static string GetBlobPath(HttpRequest request) { string hostName = request.Url.DnsSafeHost; if (hostName == "localhost" || hostName == "www.globalimpact.aalto.fi" || hostName == "globalimpact.aalto.fi") { // hostName = "demopublicoip.aaltoglobalimpact.org"; hostName = "www.aaltoglobalimpact.org"; } else if (hostName == "demowww.weconomy.protonit.net" || hostName == "weconomy.aaltoglobalimpact.org") { hostName = "www.weconomy.fi"; } else if (hostName == "demowww.ehs.protonit.net") { hostName = "www.earthhouse.fi"; } if (hostName == "localhost" || hostName == "oip.msunit.citrus.fi") { return(request.Path.Replace("/public/", "pub/")); } string containerName = hostName.Replace('.', '-'); string currServingFolder = ""; try { // "/2013-03-20_08-27-28"; CloudBlobClient publicClient = new CloudBlobClient("http://theball.blob.core.windows.net/"); string currServingPath = containerName + "/" + RenderWebSupport.CurrentToServeFileName; var currBlob = publicClient.GetBlockBlobReference(currServingPath); currServingFolder = "/" + currBlob.DownloadText(); } catch { } return(containerName + currServingFolder + request.Path); }
private void MoveDirectory(CloudBlobClient blobClient, string newPrefix, string oldPrefix) { var blobs = blobClient.ListBlobsWithPrefix(oldPrefix, new BlobRequestOptions() { BlobListingDetails = Microsoft.WindowsAzure.StorageClient.BlobListingDetails.Metadata, UseFlatBlobListing = false }); foreach (var blob in blobs) { if (blob is CloudBlobDirectory) { var dir = blob as CloudBlobDirectory; var names = dir.Uri.ToString().Split('/'); for (var i = names.Length - 1; i >= 0; i--) { if (!string.IsNullOrEmpty(names[i])) { MoveDirectory(blobClient, newPrefix + StorageNamesEncoder.EncodeBlobName(names[i]) + "/", oldPrefix + StorageNamesEncoder.EncodeBlobName(names[i]) + "/"); break; } } } else if (blob is CloudBlob) { var cloudBlob = blob as CloudBlob; if (cloudBlob.Exists()) { cloudBlob.FetchAttributes(); var newContentBlob = blobClient.GetBlockBlobReference(newPrefix + cloudBlob.Metadata["FileName"]); try { newContentBlob.CopyFromBlob(cloudBlob); } catch (Exception e) { using (Stream stream = new MemoryStream()) { cloudBlob.DownloadToStream(stream); stream.Position = 0; newContentBlob.UploadFromStream(stream); stream.Dispose(); } } newContentBlob.Metadata["FileName"] = cloudBlob.Metadata["FileName"]; if (!string.IsNullOrEmpty(cloudBlob.Metadata["UserId"])) { newContentBlob.Metadata["UserId"] = cloudBlob.Metadata["UserId"]; } if (!string.IsNullOrEmpty(cloudBlob.Metadata["Published"])) { newContentBlob.Metadata["Published"] = cloudBlob.Metadata["Published"]; } if (!string.IsNullOrEmpty(cloudBlob.Metadata["Size"])) { newContentBlob.Metadata["Size"] = cloudBlob.Metadata["Size"]; } if (cloudBlob.Metadata.AllKeys.Contains("AlternateText")) { newContentBlob.Metadata["AlternateText"] = cloudBlob.Metadata["AlternateText"]; } if (cloudBlob.Metadata.AllKeys.Contains("Description")) { newContentBlob.Metadata["Description"] = cloudBlob.Metadata["Description"]; } if (cloudBlob.Metadata.AllKeys.Contains("Title")) { newContentBlob.Metadata["Title"] = cloudBlob.Metadata["Title"]; } newContentBlob.SetMetadata(); cloudBlob.DeleteIfExists(); } } } }