public static List<string> CreateBlobs(CloudBlobContainer container, int count, BlobType type)
        {
            string name;
            List<string> blobs = new List<string>();
            for (int i = 0; i < count; i++)
            {
                switch (type)
                {
                    case BlobType.BlockBlob:
                        name = "bb" + Guid.NewGuid().ToString();
                        CloudBlockBlob blockBlob = container.GetBlockBlobReference(name);
                        blockBlob.PutBlockList(new string[] { });
                        blobs.Add(name);
                        break;

                    case BlobType.PageBlob:
                        name = "pb" + Guid.NewGuid().ToString();
                        CloudPageBlob pageBlob = container.GetPageBlobReference(name);
                        pageBlob.Create(0);
                        blobs.Add(name);
                        break;
                }
            }
            return blobs;
        }
        /// <summary>
        /// Upload the generated receipt Pdf to Blob storage.
        /// </summary>
        /// <param name="file">Byte array containig the Pdf file contents to be uploaded.</param>
        /// <param name="fileName">The desired filename of the uploaded file.</param>
        /// <returns></returns>
        public string UploadPdfToBlob(byte[] file, string fileName)
        {
            // Create the blob client.
            blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            blobContainer = blobClient.GetContainerReference(receiptBlobName);

            // Create the container if it doesn't already exist.
            blobContainer.CreateIfNotExists(BlobContainerPublicAccessType.Blob);

            string fileUri = string.Empty;

            CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileName);

            using (var stream = new MemoryStream(file))
            {
                // Upload the in-memory Pdf file to blob storage.
                blockBlob.UploadFromStream(stream);
            }

            fileUri = blockBlob.Uri.ToString();

            return fileUri;
        }
 private CloudBlockBlob GetBlob(string path, CloudBlobContainer container = null)
 {
     container = container ?? GetContainer(path);
     var blobName = GetBlobName(path);
     var blob = container.GetBlockBlobReference(blobName);
     return blob;
 }
Esempio n. 4
0
        static string GetBlobSasUri(CloudBlobContainer container)
        {
            //Get a reference to a blob within the container.
            CloudBlockBlob blob = container.GetBlockBlobReference("sasblob.txt");

            //Upload text to the blob. If the blob does not yet exist, it will be created.
            //If the blob does exist, its existing content will be overwritten.
            string blobContent = "This blob will be accessible to clients via a Shared Access Signature.";
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(blobContent));
            ms.Position = 0;
            using (ms)
            {
                blob.UploadFromStream(ms);
            }

            //Set the expiry time and permissions for the blob.
            //In this case the start time is specified as a few minutes in the past, to mitigate clock skew.
            //The shared access signature will be valid immediately.
            SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
            sasConstraints.SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5);
            sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddHours(4);
            sasConstraints.Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write;

            //Generate the shared access signature on the blob, setting the constraints directly on the signature.
            string sasBlobToken = blob.GetSharedAccessSignature(sasConstraints);

            //Return the URI string for the container, including the SAS token.
            return blob.Uri + sasBlobToken;
        }
 public static void UploadFileToBlob(string fileName, string containerSAS)
 {
     Console.WriteLine("Uploading {0} to {1}", fileName, containerSAS);
     CloudBlobContainer container = new CloudBlobContainer(new Uri(containerSAS));
     CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
     blob.UploadFromStream(new FileStream(fileName, FileMode.Open, FileAccess.Read));
 }
        public ConfigurationFile(CloudBlobContainer DeploymentContainer, string file)
        {
            //var config = DeploymentContainer.GetDirectoryReference("Configuration");
            init(DeploymentContainer.GetBlockBlobReference(file));

            //
        }
        public static List<string> CreateLogs(CloudBlobContainer container, StorageService service, int count, DateTime start, string granularity)
        {
            string name;
            List<string> blobs = new List<string>();

            for (int i = 0; i < count; i++)
            {
                CloudBlockBlob blockBlob;

                switch (granularity)
                {
                    case "hour":
                        name = string.Concat(service.ToString().ToLowerInvariant(), "/", start.AddHours(i).ToString("yyyy/MM/dd/HH", CultureInfo.InvariantCulture), "00/000001.log"); 
                        break;

                    case "day":
                        name = string.Concat(service.ToString().ToLowerInvariant(), "/", start.AddDays(i).ToString("yyyy/MM/dd/HH", CultureInfo.InvariantCulture), "00/000001.log"); 
                        break;

                    case "month":
                        name = string.Concat(service.ToString().ToLowerInvariant(), "/", start.AddMonths(i).ToString("yyyy/MM/dd/HH", CultureInfo.InvariantCulture), "00/000001.log"); 
                        break;

                    default:
                        throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "CreateLogs granularity of '{0}' is invalid.", granularity));
                }

                blockBlob = container.GetBlockBlobReference(name);
                blockBlob.PutBlockList(new string[] { });
                blobs.Add(name);
            }

            return blobs;
        }
Esempio n. 8
0
        private async Task InsertTodoItem(DataModel.TodoItem todoItem)
        {
            string errorString = string.Empty;

            if (media != null)
            {
                todoItem.ContainerName = "todoitemimages";
                todoItem.ResourceName = Guid.NewGuid().ToString();
            }

            await todoTable.InsertAsync(todoItem);

            if (!string.IsNullOrEmpty(todoItem.SasQueryString))
            {
                StorageCredentials cred = new StorageCredentials(todoItem.SasQueryString);
                var imageUri = new Uri(todoItem.ImageUri);

                CloudBlobContainer container = new CloudBlobContainer(
                    new Uri(string.Format("https://{0}/{1}",
                        imageUri.Host, todoItem.ContainerName)), cred);

                using (var inputStream = await media.OpenReadAsync())
                {
                    CloudBlockBlob blobFromSASCredential =
                        container.GetBlockBlobReference(todoItem.ResourceName);
                    await blobFromSASCredential.UploadFromStreamAsync(inputStream);
                }
                
                await ResetCaptureAsync();
            }

            items.Add(todoItem);
        }
Esempio n. 9
0
        static void Main(string[] args)
        {
            string sas = "https://jamborstorage.blob.core.windows.net/sascontainer?sv=2014-02-14&sr=c&sig=HaD3DPQYd%2FQMJnuvYRafx0NuQQWSm0ZelODLxbyjEWI%3D&se=2014-09-03T09%3A43%3A28Z&sp=wl";
            CloudBlobContainer container = new CloudBlobContainer(new Uri(sas));

            //Create a list to store blob URIs returned by a listing operation on the container.
            List<Uri> blobUris = new List<Uri>();

            try
            {
                //Write operation: write a new blob to the container.
                CloudBlockBlob blob = container.GetBlockBlobReference("blobCreatedViaSAS.txt");
                string blobContent = "This blob was created with a shared access signature granting write permissions to the container. ";
                MemoryStream msWrite = new MemoryStream(Encoding.UTF8.GetBytes(blobContent));
                msWrite.Position = 0;
                using (msWrite)
                {
                    blob.UploadFromStream(msWrite);
                }
                Console.WriteLine("Write operation succeeded for SAS " + sas);
                Console.WriteLine();
            }
            catch (StorageException e)
            {
                Console.WriteLine("Write operation failed for SAS " + sas);
                Console.WriteLine("Additional error information: " + e.Message);
                Console.WriteLine();
            }
        }
Esempio n. 10
0
        public static async Task<List<string>> CreateBlobsAsync(CloudBlobContainer container, int count, BlobType type)
        {
            string name;
            List<string> blobs = new List<string>();
            for (int i = 0; i < count; i++)
            {
                switch (type)
                {
                    case BlobType.BlockBlob:
                        name = "bb" + Guid.NewGuid().ToString();
                        CloudBlockBlob blockBlob = container.GetBlockBlobReference(name);
                        await blockBlob.PutBlockListAsync(new string[] { });
                        blobs.Add(name);
                        break;

                    case BlobType.PageBlob:
                        name = "pb" + Guid.NewGuid().ToString();
                        CloudPageBlob pageBlob = container.GetPageBlobReference(name);
                        await pageBlob.CreateAsync(0);
                        blobs.Add(name);
                        break;

                    case BlobType.AppendBlob:
                        name = "ab" + Guid.NewGuid().ToString();
                        CloudAppendBlob appendBlob = container.GetAppendBlobReference(name);
                        await appendBlob.CreateOrReplaceAsync();
                        blobs.Add(name);
                        break;
                }
            }
            return blobs;
        }
Esempio n. 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BlobFile" /> class.
 /// </summary>
 /// <param name="fileSystem">The file system.</param>
 /// <param name="address">The address.</param>
 /// <exception cref="ArgumentOutOfRangeException"/>
 /// <exception cref="ArgumentNullException"/>
 public BlobFile(BlobFileSystem fileSystem, INodeAddress address)
     : base(address, fileSystem)
 {
     _blobContainer = fileSystem.BlobClient.GetContainerReference(address.PathToDepth(1).Substring(1));
     _path = string.Join("/", Address.AbsolutePath.Split('/').Where((item, index) => index > 1));
     _blockBlob = _blobContainer.GetBlockBlobReference(_path);
 }
        public static void ParallelUploadFile(CloudBlobContainer blobContainer, string blobName, string filePath, string contentType = null)
        {
            var start = DateTime.Now;

              Console.WriteLine("\nUpload file in parallel.");
              CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(blobName);

              // 2M blocks for demo
              int blockLength = 2 * 1000 * 1024;
              byte[] dataToUpload = File.ReadAllBytes(filePath);
              int numberOfBlocks = (dataToUpload.Length / blockLength) + 1;
              string[] blockIds = new string[numberOfBlocks];

              Parallel.For(0, numberOfBlocks, x =>
              {
            var blockId = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
            var currentLength = Math.Min(blockLength, dataToUpload.Length - (x * blockLength));

            using (var memStream = new MemoryStream(dataToUpload, x * blockLength, currentLength))
            {
              blockBlob.PutBlock(blockId, memStream, null);
            }
            blockIds[x] = blockId;
            Console.WriteLine("BlockId:{0}", blockId);
              });

              if (!String.IsNullOrEmpty(contentType)) { blockBlob.Properties.ContentType = contentType; }
              blockBlob.PutBlockList(blockIds);
              Console.WriteLine("URL:{0}, ETag:{1}", blockBlob.Uri, blockBlob.Properties.ETag);

              var timespan = DateTime.Now - start;
              Console.WriteLine("{0} seconds.", timespan.Seconds);
        }
        internal void UploadFromString(string accountName, string accountKey, string containerName, string fileName, string sourceFileName, string fileContentType, string content)   //, string name, string fileDescription) {
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount =
                Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(
                    string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1};BlobEndpoint=https://{0}.blob.core.windows.net/", accountName, accountKey)
                    );
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobClient = storageAccount.CreateCloudBlobClient();
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container  = blobClient.GetContainerReference(containerName);
            container.CreateIfNotExists();
            string ext = System.IO.Path.GetExtension(sourceFileName);

            //string fileName = String.Format(

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
            blockBlob.Properties.ContentType = fileContentType;
            //blockBlob.Metadata.Add("name", name);
            blockBlob.Metadata.Add("originalfilename", sourceFileName);
            //blockBlob.Metadata.Add("userid", userId.ToString());
            //blockBlob.Metadata.Add("ownerid", userId.ToString());
            DateTime created = DateTime.UtcNow;

            // https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
            // http://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-a-iso-8601-date-in-string-format
            //blockBlob.Metadata.Add("username", userName);
            blockBlob.Metadata.Add("created", created.ToString("yyyy-MM-ddTHH:mm:ss"));  // "yyyy-MM-ddTHH:mm:ssZ"
            blockBlob.Metadata.Add("modified", created.ToString("yyyy-MM-ddTHH:mm:ss")); // "yyyy-MM-ddTHH:mm:ssZ"
            blockBlob.Metadata.Add("fileext", ext);

            blockBlob.UploadText(content, Encoding.UTF8); // .UploadFromStream(fileInputStream);

            blockBlob.SetMetadata();
        }
Esempio n. 14
0
 public static void DownloadBlob(CloudBlobContainer container)
 {
     CloudBlockBlob blockBlob = container.GetBlockBlobReference("photo1.jpg");
     using (var fileStream = System.IO.File.OpenWrite(@"path\myfile"))
     {
         blockBlob.DownloadToStream(fileStream);
     }
 }
Esempio n. 15
0
        /// <summary>
        /// Upload files by using Container Uri
        /// </summary>
        /// <param name="sasUri"></param>
        /// <param name="localFilePath"></param>
        /// <param name="filePathInSyncFolder"></param>
        public bool UploadFileWithContainerUri(string sasUri, string localFilePath,
            string filePathInSyncFolder, string fileHashVaule, DateTime fileTimestamp, string eventType)
        {
            try
            {

                CloudBlobContainer container = new CloudBlobContainer(new Uri(sasUri));

                if (fileHashVaule == "isDirectory")
                {
                    CloudBlockBlob blob = container.GetBlockBlobReference(filePathInSyncFolder);
                    LocalFileSysAccess.LocalFileSys uploadfromFile = new LocalFileSysAccess.LocalFileSys();
                    uploadfromFile.uploadfromFilesystem(blob, localFilePath, eventType);
                    //blob.UploadFromFile(localFilePath, FileMode.Open);
                    //directory.Metadata["hashValue"] = fileHashVaule;

                }
                else
                {

                    CloudBlockBlob blob = container.GetBlockBlobReference(filePathInSyncFolder);
                    LocalFileSysAccess.LocalFileSys uploadfromFile = new LocalFileSysAccess.LocalFileSys();
                    uploadfromFile.uploadfromFilesystem(blob, localFilePath, eventType);

                    //blob.UploadFromFile(localFilePath, FileMode.Open);
                    blob.Metadata["hashValue"] = fileHashVaule;
                    blob.Metadata["timestamp"] = fileTimestamp.ToUniversalTime().ToString("MM/dd/yyyy HH:mm:ss");
                    blob.Metadata["filePath"] = filePathInSyncFolder;
                    //blob.Metadata["Deleted"] = "false";
                    blob.SetMetadata();
                    blob.CreateSnapshot();

                }

            }
            catch (Exception e)
            {
                Program.ClientForm.addtoConsole(e.ToString());
                Console.WriteLine(e.Message);
                //MessageBox.Show("-----------" + e.Message);
                return false;
            }

            return true;
        }
Esempio n. 16
0
 static void getMetaData(CloudBlobContainer container, string filename)
 {
     CloudBlockBlob blob = container.GetBlockBlobReference(filename);
     blob.FetchAttributes();
     foreach (var atr in blob.Metadata)
     {
         Console.WriteLine(atr.Key + " " + atr.Value);
     }
 }
Esempio n. 17
0
        /*
        To upload a file, use the FileStream object to access the stream, and then use the
        UploadFromFileStream method on the CloudBlockBlob class to upload the file to Azure
        blob storage:
        */
        public static void UploadFile(CloudBlobContainer container)
        {
            CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");
            using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
            {
                blockBlob.UploadFromStream(fileStream);

            }
        }
Esempio n. 18
0
        /// <summary>
        /// The main entry point.
        /// </summary>
        /// <param name="args">
        /// The command line arguments. Not used.
        /// </param>
        private static void Main(string[] args)
        {
            Console.WriteLine("Hello!");

            // Create a connection to the web api server
            string command;
            var client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:10281/");

            do
            {
                // Load the file to transmit
                string fileName = string.Empty;
                while (string.IsNullOrEmpty(fileName) || !File.Exists(fileName))
                {
                    Console.Write("Enter the full name of the file you like to send to server: ");
                    fileName = Console.ReadLine();
                }

                var fileInfo = new FileInfo(fileName);

                // Get the SAS-URLs from the server
                Console.WriteLine("Asking Server for SAS-URLs");
                HttpResponseMessage resp = client.GetAsync("api/Data").Result;
                resp.EnsureSuccessStatusCode();
                var dataDto = resp.Content.ReadAsAsync<DataDto>().Result;

                Console.WriteLine("Server responds:");
                Console.WriteLine("BLOB Container URL: " + dataDto.BlobContainerUrl);
                Console.WriteLine("BLOB Container SAS Token: " + dataDto.BlobSasToken);
                Console.WriteLine("Queue URL: " + dataDto.QueueUrl);
                Console.WriteLine("Queue SAS Token: " + dataDto.QueueSasToken);

                // Load file to BLOB Storage
                Console.WriteLine("Create or overwrite the blob with contents from a local file...");
                var container = new CloudBlobContainer(new Uri(dataDto.BlobContainerUrl + dataDto.BlobSasToken));
                CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileInfo.Name);
                using (var fileStream = File.OpenRead(fileInfo.FullName))
                {
                    blockBlob.UploadFromStream(fileStream);
                }

                Console.WriteLine("done.");

                // Add message to queue
                Console.Write("Add a new message to the queue...");
                var queue = new CloudQueue(new Uri(dataDto.QueueUrl + dataDto.QueueSasToken));
                var message = new CloudQueueMessage(blockBlob.Uri.ToString());
                queue.AddMessage(message);
                Console.WriteLine("done.");

                Console.WriteLine("Press Enter to upload the next file or type 'quit' for exit");
                command = Console.ReadLine();
            }
            while (command != "quit");
        }
        /// <summary>
        /// Upload a sample file to the container
        /// </summary>
        /// <param name="blobClient">The blob client used to connect to storage</param>
        /// <param name="container">The container that the file will be uploaded to</param>
        /// <param name="fileToUpload">The file to upload</param>
        static void UploadFile(CloudBlobClient blobClient, CloudBlobContainer container, string fileToUpload)
        {
            CloudBlockBlob blockBlob = container.GetBlockBlobReference("sample.log");

            // Create or overwrite the "myblob" blob with contents from a local file.
            using (var fileStream = System.IO.File.OpenRead(fileToUpload))
            {
                blockBlob.UploadFromStream(fileStream);
            }
        }
Esempio n. 20
0
        //Delete a blob file
        public void DeleteBlob(string UserId, string BlobName)
        {
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = GetCloudBlobContainer(UserId);

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobName);
            if (blockBlob.Exists())
            {
                blockBlob.Delete();
            }
        }
 public static void UploadFile(CloudBlobContainer blobContainer, string blobName, string filePath, string contentType = null)
 {
     CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(blobName);
       using (var fileStream = File.OpenRead(filePath))
       {
     if (!String.IsNullOrEmpty(contentType)) { blockBlob.Properties.ContentType = contentType; }
     blockBlob.UploadFromStream(fileStream);
       }
       Console.WriteLine("URL:{0}, ETag:{1}", blockBlob.Uri, blockBlob.Properties.ETag);
 }
Esempio n. 22
0
        public AuthHelpers(IAppConfig appConfig)
        {
            _appConfig = appConfig;

            storageAccount = CloudStorageAccount.Parse(appConfig.StorageConnectionString);
            blobClient = storageAccount.CreateCloudBlobClient();
            container = blobClient.GetContainerReference("apipersistenace");
            container.CreateIfNotExists();
            Blob = container.GetBlockBlobReference("tenantId");
        }
        public static void GetBlobMetadata(CloudBlobContainer blobContainer, string blobName)
        {
            CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(blobName);
              blockBlob.FetchAttributes();

              Console.WriteLine("Blob metadata for: {0}", blockBlob.Name);
              Console.WriteLine("{0,-14}:{1}", "License", blockBlob.Metadata.ContainsKey("License") ? blockBlob.Metadata["License"] : "-");
              Console.WriteLine("{0,-14}:{1}", "URL", blockBlob.Metadata.ContainsKey("URL") ? blockBlob.Metadata["URL"] : "-");
              Console.WriteLine();
        }
Esempio n. 24
0
        public string DownloadPublicBlob(string UserId, string BlobName, bool PublicAccess = true)
        {
            //Retrieve a reference to a container.
            //Retrieve storage account from connection string.
            Microsoft.WindowsAzure.Storage.CloudStorageAccount StorageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(Convert.ToString(ConfigurationManager.AppSettings["StorageConnectionString"]));

            // Create the blob client.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference(UserId);

            // Create the container if it doesn't exist.
            container.CreateIfNotExists();


            //Set permission to public
            if (PublicAccess)
            {
                container.SetPermissions(
                    new Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions
                {
                    PublicAccess =
                        Microsoft.WindowsAzure.Storage.Blob.BlobContainerPublicAccessType.Blob
                });
            }
            else
            {
                container.SetPermissions(
                    new Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions
                {
                    PublicAccess =
                        Microsoft.WindowsAzure.Storage.Blob.BlobContainerPublicAccessType.Off
                });
            }



            // Retrieve reference to a blob named

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobName);

            //var sasToken = blockBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
            //{
            //    Permissions = SharedAccessBlobPermissions.Read,
            //    SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10),//assuming the blob can be downloaded in 10 miinutes
            //}, new SharedAccessBlobHeaders()
            //{
            //    ContentDisposition = "attachment; filename=file-name"
            //});

            //return string.Format("{0}{1}", blockBlob.Uri, sasToken);

            return(blockBlob.Uri.ToString());
        }
Esempio n. 25
0
        // Upload a Blob File
        public void UploadBlob(string UserId, string BlobName, HttpPostedFileBase image, int timeout = 60)
        {
            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = GetCloudBlobContainer(UserId);

            // Retrieve reference to a blob named
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobName);

            blockBlob.Properties.ContentType = image.ContentType;
            blockBlob.UploadFromStream(image.InputStream);
        }
        public static void DownloadBlob(CloudBlobContainer blobContainer, string blobName, string filePath)
        {
            CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(blobName);
              using (var fileStream = File.OpenWrite(filePath))
              {
            blockBlob.DownloadToStream(fileStream);
              }

              var fileInfo = new FileInfo(filePath);
              Console.WriteLine("Name:{0}, File Last Modified(UTC):{1}", fileInfo.FullName, fileInfo.LastWriteTimeUtc);
        }
Esempio n. 27
0
        //Upload a Blob File
        public void UploadByteBlob(string UserId, string BlobName, string ContentType, byte[] image)
        {
            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = GetCloudBlobContainer(UserId);

            // Retrieve reference to a blob named
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobName);

            blockBlob.Properties.ContentType = ContentType;
            blockBlob.UploadFromByteArray(image, 0, image.Length);
        }
Esempio n. 28
0
        /// <summary>
        /// Returns the Url of the given File object
        /// </summary>
        public static string GetImageUrl(Models.File file)
        {
            _container = _blobClient.GetContainerReference(file.Container);
            _container.CreateIfNotExists();
            _container.SetPermissions(
                new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });

            return _container.GetBlockBlobReference(file.Key).Uri.ToString();
        }
		private async Task<Uri> SaveToAzure(byte[] stream, bool isPhoto, string containerUrl) 
		{
			var creds = await GetStorageCredentials (isPhoto);

			var container = new CloudBlobContainer (new Uri (containerUrl), creds);

			var blockBlob = container.GetBlockBlobReference (Guid.NewGuid ().ToString ());

			await blockBlob.UploadFromByteArrayAsync (stream, 0, stream.Length);

			return blockBlob.Uri;
		}
 /// <summary>
 /// Save the data table to the given azure blob. This will overwrite an existing blob.
 /// </summary>
 /// <param name="table">instance of table to save</param>
 /// <param name="container">conatiner</param>
 /// <param name="blobName">blob name</param>
 public static void SaveToAzureBlob(this DataTable table, CloudBlobContainer container, string blobName)
 {
     var blob = container.GetBlockBlobReference(blobName);
     using (var stream = new MemoryStream())
     using (TextWriter writer = new StreamWriter(stream))
     {
         table.SaveToStream(writer);
         writer.Flush();
         stream.Seek(0, SeekOrigin.Begin);
         blob.UploadFromStream(stream);
     }
 }
Esempio n. 31
0
        //TODO: Move to utilities
        /// <summary>
        /// Synchronous rename of the specified blob to a new name, within the same container.
        /// </summary>
        /// <remarks>
        /// Note that Azure blobs do not really have a rename operation so the 
        /// implemenation has to be to copy the blob and then delete the original.
        /// </remarks>
        /// <param name="container">The container of the target and destination</param>
        /// <param name="sourcePath">The portion of the blob name within the container. 
        /// e.g. /Incoming/fred.mp4</param>
        /// <param name="targetPath">The path portion of the target blob within the container.
        /// e.g., /Processing/fred.mp4</param>
        /// <returns>true if the operation succeeds.</returns>
        public static bool RenameBlobWithinContainer(CloudBlobContainer container, string sourcePath, string targetPath)
        {
            // CloudBlobContainer container = blobClient.GetContainerReference(blobContainer);
            //TODO: block is okay since WAMS only supports block
            CloudBlockBlob newBlob = container.GetBlockBlobReference(targetPath);
            CloudBlockBlob sourceBlob = container.GetBlockBlobReference(sourcePath);
            var monitorStr = newBlob.StartCopyFromBlob(sourceBlob);

            CopyStatus cs = CopyStatus.Pending;
            while (cs == CopyStatus.Pending)
            {
                Thread.Sleep(2000);  // sleep for 2 seconds
                cs = newBlob.CopyState.Status;
            }

            if (cs == CopyStatus.Success)
            {
                sourceBlob.DeleteIfExists();
            }
            return true;
        }
        private void LogError(Exception ex, string message)
        {
            //ConfigurationManager.ConnectionStrings["CineStorageConStr"].ConnectionString
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("CineStorageConStr"));

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference("data");

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob logBlob = container.GetBlockBlobReference(String.Format(this.LogFile, DateTime.UtcNow.ToString("dd MMM yyyy")));
            try
            {
                using (StreamReader sr = new StreamReader(logBlob.OpenRead()))
                {
                    using (StreamWriter sw = new StreamWriter(logBlob.OpenWrite()))
                    {
                        sw.Write(sr.ReadToEnd());

                        if (ex != null)
                        {
                            sw.Write(System.Environment.NewLine);

                            sw.WriteLine(ex.Message);
                            sw.WriteLine(ex.StackTrace);

                            sw.Write(System.Environment.NewLine);

                            if (ex.InnerException != null)
                            {
                                sw.Write(System.Environment.NewLine);

                                sw.WriteLine(ex.InnerException.Message);
                                sw.WriteLine(ex.InnerException.StackTrace);

                                sw.Write(System.Environment.NewLine);
                            }
                        }

                        if (message != null)
                        {
                            sw.Write(System.Environment.NewLine);

                            sw.WriteLine(message);

                            sw.Write(System.Environment.NewLine);
                        }
                    }
                }
            }
            catch { }
        }
 public static StorageBlob.ICloudBlob GetBlob(StorageBlob.CloudBlobContainer container, string blobName, StorageType blobType)
 {
     StorageBlob.ICloudBlob blob = null;
     if (blobType == StorageType.BlockBlob)
     {
         blob = container.GetBlockBlobReference(blobName);
     }
     else
     {
         blob = container.GetPageBlobReference(blobName);
     }
     return(blob);
 }
Esempio n. 34
0
        public static string GetImageUrl(Models.File file, string size)
        {
            var key = file.Key.Split('.').First();
            var extension = file.Key.Split('.').Last();
            var keyResized = string.Format("{0}_{1}.{2}", key, size.Split('/').First(), extension);

            _container = _blobClient.GetContainerReference(file.Container);
            _container.CreateIfNotExists();
            _container.SetPermissions(
                new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });

            if (_container.GetBlockBlobReference(keyResized)
                .Exists())
            {
                return _container.GetBlockBlobReference(keyResized).Uri.ToString();
            }

            return _container.GetBlockBlobReference(file.Key).Uri.ToString();
        }
        public static void GetBlobProperties(CloudBlobContainer blobContainer, string blobName)
        {
            CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(blobName);
              blockBlob.FetchAttributes();

              Console.WriteLine("Blob properties for: {0}", blockBlob.Name);
              Console.WriteLine("{0,-14}:{1}", "BlobType", blockBlob.Properties.BlobType);
              Console.WriteLine("{0,-14}:{1}", "ETag", blockBlob.Properties.ETag.Replace(@"""",""));
              Console.WriteLine("{0,-14}:{1}", "ContentType", blockBlob.Properties.ContentType);
              Console.WriteLine("{0,-14}:{1}", "Length", blockBlob.Properties.Length);
              Console.WriteLine("{0,-14}:{1}", "LastModified", blockBlob.Properties.LastModified);
              Console.WriteLine();
        }
Esempio n. 36
0
        private async Task ProcessJob(CloudBlockBlob jobBlob, CloudBlobContainer container)
        {
            var name = await jobBlob.DownloadTextAsync();
            var result = Compliments.GetRandomCompliment(name);

            string resultBlobName = jobBlob.Name.Replace("job/", "result/");

            CloudBlockBlob resultBlob = container.GetBlockBlobReference(resultBlobName);
            await resultBlob.UploadTextAsync(result);

            jobBlob.Metadata["Status"] = "finished";
            await jobBlob.SetMetadataAsync();
        }
Esempio n. 37
0
 static void createMetadataBlob(CloudBlobContainer container, string filename)
 {
     CloudBlockBlob blob = container.GetBlockBlobReference(filename);
     //agregamos metadatos
     blob.Metadata.Add("Autor", "PinkFloyd");
     blob.Metadata.Add("Imagen", "HD");
     blob.Metadata.Add("Subidopor", "LeonardoC");
     blob.Metadata.Add("Comentario", "Mi wallpaper");
     using (Stream file = System.IO.File.OpenRead(@"C:\Users\slave\Pictures\pinkwall.jpg"))
     {
         blob.UploadFromStream(file);
     }
 }
        /// <summary>
        /// create a block blob with random properties and metadata
        /// </summary>
        /// <param name="container">CloudBlobContainer object</param>
        /// <param name="blobName">Block blob name</param>
        /// <returns>ICloudBlob object</returns>
        public StorageBlob.ICloudBlob CreateBlockBlob(StorageBlob.CloudBlobContainer container, string blobName)
        {
            StorageBlob.CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);

            int    maxBlobSize = 1024 * 1024;
            string md5sum      = string.Empty;
            int    blobSize    = random.Next(maxBlobSize);

            byte[] buffer = new byte[blobSize];
            using (MemoryStream ms = new MemoryStream(buffer))
            {
                random.NextBytes(buffer);
                //ms.Read(buffer, 0, buffer.Length);
                blockBlob.UploadFromStream(ms);
                md5sum = Convert.ToBase64String(Helper.GetMD5(buffer));
            }

            blockBlob.Properties.ContentMD5 = md5sum;
            GenerateBlobPropertiesAndMetaData(blockBlob);
            Test.Info(string.Format("create block blob '{0}' in container '{1}'", blobName, container.Name));
            return(blockBlob);
        }
Esempio n. 39
0
        // Get url of blob
        public string DownloadBlob(string UserId, string BlobName)
        {
            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = GetCloudBlobContainer(UserId);

            // Retrieve reference to a blob named

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobName);

            //var sasToken = blockBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
            //{
            //    Permissions = SharedAccessBlobPermissions.Read,
            //    SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10),//assuming the blob can be downloaded in 10 miinutes
            //}, new SharedAccessBlobHeaders()
            //{
            //    ContentDisposition = "attachment; filename=file-name"
            //});

            //return string.Format("{0}{1}", blockBlob.Uri, sasToken);

            return(blockBlob.Uri.ToString());
        }
        private DateTime GetLastModified()
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("CineStorageConStr"));

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference("data");

            // Retrieve reference to a blob.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob cinemasUKBlob = container.GetBlockBlobReference(CinemasUKFileName);
            if (cinemasUKBlob.Exists())
            {
                DateTimeOffset?dto = cinemasUKBlob.Properties.LastModified;
                if (dto.HasValue)
                {
                    return(dto.Value.DateTime);
                }
            }

            return(DateTime.MinValue);
        }
Esempio n. 41
0
        public string DownloadSharedBlob(string UserId, string BlobName)
        {
            //Retrieve storage account from connection string.
            Microsoft.WindowsAzure.Storage.CloudStorageAccount StorageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(Convert.ToString(ConfigurationManager.AppSettings["StorageConnectionString"]));

            // Create the blob client.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference(UserId);

            var blob     = container.GetBlockBlobReference(BlobName);
            var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
            {
                Permissions            = SharedAccessBlobPermissions.Read,
                SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(15),
            }, new SharedAccessBlobHeaders()
            {
                ContentDisposition = "attachment; filename=\"somefile.pdf\"",
            });
            var downloadUrl = string.Format("{0}{1}", blob.Uri.AbsoluteUri, sasToken);

            return(downloadUrl);
        }
Esempio n. 42
0
        public async Task CloudBlockBlobCopyTestAsync()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                await container.CreateAsync();

                CloudBlockBlob source = container.GetBlockBlobReference("source");

                string data = "String data";
                await UploadTextAsync(source, data, Encoding.UTF8);

                source.Metadata["Test"] = "value";
                await source.SetMetadataAsync();

                CloudBlockBlob copy   = container.GetBlockBlobReference("copy");
                string         copyId = await copy.StartCopyAsync(TestHelper.Defiddler(source));
                await WaitForCopyAsync(copy);

                Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status);
                Assert.AreEqual(source.Uri.AbsolutePath, copy.CopyState.Source.AbsolutePath);
                Assert.AreEqual(data.Length, copy.CopyState.TotalBytes);
                Assert.AreEqual(data.Length, copy.CopyState.BytesCopied);
                Assert.AreEqual(copyId, copy.CopyState.CopyId);
                Assert.IsTrue(copy.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                OperationContext opContext = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(
                    async() => await copy.AbortCopyAsync(copyId, null, null, opContext),
                    opContext,
                    "Aborting a copy operation after completion should fail",
                    HttpStatusCode.Conflict,
                    "NoPendingCopyOperation");

                await source.FetchAttributesAsync();

                Assert.IsNotNull(copy.Properties.ETag);
                Assert.AreNotEqual(source.Properties.ETag, copy.Properties.ETag);
                Assert.IsTrue(copy.Properties.LastModified > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)));

                string copyData = await DownloadTextAsync(copy, Encoding.UTF8);

                Assert.AreEqual(data, copyData, "Data inside copy of blob not similar");

                await copy.FetchAttributesAsync();

                BlobProperties prop1 = copy.Properties;
                BlobProperties prop2 = source.Properties;

                Assert.AreEqual(prop1.CacheControl, prop2.CacheControl);
                Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding);
                Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage);
                Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5);
                Assert.AreEqual(prop1.ContentType, prop2.ContentType);

                Assert.AreEqual("value", copy.Metadata["Test"], false, "Copied metadata not same");

                await copy.DeleteAsync();
            }
            finally
            {
                container.DeleteIfExistsAsync().Wait();
            }
        }
Esempio n. 43
0
        public async Task CloudBlockBlobCopyFromSnapshotTestAsync()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                await container.CreateAsync();

                CloudBlockBlob source = container.GetBlockBlobReference("source");

                string data = "String data";
                await UploadTextAsync(source, data, Encoding.UTF8);

                source.Metadata["Test"] = "value";
                await source.SetMetadataAsync();

                CloudBlockBlob snapshot = await source.CreateSnapshotAsync();

                //Modify source
                string newData = "Hello";
                source.Metadata["Test"] = "newvalue";
                await source.SetMetadataAsync();

                source.Properties.ContentMD5 = null;
                await UploadTextAsync(source, newData, Encoding.UTF8);

                Assert.AreEqual(newData, await DownloadTextAsync(source, Encoding.UTF8), "Source is modified correctly");
                Assert.AreEqual(data, await DownloadTextAsync(snapshot, Encoding.UTF8), "Modifying source blob should not modify snapshot");

                await source.FetchAttributesAsync();

                await snapshot.FetchAttributesAsync();

                Assert.AreNotEqual(source.Metadata["Test"], snapshot.Metadata["Test"], "Source and snapshot metadata should be independent");

                CloudBlockBlob copy = container.GetBlockBlobReference("copy");
                await copy.StartCopyAsync(TestHelper.Defiddler(snapshot));
                await WaitForCopyAsync(copy);

                Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status);
                Assert.AreEqual(data, await DownloadTextAsync(copy, Encoding.UTF8), "Data inside copy of blob not similar");

                await copy.FetchAttributesAsync();

                BlobProperties prop1 = copy.Properties;
                BlobProperties prop2 = snapshot.Properties;

                Assert.AreEqual(prop1.CacheControl, prop2.CacheControl);
                Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding);
                Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage);
                Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5);
                Assert.AreEqual(prop1.ContentType, prop2.ContentType);

                Assert.AreEqual("value", copy.Metadata["Test"], false, "Copied metadata not same");

                await copy.DeleteAsync();
            }
            finally
            {
                container.DeleteIfExistsAsync().Wait();
            }
        }
Esempio n. 44
0
        public void DisableContentMD5ValidationTest()
        {
            byte[] buffer = new byte[1024];
            Random random = new Random();

            random.NextBytes(buffer);

            BlobRequestOptions optionsWithNoMD5 = new BlobRequestOptions()
            {
                DisableContentMD5Validation = true,
                StoreBlobContentMD5         = true,
            };
            BlobRequestOptions optionsWithMD5 = new BlobRequestOptions()
            {
                DisableContentMD5Validation = false,
                StoreBlobContentMD5         = true,
            };

            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1");
                using (Stream stream = new NonSeekableMemoryStream(buffer))
                {
                    blockBlob.UploadFromStream(stream, null, optionsWithMD5);
                }

                using (Stream stream = new MemoryStream())
                {
                    blockBlob.DownloadToStream(stream, null, optionsWithMD5);
                    blockBlob.DownloadToStream(stream, null, optionsWithNoMD5);

                    using (Stream blobStream = blockBlob.OpenRead(null, optionsWithMD5))
                    {
                        int read;
                        do
                        {
                            read = blobStream.Read(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

                    using (Stream blobStream = blockBlob.OpenRead(null, optionsWithNoMD5))
                    {
                        int read;
                        do
                        {
                            read = blobStream.Read(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

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

                    TestHelper.ExpectedException(
                        () => blockBlob.DownloadToStream(stream, null, optionsWithMD5),
                        "Downloading a blob with invalid MD5 should fail",
                        HttpStatusCode.OK);
                    blockBlob.DownloadToStream(stream, null, optionsWithNoMD5);

                    using (Stream blobStream = blockBlob.OpenRead(null, optionsWithMD5))
                    {
                        TestHelper.ExpectedException <IOException>(
                            () =>
                        {
                            int read;
                            do
                            {
                                read = blobStream.Read(buffer, 0, buffer.Length);
                            }while (read > 0);
                        },
                            "Downloading a blob with invalid MD5 should fail");
                    }

                    using (Stream blobStream = blockBlob.OpenRead(null, optionsWithNoMD5))
                    {
                        int read;
                        do
                        {
                            read = blobStream.Read(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }
                }

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

                using (Stream stream = new MemoryStream())
                {
                    pageBlob.DownloadToStream(stream, null, optionsWithMD5);
                    pageBlob.DownloadToStream(stream, null, optionsWithNoMD5);

                    using (Stream blobStream = pageBlob.OpenRead(null, optionsWithMD5))
                    {
                        int read;
                        do
                        {
                            read = blobStream.Read(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

                    using (Stream blobStream = pageBlob.OpenRead(null, optionsWithNoMD5))
                    {
                        int read;
                        do
                        {
                            read = blobStream.Read(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

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

                    TestHelper.ExpectedException(
                        () => pageBlob.DownloadToStream(stream, null, optionsWithMD5),
                        "Downloading a blob with invalid MD5 should fail",
                        HttpStatusCode.OK);
                    pageBlob.DownloadToStream(stream, null, optionsWithNoMD5);

                    using (Stream blobStream = pageBlob.OpenRead(null, optionsWithMD5))
                    {
                        TestHelper.ExpectedException <IOException>(
                            () =>
                        {
                            int read;
                            do
                            {
                                read = blobStream.Read(buffer, 0, buffer.Length);
                            }while (read > 0);
                        },
                            "Downloading a blob with invalid MD5 should fail");
                    }

                    using (Stream blobStream = pageBlob.OpenRead(null, optionsWithNoMD5))
                    {
                        int read;
                        do
                        {
                            read = blobStream.Read(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Esempio n. 45
0
        public async Task DisableContentMD5ValidationTestAsync()
        {
            byte[] buffer = new byte[1024];
            Random random = new Random();

            random.NextBytes(buffer);

            BlobRequestOptions optionsWithNoMD5 = new BlobRequestOptions()
            {
                DisableContentMD5Validation = true,
                StoreBlobContentMD5         = true,
            };
            BlobRequestOptions optionsWithMD5 = new BlobRequestOptions()
            {
                DisableContentMD5Validation = false,
                StoreBlobContentMD5         = true,
            };

            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                await container.CreateAsync();

                CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1");
                using (Stream stream = new NonSeekableMemoryStream(buffer))
                {
                    await blockBlob.UploadFromStreamAsync(stream.AsInputStream(), null, optionsWithMD5, null);
                }

                using (Stream stream = new MemoryStream())
                {
                    await blockBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithMD5, null);

                    await blockBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithNoMD5, null);

                    using (var blobStream = await blockBlob.OpenReadAsync(null, optionsWithMD5, null))
                    {
                        Stream blobStreamForRead = blobStream.AsStreamForRead();
                        int    read;
                        do
                        {
                            read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

                    using (var blobStream = await blockBlob.OpenReadAsync(null, optionsWithNoMD5, null))
                    {
                        Stream blobStreamForRead = blobStream.AsStreamForRead();
                        int    read;
                        do
                        {
                            read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

                    blockBlob.Properties.ContentMD5 = "MDAwMDAwMDA=";
                    await blockBlob.SetPropertiesAsync();

                    OperationContext opContext = new OperationContext();
                    await TestHelper.ExpectedExceptionAsync(
                        async() => await blockBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithMD5, opContext),
                        opContext,
                        "Downloading a blob with invalid MD5 should fail",
                        HttpStatusCode.OK);

                    await blockBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithNoMD5, null);

                    using (var blobStream = await blockBlob.OpenReadAsync(null, optionsWithMD5, null))
                    {
                        Stream blobStreamForRead = blobStream.AsStreamForRead();
                        TestHelper.ExpectedException <IOException>(
                            () =>
                        {
                            int read;
                            do
                            {
                                read = blobStreamForRead.Read(buffer, 0, buffer.Length);
                            }while (read > 0);
                        },
                            "Downloading a blob with invalid MD5 should fail");
                    }

                    using (var blobStream = await blockBlob.OpenReadAsync(null, optionsWithNoMD5, null))
                    {
                        Stream blobStreamForRead = blobStream.AsStreamForRead();
                        int    read;
                        do
                        {
                            read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }
                }

                CloudPageBlob pageBlob = container.GetPageBlobReference("blob2");
                using (Stream stream = new MemoryStream(buffer))
                {
                    await pageBlob.UploadFromStreamAsync(stream.AsInputStream(), null, optionsWithMD5, null);
                }

                using (Stream stream = new MemoryStream())
                {
                    await pageBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithMD5, null);

                    await pageBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithNoMD5, null);

                    using (var blobStream = await pageBlob.OpenReadAsync(null, optionsWithMD5, null))
                    {
                        Stream blobStreamForRead = blobStream.AsStreamForRead();
                        int    read;
                        do
                        {
                            read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

                    using (var blobStream = await pageBlob.OpenReadAsync(null, optionsWithNoMD5, null))
                    {
                        Stream blobStreamForRead = blobStream.AsStreamForRead();
                        int    read;
                        do
                        {
                            read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

                    pageBlob.Properties.ContentMD5 = "MDAwMDAwMDA=";
                    await pageBlob.SetPropertiesAsync();

                    OperationContext opContext = new OperationContext();
                    await TestHelper.ExpectedExceptionAsync(
                        async() => await pageBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithMD5, opContext),
                        opContext,
                        "Downloading a blob with invalid MD5 should fail",
                        HttpStatusCode.OK);

                    await pageBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithNoMD5, null);

                    using (var blobStream = await pageBlob.OpenReadAsync(null, optionsWithMD5, null))
                    {
                        Stream blobStreamForRead = blobStream.AsStreamForRead();
                        TestHelper.ExpectedException <IOException>(
                            () =>
                        {
                            int read;
                            do
                            {
                                read = blobStreamForRead.Read(buffer, 0, buffer.Length);
                            }while (read > 0);
                        },
                            "Downloading a blob with invalid MD5 should fail");
                    }

                    using (var blobStream = await pageBlob.OpenReadAsync(null, optionsWithNoMD5, null))
                    {
                        Stream blobStreamForRead = blobStream.AsStreamForRead();
                        int    read;
                        do
                        {
                            read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }
                }
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
Esempio n. 46
0
        public void StoreBlobContentMD5Test()
        {
            BlobRequestOptions optionsWithNoMD5 = new BlobRequestOptions()
            {
                StoreBlobContentMD5 = false,
            };
            BlobRequestOptions optionsWithMD5 = new BlobRequestOptions()
            {
                StoreBlobContentMD5 = true,
            };

            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                ICloudBlob blob = container.GetBlockBlobReference("blob1");
                using (Stream stream = new NonSeekableMemoryStream())
                {
                    blob.UploadFromStream(stream, null, optionsWithMD5);
                }
                blob.FetchAttributes();
                Assert.IsNotNull(blob.Properties.ContentMD5);

                blob = container.GetBlockBlobReference("blob2");
                using (Stream stream = new NonSeekableMemoryStream())
                {
                    blob.UploadFromStream(stream, null, optionsWithNoMD5);
                }
                blob.FetchAttributes();
                Assert.IsNull(blob.Properties.ContentMD5);

                blob = container.GetBlockBlobReference("blob3");
                using (Stream stream = new NonSeekableMemoryStream())
                {
                    blob.UploadFromStream(stream);
                }
                blob.FetchAttributes();
                Assert.IsNotNull(blob.Properties.ContentMD5);

                blob = container.GetPageBlobReference("blob4");
                using (Stream stream = new MemoryStream())
                {
                    blob.UploadFromStream(stream, null, optionsWithMD5);
                }
                blob.FetchAttributes();
                Assert.IsNotNull(blob.Properties.ContentMD5);

                blob = container.GetPageBlobReference("blob5");
                using (Stream stream = new MemoryStream())
                {
                    blob.UploadFromStream(stream, null, optionsWithNoMD5);
                }
                blob.FetchAttributes();
                Assert.IsNull(blob.Properties.ContentMD5);

                blob = container.GetPageBlobReference("blob6");
                using (Stream stream = new MemoryStream())
                {
                    blob.UploadFromStream(stream);
                }
                blob.FetchAttributes();
                Assert.IsNull(blob.Properties.ContentMD5);
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Esempio n. 47
0
        private static void TestAccess(string sasToken, SharedAccessBlobPermissions permissions, SharedAccessBlobHeaders headers, CloudBlobContainer container, CloudBlob blob)
        {
            StorageCredentials credentials = string.IsNullOrEmpty(sasToken) ?
                                             new StorageCredentials() :
                                             new StorageCredentials(sasToken);

            if (container != null)
            {
                container = new CloudBlobContainer(credentials.TransformUri(container.Uri));
                if (blob.BlobType == BlobType.BlockBlob)
                {
                    blob = container.GetBlockBlobReference(blob.Name);
                }
                else if (blob.BlobType == BlobType.PageBlob)
                {
                    blob = container.GetPageBlobReference(blob.Name);
                }
                else
                {
                    blob = container.GetAppendBlobReference(blob.Name);
                }
            }
            else
            {
                if (blob.BlobType == BlobType.BlockBlob)
                {
                    blob = new CloudBlockBlob(credentials.TransformUri(blob.Uri));
                }
                else if (blob.BlobType == BlobType.PageBlob)
                {
                    blob = new CloudPageBlob(credentials.TransformUri(blob.Uri));
                }
                else
                {
                    blob = new CloudAppendBlob(credentials.TransformUri(blob.Uri));
                }
            }

            if (container != null)
            {
                if ((permissions & SharedAccessBlobPermissions.List) == SharedAccessBlobPermissions.List)
                {
                    container.ListBlobs().ToArray();
                }
                else
                {
                    TestHelper.ExpectedException(
                        () => container.ListBlobs().ToArray(),
                        "List blobs while SAS does not allow for listing",
                        HttpStatusCode.NotFound);
                }
            }

            if ((permissions & SharedAccessBlobPermissions.Read) == SharedAccessBlobPermissions.Read)
            {
                blob.FetchAttributes();

                // Test headers
                if (headers != null)
                {
                    if (headers.CacheControl != null)
                    {
                        Assert.AreEqual(headers.CacheControl, blob.Properties.CacheControl);
                    }

                    if (headers.ContentDisposition != null)
                    {
                        Assert.AreEqual(headers.ContentDisposition, blob.Properties.ContentDisposition);
                    }

                    if (headers.ContentEncoding != null)
                    {
                        Assert.AreEqual(headers.ContentEncoding, blob.Properties.ContentEncoding);
                    }

                    if (headers.ContentLanguage != null)
                    {
                        Assert.AreEqual(headers.ContentLanguage, blob.Properties.ContentLanguage);
                    }

                    if (headers.ContentType != null)
                    {
                        Assert.AreEqual(headers.ContentType, blob.Properties.ContentType);
                    }
                }
            }
            else
            {
                TestHelper.ExpectedException(
                    () => blob.FetchAttributes(),
                    "Fetch blob attributes while SAS does not allow for reading",
                    HttpStatusCode.NotFound);
            }

            if ((permissions & SharedAccessBlobPermissions.Write) == SharedAccessBlobPermissions.Write)
            {
                blob.SetMetadata();
            }
            else
            {
                TestHelper.ExpectedException(
                    () => blob.SetMetadata(),
                    "Set blob metadata while SAS does not allow for writing",
                    HttpStatusCode.NotFound);
            }

            if ((permissions & SharedAccessBlobPermissions.Delete) == SharedAccessBlobPermissions.Delete)
            {
                blob.Delete();
            }
            else
            {
                TestHelper.ExpectedException(
                    () => blob.Delete(),
                    "Delete blob while SAS does not allow for deleting",
                    HttpStatusCode.NotFound);
            }
        }
Esempio n. 48
0
        // Create a CSV file and save to Blob storage with the Headers required for our Azure Function processing
        // A new request telemetry is created
        // The request is part of the parent request (since)
        public void CreateBlob(string fileName)
        {
            ///////////////////////////////////////////////////
            // Grab existing 
            ///////////////////////////////////////////////////
            Microsoft.ApplicationInsights.TelemetryClient telemetryClient = new Microsoft.ApplicationInsights.TelemetryClient();
            telemetryClient.Context.User.AuthenticatedUserId = "*****@*****.**";

            string traceoperation = null;
            string traceparent = null;
            System.Console.WriteLine("telemetryClient.Context.Operation.Id: " + telemetryClient.Context.Operation.Id);
            System.Console.WriteLine("telemetryClient.Context.Session.Id: " + telemetryClient.Context.Session.Id);
            System.Console.WriteLine("telemetryClient.Context.Operation.ParentId: " + telemetryClient.Context.Operation.ParentId);

            Microsoft.ApplicationInsights.DataContracts.RequestTelemetry requestTelemetry = new Microsoft.ApplicationInsights.DataContracts.RequestTelemetry();
            requestTelemetry.Name = "Create Blob: " + fileName;
            //requestTelemetry.Source = requestContext.Replace("appId=",string.Empty);
            requestTelemetry.Timestamp = System.DateTimeOffset.Now;
            requestTelemetry.Context.Operation.Id = traceoperation;
            requestTelemetry.Context.Operation.ParentId = traceparent;
            requestTelemetry.Context.User.AuthenticatedUserId = "*****@*****.**";

            using (var requestBlock = telemetryClient.StartOperation<Microsoft.ApplicationInsights.DataContracts.RequestTelemetry>(requestTelemetry))
            {
                ///////////////////////////////////////////////////
                // Request Telemetry
                ///////////////////////////////////////////////////
                requestBlock.Telemetry.Context.User.AuthenticatedUserId = "*****@*****.**";

                if (!string.IsNullOrWhiteSpace(traceoperation))
                {
                    // Use the existing common operation id
                    requestBlock.Telemetry.Context.Operation.Id = traceoperation;
                    System.Console.WriteLine("[Use existing] traceoperation: " + traceoperation);
                }
                else
                {
                    // Set the traceoperation (we did not know it until now)
                    traceoperation = requestBlock.Telemetry.Context.Operation.Id;
                    System.Console.WriteLine("[Set the] traceoperation = requestBlock.Telemetry.Context.Operation.Id: " + traceoperation);
                }

                if (!string.IsNullOrWhiteSpace(traceparent))
                {
                    // Use the existing traceparent
                    requestBlock.Telemetry.Context.Operation.ParentId = traceparent;
                    System.Console.WriteLine("[Use existing] traceparent: " + traceparent);
                }
                else
                {
                    traceparent = requestBlock.Telemetry.Id;
                    System.Console.WriteLine("[Set the] traceparent = requestBlock.Telemetry.Id: " + traceparent);
                }
                // Store future parent id
                traceparent = requestBlock.Telemetry.Id;
                System.Console.WriteLine("traceparent = requestBlock.Telemetry.Id: " + traceparent);



                ///////////////////////////////////////////////////
                // Create Dependency for future Azure Function processing
                // NOTE: I trick it by giving a Start Time Offset of Now.AddSeconds(1), so it sorts correctly in the Azure Portal UI
                ///////////////////////////////////////////////////
                string operationName = "Dependency: Blob Event";
                // Set the target so it points to the "dependent" app insights account app id
                // string target = "03-disttrace-func-blob | cid-v1:676560d0-81fb-4e5b-bfdd-7da1ad11c866"
                string target = "03-disttrace-func-blob | cid-v1:" + System.Environment.GetEnvironmentVariable("ai_03_disttrace_web_app_appkey");
                string dependencyName = "Dependency Name: Azure Function Blob Trigger";
                Microsoft.ApplicationInsights.DataContracts.DependencyTelemetry dependencyTelemetry =
                   new Microsoft.ApplicationInsights.DataContracts.DependencyTelemetry(
                    operationName, target, dependencyName,
                    "02-disttrace-web-app", System.DateTimeOffset.Now.AddSeconds(1), System.TimeSpan.FromSeconds(2), "200", true);
                dependencyTelemetry.Context.Operation.Id = traceoperation;
                dependencyTelemetry.Context.Operation.ParentId = requestBlock.Telemetry.Id;
                // Store future parent id
                traceparent = dependencyTelemetry.Id;
                System.Console.WriteLine("traceparent = dependencyTelemetry.Id: " + traceparent);
                telemetryClient.TrackDependency(dependencyTelemetry);



                ///////////////////////////////////////////////////
                // Blob code
                ///////////////////////////////////////////////////
                string containerName = "appinsightstest";
                string storageConnectionString = System.Environment.GetEnvironmentVariable("ai_storage_key");
                CloudStorageAccount storageAccount = null;
                System.Console.WriteLine("storageConnectionString: " + storageConnectionString);

                CloudStorageAccount.TryParse(storageConnectionString, out storageAccount);

                System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>();

                list.Add("id,date");

                for (int i = 1; i <= 50000; i++)
                {
                    list.Add(i.ToString() + "," + string.Format("{0:MM/dd/yyyy}", System.DateTime.Now));
                }

                var text = string.Join("\n", list.ToArray());

                Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobStorage = storageAccount.CreateCloudBlobClient();
                blobStorage.DefaultRequestOptions.RetryPolicy = new Microsoft.WindowsAzure.Storage.RetryPolicies.LinearRetry(System.TimeSpan.FromSeconds(1), 10);
                Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobStorage.GetContainerReference(containerName);
                container.CreateIfNotExistsAsync().Wait();

                Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blob = container.GetBlockBlobReference(fileName);

                ///////////////////////////////////////////////////
                // Set the blob's meta data
                // We need the values from the dependency
                ///////////////////////////////////////////////////
                // Request-Context: appId=cid-v1:{The App Id of the current App Insights Account}
                string requestContext = "appId=cid-v1:" + System.Environment.GetEnvironmentVariable("ai_02_disttrace_web_app_appkey");
                System.Console.WriteLine("Blob Metadata -> requestContext: " + requestContext);
                blob.Metadata.Add("RequestContext", requestContext);

                // Request-Id / traceparent: {parent request/operation id} (e.g. the Track Dependency)
                System.Console.WriteLine("Blob Metadata -> RequestId: " + traceparent);
                blob.Metadata.Add("RequestId", traceparent);
                System.Console.WriteLine("Blob Metadata -> traceparent: " + traceparent);
                blob.Metadata.Add("traceparent", traceparent);

                // Traceoperation {common operation id} (e.g. same operation id for all requests in this telemetry pipeline)
                System.Console.WriteLine("Blob Metadata -> traceoperation: " + traceoperation);
                blob.Metadata.Add("traceoperation", traceoperation);


                using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(text)))
                {
                    blob.UploadFromStreamAsync(memoryStream).Wait();
                }

                requestTelemetry.ResponseCode = "200";
                requestTelemetry.Success = true;
                telemetryClient.StopOperation(requestBlock);
            } // using

            ///////////////////////////////////////////////////
            // For Debugging
            ///////////////////////////////////////////////////
            telemetryClient.Flush();

        } // Create Blob
Esempio n. 49
0
        public void UseTransactionalMD5GetTestAPM()
        {
            BlobRequestOptions optionsWithNoMD5 = new BlobRequestOptions()
            {
                UseTransactionalMD5 = false,
            };
            BlobRequestOptions optionsWithMD5 = new BlobRequestOptions()
            {
                UseTransactionalMD5 = true,
            };

            byte[] buffer = GetRandomBuffer(3 * 1024 * 1024);
            MD5    hasher = MD5.Create();
            string md5    = Convert.ToBase64String(hasher.ComputeHash(buffer));

            string           lastCheckMD5          = null;
            int              checkCount            = 0;
            OperationContext opContextWithMD5Check = new OperationContext();

            opContextWithMD5Check.ResponseReceived += (_, args) =>
            {
                if (args.Response.ContentLength >= buffer.Length)
                {
                    lastCheckMD5 = args.Response.Headers[HttpResponseHeader.ContentMd5];
                    checkCount++;
                }
            };

            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    IAsyncResult   result;
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1");
                    using (Stream blobStream = blockBlob.OpenWrite())
                    {
                        blobStream.Write(buffer, 0, buffer.Length);
                        blobStream.Write(buffer, 0, buffer.Length);
                    }

                    checkCount = 0;
                    using (Stream stream = new MemoryStream())
                    {
                        result = blockBlob.BeginDownloadToStream(stream, null, optionsWithNoMD5, opContextWithMD5Check,
                                                                 ar => waitHandle.Set(),
                                                                 null);
                        waitHandle.WaitOne();
                        blockBlob.EndDownloadRangeToStream(result);
                        Assert.IsNotNull(lastCheckMD5);

                        result = blockBlob.BeginDownloadToStream(stream, null, optionsWithMD5, opContextWithMD5Check,
                                                                 ar => waitHandle.Set(),
                                                                 null);
                        waitHandle.WaitOne();
                        blockBlob.EndDownloadRangeToStream(result);
                        Assert.IsNotNull(lastCheckMD5);

                        result = blockBlob.BeginDownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithNoMD5, opContextWithMD5Check,
                                                                      ar => waitHandle.Set(),
                                                                      null);
                        waitHandle.WaitOne();
                        blockBlob.EndDownloadRangeToStream(result);
                        Assert.IsNull(lastCheckMD5);

                        result = blockBlob.BeginDownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithMD5, opContextWithMD5Check,
                                                                      ar => waitHandle.Set(),
                                                                      null);
                        waitHandle.WaitOne();
                        blockBlob.EndDownloadRangeToStream(result);
                        Assert.AreEqual(md5, lastCheckMD5);

                        result = blockBlob.BeginDownloadRangeToStream(stream, 1024, 4 * 1024 * 1024 + 1, null, optionsWithNoMD5, opContextWithMD5Check,
                                                                      ar => waitHandle.Set(),
                                                                      null);
                        waitHandle.WaitOne();
                        blockBlob.EndDownloadRangeToStream(result);
                        Assert.IsNull(lastCheckMD5);

                        result = blockBlob.BeginDownloadRangeToStream(stream, 1024, 4 * 1024 * 1024 + 1, null, optionsWithMD5, opContextWithMD5Check,
                                                                      ar => waitHandle.Set(),
                                                                      null);
                        waitHandle.WaitOne();
                        StorageException storageEx = TestHelper.ExpectedException <StorageException>(
                            () => blockBlob.EndDownloadRangeToStream(result),
                            "Downloading more than 4MB with transactional MD5 should not be supported");
                        Assert.IsInstanceOfType(storageEx.InnerException, typeof(ArgumentOutOfRangeException));
                    }
                    Assert.AreEqual(5, checkCount);

                    CloudPageBlob pageBlob = container.GetPageBlobReference("blob2");
                    using (Stream blobStream = pageBlob.OpenWrite(buffer.Length * 2))
                    {
                        blobStream.Write(buffer, 0, buffer.Length);
                        blobStream.Write(buffer, 0, buffer.Length);
                    }

                    checkCount = 0;
                    using (Stream stream = new MemoryStream())
                    {
                        result = pageBlob.BeginDownloadToStream(stream, null, optionsWithNoMD5, opContextWithMD5Check,
                                                                ar => waitHandle.Set(),
                                                                null);
                        waitHandle.WaitOne();
                        pageBlob.EndDownloadRangeToStream(result);
                        Assert.IsNull(lastCheckMD5);

                        result = pageBlob.BeginDownloadToStream(stream, null, optionsWithMD5, opContextWithMD5Check,
                                                                ar => waitHandle.Set(),
                                                                null);
                        waitHandle.WaitOne();
                        StorageException storageEx = TestHelper.ExpectedException <StorageException>(
                            () => pageBlob.EndDownloadRangeToStream(result),
                            "Page blob will not have MD5 set by default; with UseTransactional, download should fail");

                        result = pageBlob.BeginDownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithNoMD5, opContextWithMD5Check,
                                                                     ar => waitHandle.Set(),
                                                                     null);
                        waitHandle.WaitOne();
                        pageBlob.EndDownloadRangeToStream(result);
                        Assert.IsNull(lastCheckMD5);

                        result = pageBlob.BeginDownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithMD5, opContextWithMD5Check,
                                                                     ar => waitHandle.Set(),
                                                                     null);
                        waitHandle.WaitOne();
                        pageBlob.EndDownloadRangeToStream(result);
                        Assert.AreEqual(md5, lastCheckMD5);

                        result = pageBlob.BeginDownloadRangeToStream(stream, 1024, 4 * 1024 * 1024 + 1, null, optionsWithNoMD5, opContextWithMD5Check,
                                                                     ar => waitHandle.Set(),
                                                                     null);
                        waitHandle.WaitOne();
                        pageBlob.EndDownloadRangeToStream(result);
                        Assert.IsNull(lastCheckMD5);

                        result = pageBlob.BeginDownloadRangeToStream(stream, 1024, 4 * 1024 * 1024 + 1, null, optionsWithMD5, opContextWithMD5Check,
                                                                     ar => waitHandle.Set(),
                                                                     null);
                        waitHandle.WaitOne();
                        storageEx = TestHelper.ExpectedException <StorageException>(
                            () => pageBlob.EndDownloadRangeToStream(result),
                            "Downloading more than 4MB with transactional MD5 should not be supported");
                        Assert.IsInstanceOfType(storageEx.InnerException, typeof(ArgumentOutOfRangeException));
                    }
                    Assert.AreEqual(5, checkCount);
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Esempio n. 50
0
        public void UseTransactionalMD5PutTestAPM()
        {
            BlobRequestOptions optionsWithNoMD5 = new BlobRequestOptions()
            {
                UseTransactionalMD5 = false,
            };
            BlobRequestOptions optionsWithMD5 = new BlobRequestOptions()
            {
                UseTransactionalMD5 = true,
            };

            byte[] buffer = GetRandomBuffer(1024);
            MD5    hasher = MD5.Create();
            string md5    = Convert.ToBase64String(hasher.ComputeHash(buffer));

            string           lastCheckMD5          = null;
            int              checkCount            = 0;
            OperationContext opContextWithMD5Check = new OperationContext();

            opContextWithMD5Check.SendingRequest += (_, args) =>
            {
                if (args.Request.ContentLength >= buffer.Length)
                {
                    lastCheckMD5 = args.Request.Headers[HttpRequestHeader.ContentMd5];
                    checkCount++;
                }
            };

            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    IAsyncResult   result;
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1");
                    List <string>  blockIds  = GetBlockIdList(3);
                    checkCount = 0;
                    using (Stream blockData = new MemoryStream(buffer))
                    {
                        result = blockBlob.BeginPutBlock(blockIds[0], blockData, null, null, optionsWithNoMD5, opContextWithMD5Check,
                                                         ar => waitHandle.Set(),
                                                         null);
                        waitHandle.WaitOne();
                        blockBlob.EndPutBlock(result);
                        Assert.IsNull(lastCheckMD5);

                        blockData.Seek(0, SeekOrigin.Begin);
                        result = blockBlob.BeginPutBlock(blockIds[1], blockData, null, null, optionsWithMD5, opContextWithMD5Check,
                                                         ar => waitHandle.Set(),
                                                         null);
                        waitHandle.WaitOne();
                        blockBlob.EndPutBlock(result);
                        Assert.AreEqual(md5, lastCheckMD5);

                        blockData.Seek(0, SeekOrigin.Begin);
                        result = blockBlob.BeginPutBlock(blockIds[2], blockData, md5, null, optionsWithNoMD5, opContextWithMD5Check,
                                                         ar => waitHandle.Set(),
                                                         null);
                        waitHandle.WaitOne();
                        blockBlob.EndPutBlock(result);
                        Assert.AreEqual(md5, lastCheckMD5);
                    }
                    Assert.AreEqual(3, checkCount);

                    CloudPageBlob pageBlob = container.GetPageBlobReference("blob2");
                    pageBlob.Create(buffer.Length);
                    checkCount = 0;
                    using (Stream pageData = new MemoryStream(buffer))
                    {
                        result = pageBlob.BeginWritePages(pageData, 0, null, null, optionsWithNoMD5, opContextWithMD5Check,
                                                          ar => waitHandle.Set(),
                                                          null);
                        waitHandle.WaitOne();
                        pageBlob.EndWritePages(result);
                        Assert.IsNull(lastCheckMD5);

                        pageData.Seek(0, SeekOrigin.Begin);
                        result = pageBlob.BeginWritePages(pageData, 0, null, null, optionsWithMD5, opContextWithMD5Check,
                                                          ar => waitHandle.Set(),
                                                          null);
                        waitHandle.WaitOne();
                        pageBlob.EndWritePages(result);
                        Assert.AreEqual(md5, lastCheckMD5);

                        pageData.Seek(0, SeekOrigin.Begin);
                        result = pageBlob.BeginWritePages(pageData, 0, md5, null, optionsWithNoMD5, opContextWithMD5Check,
                                                          ar => waitHandle.Set(),
                                                          null);
                        waitHandle.WaitOne();
                        pageBlob.EndWritePages(result);
                        Assert.AreEqual(md5, lastCheckMD5);
                    }
                    Assert.AreEqual(3, checkCount);
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Esempio n. 51
0
        public void CloudBlockBlobDownloadRangeToStreamAPMRetry()
        {
            byte[]             buffer    = GetRandomBuffer(1 * 1024 * 1024);
            int                offset    = 1024;
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
                using (MemoryStream originalBlob = new MemoryStream(buffer))
                {
                    using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                    {
                        ICancellableAsyncResult result = blob.BeginUploadFromStream(originalBlob,
                                                                                    ar => waitHandle.Set(),
                                                                                    null);
                        waitHandle.WaitOne();
                        blob.EndUploadFromStream(result);
                    }
                }

                using (MemoryStream originalBlob = new MemoryStream())
                {
                    originalBlob.Write(buffer, offset, buffer.Length - offset);

                    using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                    {
                        using (MemoryStream downloadedBlob = new MemoryStream())
                        {
                            Exception manglerEx = null;
                            using (HttpMangler proxy = new HttpMangler(false,
                                                                       new[]
                            {
                                TamperBehaviors.TamperNRequestsIf(
                                    session => ThreadPool.QueueUserWorkItem(state =>
                                {
                                    Thread.Sleep(1000);
                                    try
                                    {
                                        session.Abort();
                                    }
                                    catch (Exception e)
                                    {
                                        manglerEx = e;
                                    }
                                }),
                                    2,
                                    AzureStorageSelectors.BlobTraffic().IfHostNameContains(container.ServiceClient.Credentials.AccountName))
                            }))
                            {
                                OperationContext   operationContext = new OperationContext();
                                BlobRequestOptions options          = new BlobRequestOptions()
                                {
                                    UseTransactionalMD5 = true
                                };

                                ICancellableAsyncResult result = blob.BeginDownloadRangeToStream(downloadedBlob, offset, buffer.Length - offset, null, options, operationContext,
                                                                                                 ar => waitHandle.Set(),
                                                                                                 null);
                                waitHandle.WaitOne();
                                blob.EndDownloadToStream(result);
                                TestHelper.AssertStreamsAreEqual(originalBlob, downloadedBlob);
                            }

                            if (manglerEx != null)
                            {
                                throw manglerEx;
                            }
                        }
                    }
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
        private async Task <(bool, string)> UploadToBlob(string filename, byte[] imageBuffer = null, Stream stream = null)
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount     storageAccount     = null;
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer cloudBlobContainer = null;
            string storageConnectionString = _configuration["storageconnectionstring"];

            // Check whether the connection string can be parsed.
            if (Microsoft.WindowsAzure.Storage.CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
            {
                try
                {
                    // Create the CloudBlobClient that represents the Blob storage endpoint for the storage account.
                    Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();

                    // Create a container called 'uploadblob' and append a GUID value to it to make the name unique.
                    cloudBlobContainer = cloudBlobClient.GetContainerReference("womeninworkforce");// ("uploadblob" + Guid.NewGuid().ToString());
                    await cloudBlobContainer.CreateIfNotExistsAsync();

                    // Set the permissions so the blobs are public.
                    BlobContainerPermissions permissions = new BlobContainerPermissions
                    {
                        PublicAccess = BlobContainerPublicAccessType.Blob
                    };
                    await cloudBlobContainer.SetPermissionsAsync(permissions);

                    // Get a reference to the blob address, then upload the file to the blob.
                    CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(filename);
                    List <Task>    tasks          = new List <Task>();
                    int            count          = 0;
                    if (imageBuffer != null)
                    {
                        // OPTION A: use imageBuffer (converted from memory stream)
                        await cloudBlockBlob.UploadFromByteArrayAsync(imageBuffer, 0, imageBuffer.Length);

                        //tasks.Add(cloudBlockBlob.UploadFromByteArrayAsync(imageBuffer, 0, options, null).ContinueWith((t) =>
                        //{
                        //    sem.Release();
                        //    Interlocked.Increment(ref completed_count);
                        //}));
                        //count++;
                    }
                    else if (stream != null)
                    {
                        // OPTION B: pass in memory stream directly
                        await cloudBlockBlob.UploadFromStreamAsync(stream);
                    }
                    else
                    {
                        return(false, null);
                    }

                    return(true, cloudBlockBlob.SnapshotQualifiedStorageUri.PrimaryUri.ToString());
                }
                catch (Microsoft.WindowsAzure.Storage.StorageException ex)
                {
                    return(false, null);
                }
                finally
                {
                    // OPTIONAL: Clean up resources, e.g. blob container
                    //if (cloudBlobContainer != null)
                    //{
                    //    await cloudBlobContainer.DeleteIfExistsAsync();
                    //}
                }
            }
            else
            {
                return(false, null);
            }
        }
Esempio n. 53
0
        public void UseTransactionalMD5PutTest()
        {
            BlobRequestOptions optionsWithNoMD5 = new BlobRequestOptions()
            {
                UseTransactionalMD5 = false,
            };
            BlobRequestOptions optionsWithMD5 = new BlobRequestOptions()
            {
                UseTransactionalMD5 = true,
            };

            byte[] buffer = GetRandomBuffer(1024);
            MD5    hasher = MD5.Create();
            string md5    = Convert.ToBase64String(hasher.ComputeHash(buffer));

            string           lastCheckMD5          = null;
            int              checkCount            = 0;
            OperationContext opContextWithMD5Check = new OperationContext();

            opContextWithMD5Check.SendingRequest += (_, args) =>
            {
                if (args.Request.ContentLength >= buffer.Length)
                {
                    lastCheckMD5 = args.Request.Headers[HttpRequestHeader.ContentMd5];
                    checkCount++;
                }
            };

            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1");
                List <string>  blockIds  = GetBlockIdList(3);
                checkCount = 0;
                using (Stream blockData = new MemoryStream(buffer))
                {
                    blockBlob.PutBlock(blockIds[0], blockData, null, null, optionsWithNoMD5, opContextWithMD5Check);
                    Assert.IsNull(lastCheckMD5);

                    blockData.Seek(0, SeekOrigin.Begin);
                    blockBlob.PutBlock(blockIds[1], blockData, null, null, optionsWithMD5, opContextWithMD5Check);
                    Assert.AreEqual(md5, lastCheckMD5);

                    blockData.Seek(0, SeekOrigin.Begin);
                    blockBlob.PutBlock(blockIds[2], blockData, md5, null, optionsWithNoMD5, opContextWithMD5Check);
                    Assert.AreEqual(md5, lastCheckMD5);
                }
                Assert.AreEqual(3, checkCount);

                CloudPageBlob pageBlob = container.GetPageBlobReference("blob2");
                pageBlob.Create(buffer.Length);
                checkCount = 0;
                using (Stream pageData = new MemoryStream(buffer))
                {
                    pageBlob.WritePages(pageData, 0, null, null, optionsWithNoMD5, opContextWithMD5Check);
                    Assert.IsNull(lastCheckMD5);

                    pageData.Seek(0, SeekOrigin.Begin);
                    pageBlob.WritePages(pageData, 0, null, null, optionsWithMD5, opContextWithMD5Check);
                    Assert.AreEqual(md5, lastCheckMD5);

                    pageData.Seek(0, SeekOrigin.Begin);
                    pageBlob.WritePages(pageData, 0, md5, null, optionsWithNoMD5, opContextWithMD5Check);
                    Assert.AreEqual(md5, lastCheckMD5);
                }
                Assert.AreEqual(3, checkCount);

                blockBlob  = container.GetBlockBlobReference("blob3");
                checkCount = 0;
                using (Stream blobStream = blockBlob.OpenWrite(null, optionsWithMD5, opContextWithMD5Check))
                {
                    blobStream.Write(buffer, 0, buffer.Length);
                    blobStream.Write(buffer, 0, buffer.Length);
                }
                Assert.IsNotNull(lastCheckMD5);
                Assert.AreEqual(1, checkCount);

                blockBlob  = container.GetBlockBlobReference("blob4");
                checkCount = 0;
                using (Stream blobStream = blockBlob.OpenWrite(null, optionsWithNoMD5, opContextWithMD5Check))
                {
                    blobStream.Write(buffer, 0, buffer.Length);
                    blobStream.Write(buffer, 0, buffer.Length);
                }
                Assert.IsNull(lastCheckMD5);
                Assert.AreEqual(1, checkCount);

                pageBlob   = container.GetPageBlobReference("blob5");
                checkCount = 0;
                using (Stream blobStream = pageBlob.OpenWrite(buffer.Length * 3, null, optionsWithMD5, opContextWithMD5Check))
                {
                    blobStream.Write(buffer, 0, buffer.Length);
                    blobStream.Write(buffer, 0, buffer.Length);
                }
                Assert.IsNotNull(lastCheckMD5);
                Assert.AreEqual(1, checkCount);

                pageBlob   = container.GetPageBlobReference("blob6");
                checkCount = 0;
                using (Stream blobStream = pageBlob.OpenWrite(buffer.Length * 3, null, optionsWithNoMD5, opContextWithMD5Check))
                {
                    blobStream.Write(buffer, 0, buffer.Length);
                    blobStream.Write(buffer, 0, buffer.Length);
                }
                Assert.IsNull(lastCheckMD5);
                Assert.AreEqual(1, checkCount);
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Esempio n. 54
0
        public void BlobIngressEgressCounters()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            container.CreateIfNotExists();
            CloudBlockBlob blob = container.GetBlockBlobReference("blob1");

            string[] blockIds = new string[] { Convert.ToBase64String(Guid.NewGuid().ToByteArray()), Convert.ToBase64String(Guid.NewGuid().ToByteArray()), Convert.ToBase64String(Guid.NewGuid().ToByteArray()) };
            try
            {
                // 1 byte
                TestHelper.ValidateIngressEgress(Selectors.IfUrlContains(blob.Uri.ToString()), () =>
                {
                    OperationContext opContext = new OperationContext();
                    blob.PutBlock(blockIds[0], new MemoryStream(GetRandomBuffer(1)), null, null, new BlobRequestOptions()
                    {
                        RetryPolicy = new RetryPolicies.NoRetry()
                    }, opContext);
                    return(opContext.LastResult);
                });

                // 1024
                TestHelper.ValidateIngressEgress(Selectors.IfUrlContains(blob.Uri.ToString()), () =>
                {
                    OperationContext opContext = new OperationContext();
                    blob.PutBlock(blockIds[1], new MemoryStream(GetRandomBuffer(1024)), null, null, new BlobRequestOptions()
                    {
                        RetryPolicy = new RetryPolicies.NoRetry()
                    }, opContext);
                    return(opContext.LastResult);
                });

                // 98765
                TestHelper.ValidateIngressEgress(Selectors.IfUrlContains(blob.Uri.ToString()), () =>
                {
                    OperationContext opContext = new OperationContext();
                    blob.PutBlock(blockIds[2], new MemoryStream(GetRandomBuffer(98765)), null, null, new BlobRequestOptions()
                    {
                        RetryPolicy = new RetryPolicies.NoRetry()
                    }, opContext);
                    return(opContext.LastResult);
                });

                // PutBlockList
                TestHelper.ValidateIngressEgress(Selectors.IfUrlContains(blob.Uri.ToString()), () =>
                {
                    OperationContext opContext = new OperationContext();
                    blob.PutBlockList(blockIds, null, new BlobRequestOptions()
                    {
                        RetryPolicy = new RetryPolicies.NoRetry()
                    }, opContext);
                    return(opContext.LastResult);
                });

                // GetBlockList
                TestHelper.ValidateIngressEgress(Selectors.IfUrlContains(blob.Uri.ToString()), () =>
                {
                    OperationContext opContext = new OperationContext();
                    blob.DownloadBlockList(BlockListingFilter.All, null, new BlobRequestOptions()
                    {
                        RetryPolicy = new RetryPolicies.NoRetry()
                    }, opContext);
                    return(opContext.LastResult);
                });

                // Download
                TestHelper.ValidateIngressEgress(Selectors.IfUrlContains(blob.Uri.ToString()), () =>
                {
                    OperationContext opContext = new OperationContext();
                    blob.DownloadToStream(Stream.Null, null, new BlobRequestOptions()
                    {
                        RetryPolicy = new RetryPolicies.NoRetry()
                    }, opContext);
                    return(opContext.LastResult);
                });

                Assert.AreEqual(blob.Properties.Length, 98765 + 1024 + 1);

                // Error Case
                CloudBlockBlob   nullBlob     = container.GetBlockBlobReference("null");
                OperationContext errorContext = new OperationContext();
                try
                {
                    nullBlob.DownloadToStream(Stream.Null, null, new BlobRequestOptions()
                    {
                        RetryPolicy = new RetryPolicies.NoRetry()
                    }, errorContext);
                    Assert.Fail("Null blob, null stream, no download possible.");
                }
                catch (StorageException)
                {
                    Assert.IsTrue(errorContext.LastResult.IngressBytes > 0);
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
 private CloudBlockBlob GetBlockBlobReference(string contentName)
 {
     return(_delegateContainer.GetBlockBlobReference(AddRootPath(contentName)));
 }
Esempio n. 56
0
        private static async Task TestAccessAsync(string sasToken, SharedAccessBlobPermissions permissions, SharedAccessBlobHeaders headers, CloudBlobContainer container, CloudBlob blob, HttpStatusCode setBlobMetadataWhileSasExpectedStatusCode = HttpStatusCode.Forbidden, HttpStatusCode deleteBlobWhileSasExpectedStatusCode = HttpStatusCode.Forbidden, HttpStatusCode listBlobWhileSasExpectedStatusCode = HttpStatusCode.Forbidden)
        {
            OperationContext   operationContext = new OperationContext();
            StorageCredentials credentials      = string.IsNullOrEmpty(sasToken) ?
                                                  new StorageCredentials() :
                                                  new StorageCredentials(sasToken);

            if (container != null)
            {
                container = new CloudBlobContainer(container.Uri, credentials);
                if (blob.BlobType == BlobType.BlockBlob)
                {
                    blob = container.GetBlockBlobReference(blob.Name);
                }
                else
                {
                    blob = container.GetPageBlobReference(blob.Name);
                }
            }
            else
            {
                if (blob.BlobType == BlobType.BlockBlob)
                {
                    blob = new CloudBlockBlob(blob.Uri, credentials);
                }
                else
                {
                    blob = new CloudPageBlob(blob.Uri, credentials);
                }
            }

            if (container != null)
            {
                if ((permissions & SharedAccessBlobPermissions.List) == SharedAccessBlobPermissions.List)
                {
                    await container.ListBlobsSegmentedAsync(null);
                }
                else
                {
                    await TestHelper.ExpectedExceptionAsync(
                        async() => await container.ListBlobsSegmentedAsync(null, true, BlobListingDetails.None, null, null, null, operationContext),
                        operationContext,
                        "List blobs while SAS does not allow for listing",
                        listBlobWhileSasExpectedStatusCode);
                }
            }

            if ((permissions & SharedAccessBlobPermissions.Read) == SharedAccessBlobPermissions.Read)
            {
                await blob.FetchAttributesAsync();

                // Test headers
                if (headers != null)
                {
                    if (headers.CacheControl != null)
                    {
                        Assert.AreEqual(headers.CacheControl, blob.Properties.CacheControl);
                    }

                    if (headers.ContentDisposition != null)
                    {
                        Assert.AreEqual(headers.ContentDisposition, blob.Properties.ContentDisposition);
                    }

                    if (headers.ContentEncoding != null)
                    {
                        Assert.AreEqual(headers.ContentEncoding, blob.Properties.ContentEncoding);
                    }

                    if (headers.ContentLanguage != null)
                    {
                        Assert.AreEqual(headers.ContentLanguage, blob.Properties.ContentLanguage);
                    }

                    if (headers.ContentType != null)
                    {
                        Assert.AreEqual(headers.ContentType, blob.Properties.ContentType);
                    }
                }
            }
            else
            {
                await TestHelper.ExpectedExceptionAsync(
                    async() => await blob.FetchAttributesAsync(null, null, operationContext),
                    operationContext,
                    "Fetch blob attributes while SAS does not allow for reading",
                    HttpStatusCode.Forbidden);
            }

            if ((permissions & SharedAccessBlobPermissions.Write) == SharedAccessBlobPermissions.Write)
            {
                await blob.SetMetadataAsync();
            }
            else
            {
                await TestHelper.ExpectedExceptionAsync(
                    async() => await blob.SetMetadataAsync(null, null, operationContext),
                    operationContext,
                    "Set blob metadata while SAS does not allow for writing",
                    setBlobMetadataWhileSasExpectedStatusCode);
            }

            if ((permissions & SharedAccessBlobPermissions.Delete) == SharedAccessBlobPermissions.Delete)
            {
                await blob.DeleteAsync();
            }
            else
            {
                await TestHelper.ExpectedExceptionAsync(
                    async() => await blob.DeleteAsync(DeleteSnapshotsOption.None, null, null, operationContext),
                    operationContext,
                    "Delete blob while SAS does not allow for deleting",
                    deleteBlobWhileSasExpectedStatusCode);
            }
        }
Esempio n. 57
0
        public void StoreBlobContentMD5TestAPM()
        {
            BlobRequestOptions optionsWithNoMD5 = new BlobRequestOptions()
            {
                StoreBlobContentMD5 = false,
            };
            BlobRequestOptions optionsWithMD5 = new BlobRequestOptions()
            {
                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())
                    {
                        result = blob.BeginUploadFromStream(stream, null, optionsWithMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        blob.EndUploadFromStream(result);
                    }
                    blob.FetchAttributes();
                    Assert.IsNotNull(blob.Properties.ContentMD5);

                    blob = container.GetBlockBlobReference("blob2");
                    using (Stream stream = new NonSeekableMemoryStream())
                    {
                        result = blob.BeginUploadFromStream(stream, null, optionsWithNoMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        blob.EndUploadFromStream(result);
                    }
                    blob.FetchAttributes();
                    Assert.IsNull(blob.Properties.ContentMD5);

                    blob = container.GetBlockBlobReference("blob3");
                    using (Stream stream = new NonSeekableMemoryStream())
                    {
                        result = blob.BeginUploadFromStream(stream,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        blob.EndUploadFromStream(result);
                    }
                    blob.FetchAttributes();
                    Assert.IsNotNull(blob.Properties.ContentMD5);

                    blob = container.GetPageBlobReference("blob4");
                    using (Stream stream = new MemoryStream())
                    {
                        result = blob.BeginUploadFromStream(stream, null, optionsWithMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        blob.EndUploadFromStream(result);
                    }
                    blob.FetchAttributes();
                    Assert.IsNotNull(blob.Properties.ContentMD5);

                    blob = container.GetPageBlobReference("blob5");
                    using (Stream stream = new MemoryStream())
                    {
                        result = blob.BeginUploadFromStream(stream, null, optionsWithNoMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        blob.EndUploadFromStream(result);
                    }
                    blob.FetchAttributes();
                    Assert.IsNull(blob.Properties.ContentMD5);

                    blob = container.GetPageBlobReference("blob6");
                    using (Stream stream = new MemoryStream())
                    {
                        result = blob.BeginUploadFromStream(stream,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        blob.EndUploadFromStream(result);
                    }
                    blob.FetchAttributes();
                    Assert.IsNull(blob.Properties.ContentMD5);
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Esempio n. 58
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();
            }
        }
Esempio n. 59
0
        public void CloudBlobSASApiVersionQueryParam()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();
                CloudBlob blob;

                SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy()
                {
                    Permissions            = SharedAccessBlobPermissions.Read,
                    SharedAccessStartTime  = DateTimeOffset.UtcNow.AddMinutes(-5),
                    SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                };

                CloudBlockBlob blockBlob = container.GetBlockBlobReference("bb");
                blockBlob.PutBlockList(new string[] { });

                CloudPageBlob pageBlob = container.GetPageBlobReference("pb");
                pageBlob.Create(0);

                CloudAppendBlob appendBlob = container.GetAppendBlobReference("ab");
                appendBlob.CreateOrReplace();

                string             blockBlobToken = blockBlob.GetSharedAccessSignature(policy);
                StorageCredentials blockBlobSAS   = new StorageCredentials(blockBlobToken);
                Uri        blockBlobSASUri        = blockBlobSAS.TransformUri(blockBlob.Uri);
                StorageUri blockBlobSASStorageUri = blockBlobSAS.TransformUri(blockBlob.StorageUri);

                string             pageBlobToken = pageBlob.GetSharedAccessSignature(policy);
                StorageCredentials pageBlobSAS   = new StorageCredentials(pageBlobToken);
                Uri        pageBlobSASUri        = pageBlobSAS.TransformUri(pageBlob.Uri);
                StorageUri pageBlobSASStorageUri = pageBlobSAS.TransformUri(pageBlob.StorageUri);

                string             appendBlobToken = appendBlob.GetSharedAccessSignature(policy);
                StorageCredentials appendBlobSAS   = new StorageCredentials(appendBlobToken);
                Uri        appendBlobSASUri        = appendBlobSAS.TransformUri(appendBlob.Uri);
                StorageUri appendBlobSASStorageUri = appendBlobSAS.TransformUri(appendBlob.StorageUri);

                OperationContext apiVersionCheckContext = new OperationContext();
                apiVersionCheckContext.SendingRequest += (sender, e) =>
                {
                    Assert.IsTrue(e.Request.RequestUri.Query.Contains("api-version"));
                };

                blob = new CloudBlob(blockBlobSASUri);
                blob.FetchAttributes(operationContext: apiVersionCheckContext);
                Assert.AreEqual(blob.BlobType, BlobType.BlockBlob);
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(blockBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = new CloudBlob(pageBlobSASUri);
                blob.FetchAttributes(operationContext: apiVersionCheckContext);
                Assert.AreEqual(blob.BlobType, BlobType.PageBlob);
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(pageBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = new CloudBlob(blockBlobSASStorageUri, null, null);
                blob.FetchAttributes(operationContext: apiVersionCheckContext);
                Assert.AreEqual(blob.BlobType, BlobType.BlockBlob);
                Assert.IsTrue(blob.StorageUri.Equals(blockBlob.StorageUri));

                blob = new CloudBlob(pageBlobSASStorageUri, null, null);
                blob.FetchAttributes(operationContext: apiVersionCheckContext);
                Assert.AreEqual(blob.BlobType, BlobType.PageBlob);
                Assert.IsTrue(blob.StorageUri.Equals(pageBlob.StorageUri));
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Esempio n. 60
0
        public async Task StoreBlobContentMD5TestAsync()
        {
            BlobRequestOptions optionsWithNoMD5 = new BlobRequestOptions()
            {
                StoreBlobContentMD5 = false,
            };
            BlobRequestOptions optionsWithMD5 = new BlobRequestOptions()
            {
                StoreBlobContentMD5 = true,
            };

            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                await container.CreateAsync();

                CloudBlockBlob blob1 = container.GetBlockBlobReference("blob1");
                using (Stream stream = new NonSeekableMemoryStream())
                {
                    await blob1.UploadFromStreamAsync(stream.AsInputStream(), null, optionsWithMD5, null);
                }
                await blob1.FetchAttributesAsync();

                Assert.IsNotNull(blob1.Properties.ContentMD5);

                blob1 = container.GetBlockBlobReference("blob2");
                using (Stream stream = new NonSeekableMemoryStream())
                {
                    await blob1.UploadFromStreamAsync(stream.AsInputStream(), null, optionsWithNoMD5, null);
                }
                await blob1.FetchAttributesAsync();

                Assert.IsNull(blob1.Properties.ContentMD5);

                blob1 = container.GetBlockBlobReference("blob3");
                using (Stream stream = new NonSeekableMemoryStream())
                {
                    await blob1.UploadFromStreamAsync(stream.AsInputStream());
                }
                await blob1.FetchAttributesAsync();

                Assert.IsNotNull(blob1.Properties.ContentMD5);

                CloudPageBlob blob2 = container.GetPageBlobReference("blob4");
                blob2 = container.GetPageBlobReference("blob4");
                using (Stream stream = new MemoryStream())
                {
                    await blob2.UploadFromStreamAsync(stream.AsInputStream(), null, optionsWithMD5, null);
                }
                await blob2.FetchAttributesAsync();

                Assert.IsNotNull(blob2.Properties.ContentMD5);

                blob2 = container.GetPageBlobReference("blob5");
                using (Stream stream = new MemoryStream())
                {
                    await blob2.UploadFromStreamAsync(stream.AsInputStream(), null, optionsWithNoMD5, null);
                }
                await blob2.FetchAttributesAsync();

                Assert.IsNull(blob2.Properties.ContentMD5);

                blob2 = container.GetPageBlobReference("blob6");
                using (Stream stream = new MemoryStream())
                {
                    await blob2.UploadFromStreamAsync(stream.AsInputStream());
                }
                await blob2.FetchAttributesAsync();

                Assert.IsNull(blob2.Properties.ContentMD5);
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }