// get list of images give blob container name public IEnumerable <Uri> RetrieveImageUris(String containerName, String prefix = "") { IEnumerable <Uri> blobNames = new List <Uri>(); try { // get unescaped names of blobs that have image content var contentTypes = new String[] { "image/jpeg", "image/gif", "image/png" }; blobNames = from CloudBlob b in _authBlobClient.ListBlobsWithPrefix(String.Format("{0}/{1}", containerName, prefix), new BlobRequestOptions() { UseFlatBlobListing = true }) where contentTypes.Contains(b.Properties.ContentType) select b.Uri; } catch (Exception e) { Trace.TraceError("BlobUri: {0}{4}Exception: {1}{4}Message: {2}{4}Trace: {3}", String.Format("{0}/{1}", containerName, prefix), e.GetType(), e.Message, e.StackTrace, Environment.NewLine); throw; } return(blobNames); }
public IPartner GetPartnerByLoginName(string loginName) { IPartner result = null; string location = @"AzureDalManager.GetPartnerByLoginName"; Stopwatch watch = Stopwatch.StartNew(); try { CloudBlobClient client = this.storageAccount.CreateCloudBlobClient(); BlobRequestOptions requestOption = new BlobRequestOptions() { UseFlatBlobListing = true }; string prefix = string.Format(CultureInfo.InvariantCulture, "{0}/partneraccount/", this.container.Name); foreach (IListBlobItem partnetAccountBlobItem in client.ListBlobsWithPrefix(prefix, requestOption)) { string[] segments = partnetAccountBlobItem.Uri.Segments; if (segments != null && segments.Length > 1) { string lastSegment = segments[segments.Length - 1]; if (!string.IsNullOrWhiteSpace(lastSegment)) { if (loginName.Equals(lastSegment, StringComparison.OrdinalIgnoreCase)) { string partnerIdentifier = segments[segments.Length - 2].Trim('/'); if (!string.IsNullOrWhiteSpace(partnerIdentifier)) { DalPartner dal = new DalPartner(this.storageAccount, this.container); result = dal.Get(partnerIdentifier); break; } } } } } } catch (Exception exception) { LoggerFactory.Logger.Warning(location, EventId.DALGetPartnerByLoginName, "Error executing AzureDalManager.GetPartnerByLoginName({0}) in container {1}: {2}." , loginName, this.container.Name, exception.ToString()); } finally { watch.Stop(); LoggerFactory.Logger.Debug(location, "AzureDalManager.GetPartnerByLoginName() finished in {0} ms.", watch.ElapsedMilliseconds); } return(result); }
private void zipFolder(CloudBlobClient blobClient, string basePrefix, string folderName, string zipDir, ref ZipFile zipFile) { zipDir = string.IsNullOrEmpty(zipDir) ? folderName : zipDir + "/" + folderName; zipFile.AddDirectoryByName(zipDir); var folderPrefix = basePrefix + StorageNamesEncoder.EncodeBlobName(folderName) + "/"; var blobs = blobClient.ListBlobsWithPrefix(folderPrefix, 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])) { zipFolder(blobClient, folderPrefix, StorageNamesEncoder.DecodeBlobName(names[i]), zipDir, ref zipFile); break; } } } if (blob is CloudBlob) { var cloudBlob = blob as CloudBlob; var subStr = cloudBlob.Uri.ToString().Substring(cloudBlob.Uri.ToString().IndexOf(folderPrefix) + folderPrefix.Length); var index = subStr.LastIndexOf('/'); if (index < 0) { index = 0; } var subFolderName = subStr.Substring(0, index); var fileName = subStr.Substring(index); var bytes = cloudBlob.DownloadByteArray(); if (!string.IsNullOrEmpty(subFolderName)) { zipFile.AddDirectoryByName(zipDir + "/" + StorageNamesEncoder.DecodeBlobName(subFolderName)); } zipFile.AddEntry(zipDir + "/" + StorageNamesEncoder.DecodeBlobName(subStr), bytes); } } }
public CloudFileCollection GetFileListing(string path) { String prefix = String.Concat(ContainerName, "/", ParsePath(path)); var files = new CloudFileCollection(); files.AddRange( _blobClient.ListBlobsWithPrefix(prefix).OfType <CloudBlob>().Select( blob => new AzureCloudFile { Meta = blob.Metadata, Uri = blob.Uri, Size = blob.Properties.Length, ContentType = blob.Properties.ContentType })); return(files); }
public IEnumerable <Uri> ListFiles(String containerName) { List <Uri> ret = new List <Uri>(); StorageCredentials credentials = new StorageCredentialsAccountAndKey(accountName, accountKey); CloudStorageAccount storageAccountInfo = new CloudStorageAccount(credentials, blobEndpoint, queueEndpoint, tableEndpoint); CloudBlobClient cloudBlobClient = storageAccountInfo.CreateCloudBlobClient(); cloudBlobClient.Timeout = new TimeSpan(0, 30, 0); IEnumerable <IListBlobItem> blobs = cloudBlobClient.ListBlobsWithPrefix(containerName); foreach (IListBlobItem blob in blobs) { ret.Add(blob.Uri); } return(ret); }
// download log private void Button_Click(object sender, RoutedEventArgs e) { log_g.IsEnabled = false; if (_account == null) { MessageBox.Show("Storage account haven't been initialized"); log_g.IsEnabled = true; return; } CloudBlobClient client = _account.CreateCloudBlobClient(); string startdatestr = null; string enddatestr = null; string filename = null; string curtypestr = _currentstoragetype.ToString().ToLower(); DateTime startdate = DateTime.MinValue; DateTime enddate = DateTime.MinValue; // time range if (currb.IsChecked.HasValue && currb.IsChecked.Value) { if (_selectedmte == null) { MessageBox.Show("no timepoint is selected."); log_g.IsEnabled = true; return; } startdate = _selectedmte.Time; enddate = startdate.Add(_selectedmte.TimeSpan); } else if (allrb.IsChecked.HasValue && allrb.IsChecked.Value) { startdate = start_dp.SelectedDate.Value.Add((TimeSpan)start_cb.SelectedItem); enddate = end_dp.SelectedDate.Value.Add((TimeSpan)end_cb.SelectedItem); } startdatestr = "$logs/" + curtypestr + startdate.ToString("/yyyy/MM/dd/HHmm"); enddatestr = "$logs/" + curtypestr + enddate.ToString("/yyyy/MM/dd/HHmm"); filename = string.Format("{0}_{1}_logging_{2}__{3}", _account.Credentials.AccountName, curtypestr, startdate.ToString("yyyy_MM_dd_HHmm"), enddate.ToString("yyyy_MM_dd_HHmm")); // format if (csvrb.IsChecked.HasValue && csvrb.IsChecked.Value) { filename += ".csv"; } else { filename += ".log"; } // list with prefix var prefixstr = FindPrefix(startdatestr, enddatestr); var items = client.ListBlobsWithPrefix(prefixstr, new BlobRequestOptions { UseFlatBlobListing = true, }).ToList(); // filter with time range var filteritems = items.Where( (item) => { // $logs/blob/2011/11/21/2000/000000.log var uripath = item.Uri.AbsolutePath.TrimStart('/'); if (uripath.CompareTo(startdatestr) >= 0 && uripath.CompareTo(enddatestr) < 0) { return(true); } else { return(false); } }).Select(i => (CloudBlob)i) .ToList(); if (filteritems.Count == 0) { MessageBox.Show("No logs in selected time"); log_g.IsEnabled = true; return; } var dialog = new System.Windows.Forms.FolderBrowserDialog(); if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { BackgroundWorker bw = new BackgroundWorker(); bw.WorkerReportsProgress = true; bw.DoWork += new DoWorkEventHandler(bw_DoWork); bw.RunWorkerAsync(new object[] { filteritems, dialog.SelectedPath + "\\" + filename }); bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged); bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted); } else { log_g.IsEnabled = true; } }
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(); } } } }