byte[] BufferTip(long position, int count) { var buffer = new byte[count]; using (var s = _blob.OpenRead()) { s.ReadAheadSize = count; s.Seek(position, SeekOrigin.Begin); s.Read(buffer, 0, count); return(buffer); } }
// Get a page of a page blob. // Return true on success, false if unable to create, throw exception on error. public bool GetPage(string containerName, string blobName, int pageOffset, int pageSize, out string content) { content = null; try { CloudBlobContainer container = BlobClient.GetContainerReference(containerName); CloudPageBlob blob = container.GetPageBlobReference(blobName); BlobStream stream = blob.OpenRead(); byte[] data = new byte[pageSize]; stream.Seek(pageOffset, SeekOrigin.Begin); stream.Read(data, 0, pageSize); content = new UTF8Encoding().GetString(data); stream.Close(); return(true); } catch (StorageClientException ex) { if ((int)ex.StatusCode == 404) { return(false); } throw; } }
public override int Read(byte[] buffer, int offset, int count) { if (count % PageSize != 0) { throw new Exception("offset not page aligned"); } if (count == 0) { return(0); } long len = Length; if (Position + count > len) // Length will send HTTP request { return(0); } using (var _stream = _blob.OpenRead()) { _stream.Seek(Position, SeekOrigin.Begin); int readBytes = _stream.Read(buffer, offset, count); _position += readBytes; return(readBytes); } }
public static VhdFooter GetVhdFooter(this CloudPageBlob basePageBlob) { var vhdFileFactory = new VhdFileFactory(); using (var file = vhdFileFactory.Create(basePageBlob.OpenRead())) { return(file.Footer); } }
public PageBlobReadStream(CloudPageBlob blob) { _blob = blob; if (!_blob.Exists()) { throw new ArgumentException(); } _reader = new BinaryReader(_blob.OpenRead()); }
static void Main(string[] args) { string uri = @"msds:nc?file=\\vienna.mslab.cs.msu.su\ClimateData\air.sig995.2007.nc&openMode=readOnly"; DataSet d = DataSet.Open(uri); string blobUri = @"msds:ab?UseDevelopmentStorage=true&Container=testcontainer&Blob=testblob"; DataSet blobD = /*new AzureBlobDataSet(blobUri);*/ AzureBlobDataSet.ArrangeData(blobUri, d, new SerializableVariableSchema[0]); AzureBlobDataSetUri azureUri = new AzureBlobDataSetUri(blobUri); CloudStorageAccount storageAccount = CloudStorageAccount.Parse(azureUri.ConnectionString); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // Retrieve a reference to a container CloudBlobContainer container = blobClient.GetContainerReference(azureUri.Container); CloudPageBlob blob = container.GetPageBlobReference(azureUri.Blob); Int32 schemeSize; using (BinaryReader br = new BinaryReader(blob.OpenRead())) { //schemeSize = br.ReadInt32(); UTF8Encoding utf8 = new UTF8Encoding(); byte[] buffer = new byte[8192]; br.BaseStream.Read(buffer, 0, 8192); string sizeStr = utf8.GetString(buffer); Console.WriteLine(sizeStr); //schemeSize = Int32.Parse(sizeStr); //br.BaseStream.Seek(512, SeekOrigin.Begin); //DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(DataSetInfo)); //byte[] scheme = new byte[schemeSize]; //br.BaseStream.Read(scheme, 0, schemeSize); //info = (DataSetInfo)serializer.ReadObject(new MemoryStream(scheme)); } //foreach (var i in blobD.Variables) Console.WriteLine(i.Name); Single[] a = (Single[])blobD["lat"].GetData(); blobD["lat"].PutData(a); //foreach (var i in a) Console.WriteLine(i.ToString()); foreach (var i in blobD["lat"].Metadata) { Console.WriteLine(i.Key.ToString() + " = " + i.Value.ToString()); } foreach (var i in blobD.Metadata) { Console.WriteLine(i.Key.ToString() + " = " + i.Value.ToString()); } Console.ReadLine(); }
public byte[] ReadPage(long offset) { Require.OffsetMultiple("offset", offset, PageSize); using (var stream = _blob.OpenRead()) { var buffer = new byte[PageSize]; stream.Seek(offset, SeekOrigin.Begin); stream.Read(buffer, 0, PageSize); return(buffer); } }
public static void DownloadVHDFromCloud(CloudBlobClient blobStorage, string containerName, string blobName) { CloudBlobContainer container = blobStorage.GetContainerReference(containerName); CloudPageBlob pageBlob = container.GetPageBlobReference(blobName); // Get the length of the blob pageBlob.FetchAttributes(); long vhdLength = pageBlob.Properties.Length; long totalDownloaded = 0; Console.WriteLine("Vhd size: " + Megabytes(vhdLength)); // Create a new local file to write into FileStream fileStream = new FileStream(blobName, FileMode.Create, FileAccess.Write); fileStream.SetLength(128 * OneMegabyteAsBytes); // Download the valid ranges of the blob, and write them to the file IEnumerable <PageRange> pageRanges = pageBlob.GetPageRanges(); Stream blobStream = pageBlob.OpenRead(); foreach (PageRange range in pageRanges) { // EndOffset is inclusive... so need to add 1 int rangeSize = (int)(range.EndOffset + 1 - range.StartOffset); // Chop range into 4MB chucks, if needed for (int subOffset = 0; subOffset < rangeSize; subOffset += FourMegabyteAsBytes) { int subRangeSize = Math.Min(rangeSize - subOffset, FourMegabyteAsBytes); blobStream.Seek(range.StartOffset + subOffset, SeekOrigin.Begin); fileStream.Seek(range.StartOffset + subOffset, SeekOrigin.Begin); Console.WriteLine("Range: ~" + Megabytes(range.StartOffset + subOffset) + " + " + PrintSize(subRangeSize)); byte[] buffer = new byte[subRangeSize]; blobStream.Read(buffer, 0, subRangeSize); fileStream.Write(buffer, 0, subRangeSize); totalDownloaded += subRangeSize; if (totalDownloaded > 128 * OneMegabyteAsBytes) { break; } } if (totalDownloaded > 128 * OneMegabyteAsBytes) { break; } } Console.WriteLine("Downloaded " + Megabytes(totalDownloaded) + " of " + Megabytes(vhdLength)); }
public PageBlobAppendStream(CloudPageBlob blob) { _blob = blob; if (blob == null) { throw new ArgumentNullException("blob"); } _reader = new BinaryReader(_blob.OpenRead()); if (!blob.Exists()) { _blob.Create(0); _lastPageIndex = -1; return; } _blobLength = _blob.Properties.Length; _lastPageIndex = (_blobLength / PageSize) - 1; }
protected virtual byte[] GetBytesFromBlob(long offset, int count) { byte[] buffer = new byte[count]; using (var stream = blob.OpenRead(new BlobRequestOptions() { Timeout = TimeSpan.FromMinutes(30.0) })) { int curOffset = 0; int curCount = count; int temp = 0; stream.Seek(offset, SeekOrigin.Begin); do { temp = stream.Read(buffer, curOffset, count); curOffset += temp; curCount -= temp; }while (curOffset < count); } return(buffer); }
public AzureBlobDataSet(string uri) { AzureBlobDataSetUri azureUri = null; if (DataSetUri.IsDataSetUri(uri)) { azureUri = new AzureBlobDataSetUri(uri); } else { azureUri = AzureBlobDataSetUri.ToUri(uri); } this.uri = azureUri; CloudStorageAccount storageAccount;// = CloudStorageAccount.Parse(azureUri.ConnectionString); if (CloudStorageAccount.TryParse(azureUri.ConnectionString, out storageAccount)) { CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // Retrieve a reference to a container CloudBlobContainer container = blobClient.GetContainerReference(azureUri.Container); blob = container.GetPageBlobReference(azureUri.Blob); } else { blob = new CloudPageBlob(@"http://" + azureUri.AccountName + @".blob.core.windows.net/" + azureUri.Container + @"/" + azureUri.Blob); _anonymous = true; } SerializableDataSetSchema info; Int32 schemeSize; using (BinaryReader br = new BinaryReader(blob.OpenRead())) { int curOffset = 0; int curCount = 512; int temp = 0; UTF8Encoding utf8 = new UTF8Encoding(); byte[] buffer = new byte[512]; do { temp = br.BaseStream.Read(buffer, curOffset, curCount); curOffset += temp; curCount -= temp; } while (curOffset < 512); string sizeStr = utf8.GetString(buffer); schemeSize = Int32.Parse(sizeStr); br.BaseStream.Seek(512, SeekOrigin.Begin); DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(SerializableDataSetSchema)); byte[] scheme = new byte[schemeSize]; curOffset = 0; curCount = schemeSize; do { temp = br.BaseStream.Read(scheme, curOffset, curCount); curOffset += temp; curCount -= temp; } while (curOffset < schemeSize); info = (SerializableDataSetSchema)serializer.ReadObject(new MemoryStream(scheme)); } bool savedAutoCommitState = IsAutocommitEnabled; IsAutocommitEnabled = false; Initialize(schemeSize, info); Commit(); if (_anonymous) { this.SetCompleteReadOnly(); } IsAutocommitEnabled = savedAutoCommitState; _IsInitialized = true; }