Ejemplo n.º 1
0
 public static void DeletePackageFromBlob(IServiceManagement channel, string storageName, string subscriptionId, Uri packageUri)
 {
     var storageService = channel.GetStorageKeys(subscriptionId, storageName);
     var storageKey = storageService.StorageServiceKeys.Primary;
     storageService = channel.GetStorageService(subscriptionId, storageName);
     var blobStorageEndpoint = new Uri(storageService.StorageServiceProperties.Endpoints.Find(p => p.Contains(BlobEndpointIdentifier)));
     var credentials = new StorageCredentials(storageName, storageKey);
     var client = new CloudBlobClient(blobStorageEndpoint, credentials);
     ICloudBlob blob = client.GetBlobReferenceFromServer(packageUri);
     blob.DeleteIfExists();
 }
 public void DeletePackageFromBlob(string storageName, Uri packageUri)
 {
     StorageAccountGetKeysResponse keys = StorageManagementClient.StorageAccounts.GetKeys(storageName);
     string storageKey = keys.PrimaryKey;
     var storageService = StorageManagementClient.StorageAccounts.Get(storageName);
     var blobStorageEndpoint = storageService.StorageAccount.Properties.Endpoints[0];
     var credentials = new StorageCredentials(storageName, storageKey);
     var client = new CloudBlobClient(blobStorageEndpoint, credentials);
     ICloudBlob blob = client.GetBlobReferenceFromServer(packageUri);
     blob.DeleteIfExists();
 }
Ejemplo n.º 3
0
        public static void RemoveVHD(IServiceManagement channel, string subscriptionId, Uri mediaLink)
        {
            var accountName = mediaLink.Host.Split('.')[0];
            var blobEndpoint = new Uri(mediaLink.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped));

            StorageService storageService;
            using (new OperationContextScope(channel.ToContextChannel()))
            {
                storageService = channel.GetStorageKeys(subscriptionId, accountName);
            }

            var storageAccountCredentials = new StorageCredentials(accountName, storageService.StorageServiceKeys.Primary);
            var client = new CloudBlobClient(blobEndpoint, storageAccountCredentials);
            var blob = client.GetBlobReferenceFromServer(mediaLink);
            blob.DeleteIfExists();
        }
        public string GetTempUrl(string accountName, string accountKey, string fullPath)
        {
            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();
            var blob = blobClient.GetBlobReferenceFromServer(new Uri(fullPath));

            var readPolicy = new Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPolicy()
            {
                Permissions            = Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions.Read, // SharedAccessPermissions.Read,
                SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromMinutes(10)
            };

            string resultUrl = new Uri(blob.Uri.AbsoluteUri + blob.GetSharedAccessSignature(readPolicy)).ToString();

            return(resultUrl);
        }
        public string GetTempDownloadUrl(string accountName, string accountKey, string fullPath)
        {
            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();
            var blob = blobClient.GetBlobReferenceFromServer(new Uri(fullPath));

            var readPolicy = new Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPolicy()
            {
                Permissions            = Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions.Read, // SharedAccessPermissions.Read,
                SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromMinutes(10)
            };

            string resultUrl = new Uri(blob.Uri.AbsoluteUri + blob.GetSharedAccessSignature(readPolicy,
                                                                                            new SharedAccessBlobHeaders {
                ContentDisposition = blob.Metadata.ContainsKey("originalfilename") ? "attachment; filename=" + blob.Metadata["originalfilename"] : "attachment; filename=FileUnknown",
                ContentType        = blob.Properties.ContentType
            }
                                                                                            )).ToString();

            return(resultUrl);
        }
 public static void DownloadTestCredentials(string testEnvironment, string downloadDirectoryPath, string blobUri, string storageAccount, string storageKey)
 {
     string containerPath = string.Format(EnvironmentPathFormat, testEnvironment);
     StorageCredentials credentials = new StorageCredentials(storageAccount, storageKey);
     CloudBlobClient blobClient = new CloudBlobClient(new Uri(blobUri), credentials);
     CloudBlobContainer container = blobClient.GetContainerReference(containerPath);
     foreach (IListBlobItem blobItem in container.ListBlobs())
     {
         ICloudBlob blob = blobClient.GetBlobReferenceFromServer(blobItem.Uri);
         Console.WriteLine("Downloading file {0} from blob Uri {1}", blob.Name, blob.Uri);
         FileStream blobStream = new FileStream(Path.Combine(downloadDirectoryPath, blob.Name), FileMode.Create);
         blob.DownloadToStream(blobStream);
         blobStream.Flush();
         blobStream.Close();
     }
 }
Ejemplo n.º 7
0
        public void CloudBlobContainerGetBlobReferenceFromServer()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                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);

                ICloudBlob blob1 = container.GetBlobReferenceFromServer("bb");
                Assert.IsInstanceOfType(blob1, typeof(CloudBlockBlob));

                CloudBlockBlob blob1Snapshot = ((CloudBlockBlob)blob1).CreateSnapshot();
                blob1.SetProperties();
                Uri        blob1SnapshotUri       = new Uri(blob1Snapshot.Uri.AbsoluteUri + "?snapshot=" + blob1Snapshot.SnapshotTime.Value.UtcDateTime.ToString("o"));
                ICloudBlob blob1SnapshotReference = container.ServiceClient.GetBlobReferenceFromServer(blob1SnapshotUri);
                AssertAreEqual(blob1Snapshot.Properties, blob1SnapshotReference.Properties);

                ICloudBlob blob2 = container.GetBlobReferenceFromServer("pb");
                Assert.IsInstanceOfType(blob2, typeof(CloudPageBlob));

                CloudPageBlob blob2Snapshot = ((CloudPageBlob)blob2).CreateSnapshot();
                blob2.SetProperties();
                Uri        blob2SnapshotUri       = new Uri(blob2Snapshot.Uri.AbsoluteUri + "?snapshot=" + blob2Snapshot.SnapshotTime.Value.UtcDateTime.ToString("o"));
                ICloudBlob blob2SnapshotReference = container.ServiceClient.GetBlobReferenceFromServer(blob2SnapshotUri);
                AssertAreEqual(blob2Snapshot.Properties, blob2SnapshotReference.Properties);

                ICloudBlob blob3 = container.ServiceClient.GetBlobReferenceFromServer(blockBlob.Uri);
                Assert.IsInstanceOfType(blob3, typeof(CloudBlockBlob));

                ICloudBlob blob4 = container.ServiceClient.GetBlobReferenceFromServer(pageBlob.Uri);
                Assert.IsInstanceOfType(blob4, typeof(CloudPageBlob));

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

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

                ICloudBlob blob5 = container.ServiceClient.GetBlobReferenceFromServer(blockBlobSASUri);
                Assert.IsInstanceOfType(blob5, typeof(CloudBlockBlob));

                ICloudBlob blob6 = container.ServiceClient.GetBlobReferenceFromServer(pageBlobSASUri);
                Assert.IsInstanceOfType(blob6, typeof(CloudPageBlob));

                CloudBlobClient client7 = new CloudBlobClient(container.ServiceClient.BaseUri, blockBlobSAS);
                ICloudBlob      blob7   = client7.GetBlobReferenceFromServer(blockBlobSASUri);
                Assert.IsInstanceOfType(blob7, typeof(CloudBlockBlob));

                CloudBlobClient client8 = new CloudBlobClient(container.ServiceClient.BaseUri, pageBlobSAS);
                ICloudBlob      blob8   = client8.GetBlobReferenceFromServer(pageBlobSASUri);
                Assert.IsInstanceOfType(blob8, typeof(CloudPageBlob));
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
        private static void DownloadTestVhdsAndPackages(string testEnvironment, string downloadDirectoryPath, string blobUri, string storageAccount, string storageKey)
        {
            StorageCredentials credentials = new StorageCredentials(storageAccount, storageKey);
            CloudBlobClient blobClient = new CloudBlobClient(new Uri(blobUri), credentials);
            blobContainer = blobClient.GetContainerReference(VhdFilesContainerName);
            foreach (IListBlobItem blobItem in blobContainer.ListBlobs())
            {
                ICloudBlob blob = blobClient.GetBlobReferenceFromServer(blobItem.Uri);
                Console.WriteLine("Downloading file {0} from blob Uri {1}", blob.Name, blob.Uri);
                FileStream blobStream = new FileStream(Path.Combine(downloadDirectoryPath, blob.Name), FileMode.Create);
                blob.DownloadToStream(blobStream);
                blobStream.Flush();
                blobStream.Close();
            }

            blobContainer = blobClient.GetContainerReference(toolsContainerName);
            foreach (IListBlobItem blobItem in blobContainer.ListBlobs())
            {
                ICloudBlob blob = blobClient.GetBlobReferenceFromServer(blobItem.Uri);
                Console.WriteLine("Downloading file {0} from blob Uri {1}", blob.Name, blob.Uri);
                FileStream blobStream = new FileStream(Path.Combine(downloadDirectoryPath, @"..\..\", blob.Name), FileMode.Create);
                blob.DownloadToStream(blobStream);
                blobStream.Flush();
                blobStream.Close();
            }
        }
        public void CloudBlobContainerGetBlobReferenceFromServer()
        {
            CloudBlobContainer container = GetRandomContainerReference();
            try
            {
                container.Create();

                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();

                CloudBlobClient client;
                ICloudBlob blob;

                blob = container.GetBlobReferenceFromServer("bb");
                Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob));
                Assert.IsTrue(blob.StorageUri.Equals(blockBlob.StorageUri));

                CloudBlockBlob blockBlobSnapshot = ((CloudBlockBlob)blob).CreateSnapshot();
                blob.SetProperties();
                Uri blockBlobSnapshotUri = new Uri(blockBlobSnapshot.Uri.AbsoluteUri + "?snapshot=" + blockBlobSnapshot.SnapshotTime.Value.UtcDateTime.ToString("o"));
                blob = container.ServiceClient.GetBlobReferenceFromServer(blockBlobSnapshotUri);
                AssertAreEqual(blockBlobSnapshot.Properties, blob.Properties);
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(blockBlobSnapshot.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = container.GetBlobReferenceFromServer("pb");
                Assert.IsInstanceOfType(blob, typeof(CloudPageBlob));
                Assert.IsTrue(blob.StorageUri.Equals(pageBlob.StorageUri));

                CloudPageBlob pageBlobSnapshot = ((CloudPageBlob)blob).CreateSnapshot();
                blob.SetProperties();
                Uri pageBlobSnapshotUri = new Uri(pageBlobSnapshot.Uri.AbsoluteUri + "?snapshot=" + pageBlobSnapshot.SnapshotTime.Value.UtcDateTime.ToString("o"));
                blob = container.ServiceClient.GetBlobReferenceFromServer(pageBlobSnapshotUri);
                AssertAreEqual(pageBlobSnapshot.Properties, blob.Properties);
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(pageBlobSnapshot.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = container.GetBlobReferenceFromServer("ab");
                Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob));
                Assert.IsTrue(blob.StorageUri.Equals(appendBlob.StorageUri));

                CloudAppendBlob appendBlobSnapshot = ((CloudAppendBlob)blob).CreateSnapshot();
                blob.SetProperties();
                Uri appendBlobSnapshotUri = new Uri(appendBlobSnapshot.Uri.AbsoluteUri + "?snapshot=" + appendBlobSnapshot.SnapshotTime.Value.UtcDateTime.ToString("o"));
                blob = container.ServiceClient.GetBlobReferenceFromServer(appendBlobSnapshotUri);
                AssertAreEqual(appendBlobSnapshot.Properties, blob.Properties);
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(appendBlobSnapshot.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = container.ServiceClient.GetBlobReferenceFromServer(blockBlob.Uri);
                Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob));
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(blockBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = container.ServiceClient.GetBlobReferenceFromServer(pageBlob.Uri);
                Assert.IsInstanceOfType(blob, typeof(CloudPageBlob));
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(pageBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = container.ServiceClient.GetBlobReferenceFromServer(appendBlob.Uri);
                Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob));
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(appendBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = container.ServiceClient.GetBlobReferenceFromServer(blockBlob.StorageUri);
                Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob));
                Assert.IsTrue(blob.StorageUri.Equals(blockBlob.StorageUri));

                blob = container.ServiceClient.GetBlobReferenceFromServer(pageBlob.StorageUri);
                Assert.IsInstanceOfType(blob, typeof(CloudPageBlob));
                Assert.IsTrue(blob.StorageUri.Equals(pageBlob.StorageUri));

                blob = container.ServiceClient.GetBlobReferenceFromServer(appendBlob.StorageUri);
                Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob));
                Assert.IsTrue(blob.StorageUri.Equals(appendBlob.StorageUri));

                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);

                blob = container.ServiceClient.GetBlobReferenceFromServer(blockBlobSASUri);
                Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob));
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(blockBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = container.ServiceClient.GetBlobReferenceFromServer(pageBlobSASUri);
                Assert.IsInstanceOfType(blob, typeof(CloudPageBlob));
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(pageBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = container.ServiceClient.GetBlobReferenceFromServer(appendBlobSASUri);
                Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob));
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(appendBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = container.ServiceClient.GetBlobReferenceFromServer(blockBlobSASStorageUri);
                Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob));
                Assert.IsTrue(blob.StorageUri.Equals(blockBlob.StorageUri));

                blob = container.ServiceClient.GetBlobReferenceFromServer(pageBlobSASStorageUri);
                Assert.IsInstanceOfType(blob, typeof(CloudPageBlob));
                Assert.IsTrue(blob.StorageUri.Equals(pageBlob.StorageUri));

                blob = container.ServiceClient.GetBlobReferenceFromServer(appendBlobSASStorageUri);
                Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob));
                Assert.IsTrue(blob.StorageUri.Equals(appendBlob.StorageUri));

                client = new CloudBlobClient(container.ServiceClient.BaseUri, blockBlobSAS);
                blob = client.GetBlobReferenceFromServer(blockBlobSASUri);
                Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob));
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(blockBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                client = new CloudBlobClient(container.ServiceClient.BaseUri, pageBlobSAS);
                blob = client.GetBlobReferenceFromServer(pageBlobSASUri);
                Assert.IsInstanceOfType(blob, typeof(CloudPageBlob));
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(pageBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                client = new CloudBlobClient(container.ServiceClient.BaseUri, appendBlobSAS);
                blob = client.GetBlobReferenceFromServer(appendBlobSASUri);
                Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob));
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(appendBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                client = new CloudBlobClient(container.ServiceClient.StorageUri, blockBlobSAS);
                blob = client.GetBlobReferenceFromServer(blockBlobSASStorageUri);
                Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob));
                Assert.IsTrue(blob.StorageUri.Equals(blockBlob.StorageUri));

                client = new CloudBlobClient(container.ServiceClient.StorageUri, pageBlobSAS);
                blob = client.GetBlobReferenceFromServer(pageBlobSASStorageUri);
                Assert.IsInstanceOfType(blob, typeof(CloudPageBlob));
                Assert.IsTrue(blob.StorageUri.Equals(pageBlob.StorageUri));

                client = new CloudBlobClient(container.ServiceClient.StorageUri, appendBlobSAS);
                blob = client.GetBlobReferenceFromServer(appendBlobSASStorageUri);
                Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob));
                Assert.IsTrue(blob.StorageUri.Equals(appendBlob.StorageUri));
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
        public void CloudBlobContainerGetBlobReferenceFromServer()
        {
            CloudBlobContainer container = GetRandomContainerReference();
            try
            {
                container.Create();

                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);
                
                ICloudBlob blob1 = container.GetBlobReferenceFromServer("bb");
                Assert.IsInstanceOfType(blob1, typeof(CloudBlockBlob));

                CloudBlockBlob blob1Snapshot = ((CloudBlockBlob)blob1).CreateSnapshot();
                blob1.SetProperties();
                Uri blob1SnapshotUri = new Uri(blob1Snapshot.Uri.AbsoluteUri + "?snapshot=" + blob1Snapshot.SnapshotTime.Value.UtcDateTime.ToString("o"));
                ICloudBlob blob1SnapshotReference = container.ServiceClient.GetBlobReferenceFromServer(blob1SnapshotUri);
                AssertAreEqual(blob1Snapshot.Properties, blob1SnapshotReference.Properties);

                ICloudBlob blob2 = container.GetBlobReferenceFromServer("pb");
                Assert.IsInstanceOfType(blob2, typeof(CloudPageBlob));

                CloudPageBlob blob2Snapshot = ((CloudPageBlob)blob2).CreateSnapshot();
                blob2.SetProperties();
                Uri blob2SnapshotUri = new Uri(blob2Snapshot.Uri.AbsoluteUri + "?snapshot=" + blob2Snapshot.SnapshotTime.Value.UtcDateTime.ToString("o"));
                ICloudBlob blob2SnapshotReference = container.ServiceClient.GetBlobReferenceFromServer(blob2SnapshotUri);
                AssertAreEqual(blob2Snapshot.Properties, blob2SnapshotReference.Properties);

                ICloudBlob blob3 = container.ServiceClient.GetBlobReferenceFromServer(blockBlob.Uri);
                Assert.IsInstanceOfType(blob3, typeof(CloudBlockBlob));

                ICloudBlob blob4 = container.ServiceClient.GetBlobReferenceFromServer(pageBlob.Uri);
                Assert.IsInstanceOfType(blob4, typeof(CloudPageBlob));

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

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

                ICloudBlob blob5 = container.ServiceClient.GetBlobReferenceFromServer(blockBlobSASUri);
                Assert.IsInstanceOfType(blob5, typeof(CloudBlockBlob));

                ICloudBlob blob6 = container.ServiceClient.GetBlobReferenceFromServer(pageBlobSASUri);
                Assert.IsInstanceOfType(blob6, typeof(CloudPageBlob));

                CloudBlobClient client7 = new CloudBlobClient(container.ServiceClient.BaseUri, blockBlobSAS);
                ICloudBlob blob7 = client7.GetBlobReferenceFromServer(blockBlobSASUri);
                Assert.IsInstanceOfType(blob7, typeof(CloudBlockBlob));

                CloudBlobClient client8 = new CloudBlobClient(container.ServiceClient.BaseUri, pageBlobSAS);
                ICloudBlob blob8 = client8.GetBlobReferenceFromServer(pageBlobSASUri);
                Assert.IsInstanceOfType(blob8, typeof(CloudPageBlob));
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Begins orchestration of bootstrapper tasks.
        /// </summary>
        /// <param name="args">The validated args parsed from the commmand line.</param>
        public void Start(BootStrapperArgs args)
        {
            if (RoleEnvironment.IsAvailable && RoleEnvironment.IsEmulated && !args.RunInEmulator)
            {
                // Skip running if under emulator
                return;
            }

            try
            {
                Client = String.IsNullOrEmpty(args.StorageConnection) ? null : CloudStorageAccount.Parse(args.StorageConnection).CreateCloudBlobClient();
            }
            catch (Exception ex)
            {
                throw new ArgumentException("The Azure storage connection string is not valid:  " + ex.Message, args.StorageConnection);
            }

            //download package (if not previously downloaded).  RunAlways forces download always.
            Uri targetUri = null;
            String package = null;
            if ( !String.IsNullOrEmpty(args.Get) )
            {
                Uri urlToDownload = null;

                //if a storage account is present generate a SAS for the get
                if (Client != null)
                {
                    //x| var account = CloudStorageAccount.Parse(args.StorageConnection);
                    //x| var client = account.CreateCloudBlobClient();

                    //TODO BASEURI THING
                    // MAKE

                    var uri = new Uri(Client.BaseUri, args.Get);
                    var reference = Client.GetBlobReferenceFromServer(uri);

                    var sas = reference.GetSharedAccessSignature(
                        new SharedAccessBlobPolicy()
                        {
                            Permissions = SharedAccessBlobPermissions.Read,
                            SharedAccessExpiryTime = DateTime.Now.AddMinutes(10)
                        });

                    urlToDownload = new Uri(reference.Uri, sas);
                }
                else //otherwise, this is a standard get
                {
                    if (!Uri.TryCreate(args.Get, UriKind.Absolute, out urlToDownload))
                        throw new ArgumentException("Invalid Uri", "args.Get");
                }

                //always download as a get
                package = this.downloader.DownloadPackageToDisk(
                    urlToDownload,
                    args.RunAlways,
                    args.LocalResource
                    );

                //Unzip without replace.  RunAlways forces unzip with replace
                if (args.Unzip)
                {
                    this.unzipper.Unzip(package, args.UnzipTarget, args.RunAlways);
                }
            }

            if ( !String.IsNullOrEmpty(args.Put) )
            {
                if ( Client != null )
                {
                    try {
                        var uri = new Uri(Client.BaseUri, args.Put);
                        var blob = Client.GetBlobReferenceFromServer(uri);
                        blob.Container.CreateIfNotExists();
                        String target = blob.GetSharedAccessSignature(
                            new SharedAccessBlobPolicy() {
                                Permissions = SharedAccessBlobPermissions.Write,
                                SharedAccessExpiryTime = DateTime.Now.AddMinutes(30)
                            });
                        if (args.Overwrite)
                            blob.DeleteIfExists();
                        targetUri = new Uri(blob.Uri, target);
                    }
                    catch ( StorageException ex ) {
                        if (Uri.IsWellFormedUriString(args.Put, UriKind.Relative))
                            throw new ArgumentException(
                                "A valid connection string to Azure blob storage must be supplied when uploading to a relative URL.\n" + ex.Message,
                                args.StorageConnection);
                    }
                }
                if ( targetUri == null ) //i| fall back on standard put.
                {
                    if ( !Uri.TryCreate(args.Put, UriKind.Absolute, out targetUri) )
                        throw new ArgumentException("Invalid URL was supplied for the PUT argument.", args.Put);
                }

                //i| Only package when we don't have a filename (didn't get), and the local resource is an existing folder.
                if ( package == null &&  Directory.Exists(args.LocalResource))
                {
                    package = Path.Combine(args.LocalResource, Path.GetFileName(targetUri.LocalPath));
                    this.zipper.Zip(args.LocalResource, package, args.Overwrite);
                }

                //i| Always download as a get
                package = this.uploader.UploadPackageToStorage(
                    package ?? args.LocalResource,
                    targetUri,
                    args.Overwrite
                    );
            }

            //Run the command, record output to local disk for repository
            if (!String.IsNullOrEmpty(args.Run))
            {
                if (!this.runner.PackageExists(args.Run) || args.RunAlways)
                {
                    this.runner.Start(
                        args.Run,
                        args.Args,
                        args.EnableSystemProfile,
                        args.Block
                        );
                }
            }
        }