コード例 #1
0
        public async Task <byte[]> GetBlobAsync(string containerName, string blobName)
        {
            try
            {
                CloudBlobContainer container = _blobClient.GetContainerReference(containerName);
                ICloudBlob         blob      = container.GetBlobReferenceFromServer(blobName);
                var target    = new MemoryStream();
                var futureRes = blob.BeginDownloadToStream(target, null, null);
                return(await Task.Factory.FromAsync(futureRes, res =>
                {
                    blob.EndDownloadToStream(res);
                    byte[] array = target.ToArray();
                    return array;
                }));
            }
            catch (StorageException ex)
            {
                if (ex.RequestInformation.HttpStatusCode == 404)
                {
                    return(null);
                }

                throw;
            }
        }
コード例 #2
0
        public void DownloadBlobAsync(ICloudBlob blob, string LocalFile)
        {
            // The class currently stores state in class level variables so calling UploadBlobAsync or DownloadBlobAsync a second time will cause problems.
            // A better long term solution would be to better encapsulate the state, but the current solution works for the needs of my primary client.
            // Throw an exception if UploadBlobAsync or DownloadBlobAsync has already been called.
            lock (WorkingLock)
            {
                if (!Working)
                {
                    Working = true;
                }
                else
                {
                    throw new Exception("BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer.");
                }
            }

            // Create an async op in order to raise the events back to the client on the correct thread.
            asyncOp = AsyncOperationManager.CreateOperation(blob);

            TransferType = TransferTypeEnum.Download;
            m_Blob       = blob;
            m_FileName   = LocalFile;

            m_Blob.FetchAttributes();

            FileStream     fs      = new FileStream(m_FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
            ProgressStream pstream = new ProgressStream(fs);

            pstream.ProgressChanged += pstream_ProgressChanged;
            pstream.SetLength(m_Blob.Properties.Length);
            m_Blob.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount = 10;
            asyncresult = m_Blob.BeginDownloadToStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(m_Blob, pstream));
        }
コード例 #3
0
        /// <summary>
        /// Read a blob data into the given stream
        /// </summary>
        /// <returns>Return true on success, false if unable to create, throw exception on error</returns>
        public async Task <bool> GetBlobAsync(string containerName, string blobName, Stream content)
        {
            try
            {
                BlobRequestOptions opt = new BlobRequestOptions {
                    MaximumExecutionTime = GetDataTimeout
                };
                CloudBlobContainer container = _blobClient.GetContainerReference(containerName);
                ICloudBlob         blob      = container.GetBlobReferenceFromServer(blobName);
                var futureRes = blob.BeginDownloadToStream(content, null, opt);
                return(await Task.Factory.FromAsync(futureRes, res =>
                {
                    blob.EndDownloadToStream(res);
                    return true;
                }));
            }
            catch (StorageException ex)
            {
                if (ex.RequestInformation.HttpStatusCode == 404)
                {
                    return(false);
                }

                throw;
            }
        }
コード例 #4
0
 public static string DownloadTextAPM(ICloudBlob blob, Encoding encoding, AccessCondition accessCondition = null, BlobRequestOptions options = null, OperationContext operationContext = null)
 {
     using (MemoryStream stream = new MemoryStream())
     {
         using (AutoResetEvent waitHandle = new AutoResetEvent(false))
         {
             IAsyncResult result = blob.BeginDownloadToStream(stream, accessCondition, options, operationContext, ar => waitHandle.Set(), null);
             waitHandle.WaitOne();
             blob.EndDownloadToStream(result);
             return(encoding.GetString(stream.ToArray()));
         }
     }
 }
コード例 #5
0
 public static string DownloadTextAPM(ICloudBlob blob, Encoding encoding, AccessCondition accessCondition = null, BlobRequestOptions options = null, OperationContext operationContext = null)
 {
     using (MemoryStream stream = new MemoryStream())
     {
         using (AutoResetEvent waitHandle = new AutoResetEvent(false))
         {
             IAsyncResult result = blob.BeginDownloadToStream(stream, accessCondition, options, operationContext, ar => waitHandle.Set(), null);
             waitHandle.WaitOne();
             blob.EndDownloadToStream(result);
             return encoding.GetString(stream.ToArray());
         }
     }
 }
コード例 #6
0
        public void DisableContentMD5ValidationTestAPM()
        {
            BlobRequestOptions optionsWithNoMD5 = new BlobRequestOptions()
            {
                DisableContentMD5Validation = true,
                StoreBlobContentMD5         = true,
            };
            BlobRequestOptions optionsWithMD5 = new BlobRequestOptions()
            {
                DisableContentMD5Validation = false,
                StoreBlobContentMD5         = true,
            };

            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    IAsyncResult result;
                    ICloudBlob   blob = container.GetBlockBlobReference("blob1");
                    using (Stream stream = new NonSeekableMemoryStream())
                    {
                        blob.UploadFromStream(stream, null, optionsWithMD5);
                    }

                    using (Stream stream = new MemoryStream())
                    {
                        result = blob.BeginDownloadToStream(stream, null, optionsWithMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        blob.EndDownloadToStream(result);
                        result = blob.BeginDownloadToStream(stream, null, optionsWithNoMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        blob.EndDownloadToStream(result);

                        blob.Properties.ContentMD5 = "MDAwMDAwMDA=";
                        blob.SetProperties();

                        result = blob.BeginDownloadToStream(stream, null, optionsWithMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        TestHelper.ExpectedException(
                            () => blob.EndDownloadToStream(result),
                            "Downloading a blob with invalid MD5 should fail",
                            HttpStatusCode.OK);
                        result = blob.BeginDownloadToStream(stream, null, optionsWithNoMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        blob.EndDownloadToStream(result);
                    }

                    blob = container.GetPageBlobReference("blob2");
                    using (Stream stream = new MemoryStream())
                    {
                        blob.UploadFromStream(stream, null, optionsWithMD5);
                    }

                    using (Stream stream = new MemoryStream())
                    {
                        result = blob.BeginDownloadToStream(stream, null, optionsWithMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        blob.EndDownloadToStream(result);
                        result = blob.BeginDownloadToStream(stream, null, optionsWithNoMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        blob.EndDownloadToStream(result);

                        blob.Properties.ContentMD5 = "MDAwMDAwMDA=";
                        blob.SetProperties();

                        result = blob.BeginDownloadToStream(stream, null, optionsWithMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        TestHelper.ExpectedException(
                            () => blob.EndDownloadToStream(result),
                            "Downloading a blob with invalid MD5 should fail",
                            HttpStatusCode.OK);
                        result = blob.BeginDownloadToStream(stream, null, optionsWithNoMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        blob.EndDownloadToStream(result);
                    }
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
コード例 #7
0
        public void DownloadBlobAsync(ICloudBlob blob, string LocalFile)
        {
            // The class currently stores state in class level variables so calling UploadBlobAsync or DownloadBlobAsync a second time will cause problems.
            // A better long term solution would be to better encapsulate the state, but the current solution works for the needs of my primary client.
            // Throw an exception if UploadBlobAsync or DownloadBlobAsync has already been called.
            lock (WorkingLock)
            {
                if (!Working)
                    Working = true;
                else
                    throw new Exception("BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer.");
            }

            // Create an async op in order to raise the events back to the client on the correct thread.
            asyncOp = AsyncOperationManager.CreateOperation(blob);

            TransferType = TransferTypeEnum.Download;
            m_Blob = blob;
            m_FileName = LocalFile;

            m_Blob.FetchAttributes();

            FileStream fs = new FileStream(m_FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
            ProgressStream pstream = new ProgressStream(fs);
            pstream.ProgressChanged += pstream_ProgressChanged;
            pstream.SetLength(m_Blob.Properties.Length);
            m_Blob.ServiceClient.ParallelOperationThreadCount = 10;
            asyncresult = m_Blob.BeginDownloadToStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(m_Blob, pstream));
        }
コード例 #8
0
 public Task DownloadToStreamAsync(Stream target)
 {
     return(Task.Factory.FromAsync(_blob.BeginDownloadToStream(target, null, null), _blob.EndDownloadToStream));
 }