public static BlobFile DownloadFile(Guid id) { var fileInfo = db.Files.FirstOrDefault(f => f.File_Id == id); if (fileInfo == null) { // means that no file return(null); } var uniqueBlobName = String.Format("files/{0}", id); CloudBlockBlob blob = container.GetBlockBlobReference(uniqueBlobName); // catch here errors var data = blob.DownloadByteArray(); var res = new BlobFile { ID = fileInfo.File_Id, Name = fileInfo.Name, CreationTime = fileInfo.CreationTime, Description = fileInfo.Description, LongDescription = fileInfo.LongComment, Data = data }; return(res); }
public static void CommandQueuePoller(CommandHandler service) { //Trace.WriteLine("LogPoller has started -- seeing if there's anything in the queue for me!!"); CloudQueueMessage msg = CommandQueue.GetMessage(); if (msg == null) { //Trace.TraceInformation("COMMAND QUEUE nothing found - going back to sleep"); } while (msg != null) { string myMessage = msg.AsString; Trace.TraceInformation("Got message {0}", myMessage); //probably should do some checking here before deleting... CommandQueue.DeleteMessage(msg); //split the ID from the type -- first item is ID, second is type string[] messageParts = myMessage.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); if (messageParts.Length != 2) { Trace.TraceError("COMMAND QUEUE message improperly formed. {0}", myMessage); continue; } CloudBlockBlob theBlob = CommandBlobContainer.GetBlockBlobReference(messageParts[0]); Type commandType = Type.GetType(messageParts[1]); CommandBase theCommand = null; using (MemoryStream msBlob = new MemoryStream()) { byte[] logBytes = theBlob.DownloadByteArray(); msBlob.Write(logBytes, 0, logBytes.Length); BinaryFormatter bf = new BinaryFormatter(); msBlob.Position = 0; object theObject = bf.Deserialize(msBlob); theCommand = Convert.ChangeType(theObject, commandType) as CommandBase; } if (theCommand != null) { service.Handle(theCommand); } else { Trace.TraceInformation("COMMAND BLOB Could not deserialize message from queue id {0}", myMessage); } msg = CommandQueue.GetMessage(); } }
private static void DeserializeObjectFromBlob <T>(CloudBlockBlob theBlob, out T theObject) where T : class { using (MemoryStream msBlob = new MemoryStream()) { byte[] logBytes = theBlob.DownloadByteArray(); msBlob.Write(logBytes, 0, logBytes.Length); BinaryFormatter bf = new BinaryFormatter(); msBlob.Position = 0; theObject = bf.Deserialize(msBlob) as T; } }
public Stream GetFileFromPath(string path) { string[] cont = path.Split('/'); string containerName = cont[0]; string fileName = cont[1]; CloudBlobContainer objContainer; objContainer = CloudBlobClient.GetContainerReference(containerName); CloudBlockBlob blob = objContainer.GetBlockBlobReference(fileName); return(new MemoryStream(blob.DownloadByteArray())); }
public static byte[] DownloadIntellect(Guid intellectID) { string name1 = (from b in db.Intellect where b.Intellect_ID == intellectID select b.Intellect_Name).First <string>(); Guid name2 = (from a in db.Account where a.Account_ID == ((from b in db.Intellect where b.Intellect_ID == intellectID select b.AccountAccount_ID).FirstOrDefault <Guid>()) select a.Account_ID).First <Guid>(); string neededname = string.Format("intellects/{0}/{1}", name2.ToString(), name1); CloudBlockBlob blob = container.GetBlockBlobReference(neededname); return(blob.DownloadByteArray()); }
/* * Sample call for upload:- * byte[] array = new byte[1024*1024*1024]; * Random random = new Random(); * random.NextBytes(array); * double timeTaken_Upload = Experiment.doRawCloudPerf(array, SynchronizerType.Azure, SynchronizeDirection.Upload, "fooContainer", "fooBlob"); * double timeTaken_Download = Experiment.doRawCloudPerf(array, SynchronizerType.Azure, SynchronizeDirection.Download, "fooContainer", "fooBlob"); * * */ public static double doRawCloudPerf(byte[] input, SynchronizerType synchronizerType, SynchronizeDirection syncDirection, string exp_directory, Logger logger, string containerName = null, string blobName = null) { string accountName = ConfigurationManager.AppSettings.Get("AccountName"); string accountKey = ConfigurationManager.AppSettings.Get("AccountSharedKey"); DateTime begin = DateTime.Now, end = DateTime.Now; if (synchronizerType == SynchronizerType.Azure) { #region azure download/upload if (containerName == null) { containerName = "testingraw"; } if (blobName == null) { blobName = Guid.NewGuid().ToString(); } CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentialsAccountAndKey(accountName, accountKey), true); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference(containerName); if (syncDirection == SynchronizeDirection.Upload) { logger.Log("Start Stream Append"); container.CreateIfNotExist(); begin = DateTime.UtcNow;////////////////////////////////////// try { using (MemoryStream memoryStream = new System.IO.MemoryStream(input)) { CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName); blockBlob.UploadFromStream(memoryStream); } } catch (Exception e) { Console.WriteLine("exception: " + e); } end = DateTime.UtcNow;////////////////////////////////////// logger.Log("End Stream Append"); } if (syncDirection == SynchronizeDirection.Download) { logger.Log("Start Stream Get"); logger.Log("Start Stream GetAll"); try { CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName); byte[] blobContents = blockBlob.DownloadByteArray(); //if (File.Exists(blobName)) // File.Delete(blobName); begin = DateTime.UtcNow;////////////////////////////////////// // using (FileStream fs = new FileStream(blobName, FileMode.OpenOrCreate)) // { byte[] contents = blockBlob.DownloadByteArray(); // fs.Write(contents, 0, contents.Length); // } } catch (Exception e) { Console.WriteLine("exception: " + e); } end = DateTime.UtcNow;////////////////////////////////////// logger.Log("End Stream Get"); logger.Log("End Stream GetAll"); } #endregion } else if (synchronizerType == SynchronizerType.AmazonS3) { #region amazon s3 stuff if (containerName == null) { containerName = "testingraw"; } if (blobName == null) { blobName = Guid.NewGuid().ToString(); } AmazonS3Client amazonS3Client = new AmazonS3Client(accountName, accountKey); if (syncDirection == SynchronizeDirection.Upload) { ListBucketsResponse response = amazonS3Client.ListBuckets(); foreach (S3Bucket bucket in response.Buckets) { if (bucket.BucketName == containerName) { break; } } amazonS3Client.PutBucket(new PutBucketRequest().WithBucketName(containerName)); begin = DateTime.UtcNow;////////////////////////////////////// MemoryStream ms = new MemoryStream(); ms.Write(input, 0, input.Length); PutObjectRequest request = new PutObjectRequest(); request.WithBucketName(containerName); request.WithKey(blobName); request.InputStream = ms; amazonS3Client.PutObject(request); end = DateTime.UtcNow;////////////////////////////////////// } if (syncDirection == SynchronizeDirection.Download) { if (File.Exists(blobName)) { File.Delete(blobName); } begin = DateTime.UtcNow;////////////////////////////////////// GetObjectRequest request = new GetObjectRequest(); request.WithBucketName(containerName); request.WithKey(blobName); GetObjectResponse response = amazonS3Client.GetObject(request); var localFileStream = File.Create(blobName); response.ResponseStream.CopyTo(localFileStream); localFileStream.Close(); end = DateTime.UtcNow;////////////////////////////////////// } #endregion } else { throw new InvalidDataException("syncronizer type is not valid"); } return((end - begin).TotalMilliseconds);// return total time to upload in milliseconds }