public bool Connect()
        {
            if (!_config.SasEndpointInfo.IsPopulated())
            {
                Log.Warning("no blob or token info. exiting:", _config.SasEndpointInfo);
                return(false);
            }

            try
            {
                CloudStorageAccount.UseV1MD5 = false;
                _account = CloudStorageAccount.Parse(_config.SasEndpointInfo.ConnectionString);
                CloudBlobClient storageClient = _account.CreateCloudBlobClient();
                _blobClient = storageClient.GetRootContainerReference().ServiceClient;

                // no communication with storage account until here:
                EnumerateContainers(null, true);
                return(true);
            }
            catch (Exception e)
            {
                Log.Exception($"{e}");
                return(false);
            }
        }
Beispiel #2
0
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            string storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=strrdm;AccountKey=jykbgp7GhLpHiqZIMtstB+tTLx+EtfELlrOHhPbNV4PUHDySga97Rv/jK68v9mSrFx6INL/jVFJpzv51uyWvqQ==;EndpointSuffix=core.windows.net";
            CloudStorageAccount storageAccount;

            if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
            {// Create the CloudBlobClient that represents the Blob storage endpoint for the storage account.
                CloudBlobClient cbc     = storageAccount.CreateCloudBlobClient();
                var             conroot = cbc.GetRootContainerReference();
                Console.WriteLine(conroot.Name);
                BlobContinuationToken token = null;
                var listcon = new List <CloudBlobContainer>();
                do
                {
                    var respon = await cbc.ListContainersSegmentedAsync(token);

                    listcon.AddRange(respon.Results);
                    token = respon.ContinuationToken;
                } while (token != null);
                foreach (var c in listcon)
                {
                    Console.WriteLine(c.Name);
                }
                Console.WriteLine(listcon.Count);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Lists all blobs in a container.
        /// Can be supplied a blobPrefix which basically acts as virtual directory options.
        /// eg, if we have blobs called: "virt1/virt2/myblob"    and
        ///                              "virt1/virt2/myblob2"
        /// Although the blob names are the complete strings mentioned above, we might like to think that the blobs
        /// are just called myblob and myblob2. We can supply a blobPrefix of "virt1/virt2/" which we can *think* of
        /// as a directory, but again, its just really a prefix behind the scenes.
        ///
        /// For other sytems (not Azure) the blobPrefix might be real directories....  will need to investigate
        /// </summary>
        /// <param name="containerName"></param>
        /// <param name="blobPrefix"></param>
        /// <returns></returns>
        public IEnumerable <BasicBlobContainer> ListBlobsInContainer(string containerName = null, string blobPrefix = null, bool debug = false)
        {
            IEnumerable <IListBlobItem> azureBlobList;
            CloudBlobContainer          container;

            if (string.IsNullOrEmpty(containerName))
            {
                container = client.GetRootContainerReference();

                // add container.
                // Assuming no blobs at root level.
                // incorrect assumption. FIXME!
                var containerList = client.ListContainers();
                foreach (var cont in containerList)
                {
                    var b = new BasicBlobContainer();
                    b.Name        = cont.Name;
                    b.DisplayName = AzureHelper.GetDisplayName(cont.Name);
                    b.Container   = "";
                    b.Url         = cont.Uri.AbsoluteUri;
                    b.BlobType    = BlobEntryType.Container;
                    yield return(b);
                }
            }
            else
            {
                container = client.GetContainerReference(containerName);

                // if we were only passed the container name, then list contents of container.
                if (string.IsNullOrEmpty(blobPrefix))
                {
                    // add blobs
                    azureBlobList = container.ListBlobs(useFlatBlobListing: true);
                }
                else
                {
                    throw new NotImplementedException();

                    // if passed virtual directory information, then filter based off that.
                    //var vd = container.GetDirectoryReference(virtualDirectoryName);
                    //azureBlobList = vd.ListBlobs();
                }

                foreach (var blob in azureBlobList)
                {
                    var b  = new BasicBlobContainer();
                    var bn = AzureHelper.GetBlobFromUrl(blob.Uri.AbsoluteUri);
                    b.Name = bn;
                    var sp          = bn.Split('/');
                    var displayName = sp[sp.Length - 1];
                    b.DisplayName = displayName;
                    b.Container   = blob.Container.Name;
                    b.Url         = blob.Uri.AbsoluteUri;
                    b.BlobType    = BlobEntryType.Blob;
                    yield return(b);
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="account"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static CloudBlobContainer GetRootContainer(string account, string key)
        {
            StorageCredentials sc = new StorageCredentials(account, key);
            //Initializes a new instance of the CloudStorageAccount class using the specified credentials, and specifies whether to use HTTP or HTTPS to connect to the storage services.
            CloudStorageAccount csa = new CloudStorageAccount(sc, true);

            //Initializes a new instance of the CloudBlobClient class using the specified Blob service endpoint and account credentials.
            var blobClient = new CloudBlobClient(csa.BlobStorageUri, sc);

            return(blobClient.GetRootContainerReference());
        }
        private void rootFix(ref CloudBlobContainer container, ref string blobPath)
        {
            if (ContainerName != "$root")
            {
                return;
            }

            var slashIdx = blobPath.IndexOf('/');

            if (slashIdx > -1)
            {
                container = _blobClient.GetContainerReference(blobPath.Substring(0, slashIdx));
                blobPath  = blobPath.Substring(slashIdx + 1);
            }
            else
            {
                container = _blobClient.GetRootContainerReference();
            }
        }
Beispiel #6
0
        /// <summary>
        /// Lists all blobs in a container.
        /// Can be supplied a blobPrefix which basically acts as virtual directory options.
        /// eg, if we have blobs called: "virt1/virt2/myblob"    and
        ///                              "virt1/virt2/myblob2"
        /// Although the blob names are the complete strings mentioned above, we might like to think that the blobs
        /// are just called myblob and myblob2. We can supply a blobPrefix of "virt1/virt2/" which we can *think* of
        /// as a directory, but again, its just really a prefix behind the scenes.
        ///
        /// For other sytems (not Azure) the blobPrefix might be real directories....  will need to investigate
        /// </summary>
        /// <param name="container"></param>
        /// <param name="blobPrefix"></param>
        /// <returns></returns>
        public IEnumerable <BasicBlobContainer> ListBlobsInContainer(string containerName = null, string blobPrefix = null, bool debug = false)
        {
            IEnumerable <IListBlobItem> azureBlobList;
            CloudBlobContainer          container;

            if (string.IsNullOrWhiteSpace(containerName))
            {
                containerName = defaultContainerName;
            }

            if (string.IsNullOrWhiteSpace(blobPrefix))
            {
                blobPrefix = defaultBlobPrefix;
            }

            if (string.IsNullOrEmpty(containerName))
            {
                container = client.GetRootContainerReference();

                // add container.
                // Assuming no blobs at root level.
                // incorrect assumption. FIXME!
                var containerList = client.ListContainers();
                foreach (var cont in containerList)
                {
                    var b = new BasicBlobContainer();
                    b.Name        = Uri.UnescapeDataString(cont.Name);
                    b.DisplayName = AzureHelper.GetDisplayName(cont.Name);
                    b.Container   = "";
                    b.Url         = Uri.UnescapeDataString(cont.Uri.AbsoluteUri);
                    b.BlobType    = BlobEntryType.Container;

                    yield return(b);
                }
            }
            else
            {
                container = client.GetContainerReference(containerName);

                // if we were only passed the container name, then list contents of container.
                if (string.IsNullOrEmpty(blobPrefix))
                {
                    // add blobs
                    azureBlobList = container.ListBlobs(useFlatBlobListing: true);
                }
                else
                {
                    var vd = container.GetDirectoryReference(blobPrefix);
                    azureBlobList = ListVirtualDirectoryBlobs(vd);
                }

                foreach (var blob in azureBlobList)
                {
                    var b  = new BasicBlobContainer();
                    var bn = AzureHelper.GetBlobFromUrl(blob.Uri.AbsoluteUri);
                    b.BlobPrefix  = blobPrefix;
                    b.Name        = Uri.UnescapeDataString(bn);
                    b.DisplayName = AzureHelper.GetDisplayName(bn);
                    b.Container   = blob.Container.Name;
                    b.Url         = Uri.UnescapeDataString(blob.Uri.AbsoluteUri);
                    b.BlobType    = BlobEntryType.Blob;
                    yield return(b);
                }
            }
        }
Beispiel #7
0
 /// <summary>
 /// Get the cloud blob.
 /// </summary>
 /// <returns>The cloud blob.</returns>
 public CloudBlobContainer GetRootCloudBlobContainer()
 {
     return(_cloudBlobClient.GetRootContainerReference());
 }