Exemplo n.º 1
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);
                }
            }
        }
Exemplo n.º 2
0
        // have destination location.
        // have original blob name and prefix
        // new name should be destination name + (blob.name - blob.prefix)
        public static BlobCopyData StartCopy(BasicBlobContainer origBlob, string DestinationUrl, DestinationBlobType destBlobType, bool skipIfExists)
        {
            var client = AzureHelper.GetTargetCloudBlobClient(DestinationUrl);
            var opt    = client.GetServiceProperties();

            var containerName  = AzureHelper.GetContainerFromUrl(DestinationUrl);
            var destBlobPrefix = AzureHelper.GetBlobFromUrl(DestinationUrl);

            var container = client.GetContainerReference(containerName);

            container.CreateIfNotExists();

            ICloudBlob blob = null;
            var        url  = GeneratedAccessibleUrl(origBlob);

            string blobName;

            string blobWithoutPrefix = string.Empty;

            if (!string.IsNullOrWhiteSpace(origBlob.BlobPrefix) && origBlob.Name.Length > origBlob.BlobPrefix.Length)
            {
                blobWithoutPrefix = origBlob.Name.Substring(origBlob.BlobPrefix.Length);
            }

            if (!string.IsNullOrWhiteSpace(blobWithoutPrefix))
            {
                blobName = string.Format("{0}/{1}", destBlobPrefix, blobWithoutPrefix);
            }
            else
            {
                // need to get just filename. ie last element of /
                var actualBlobName = origBlob.Name.Split('/').Last();

                if (string.IsNullOrWhiteSpace(destBlobPrefix))
                {
                    blobName = actualBlobName;
                }
                else
                {
                    blobName = string.Format("{0}/{1}", destBlobPrefix, actualBlobName);
                }
            }

            // include unknown for now. Unsure.
            if (destBlobType == DestinationBlobType.Block || destBlobType == DestinationBlobType.Unknown)
            {
                blob = container.GetBlockBlobReference(blobName);
            }
            else if (destBlobType == DestinationBlobType.Page)
            {
                blob = container.GetPageBlobReference(blobName);
            }

            if (skipIfExists)
            {
                try
                {
                    blob.Exists();
                }
                catch (Exception)
                {
                    Console.WriteLine("Skipping {0}", url);
                    return(null);
                }
            }

            if (blob != null)
            {
                try
                {
                    // crazy large values, want to try and debug an issue.
                    var brOptions = new BlobRequestOptions();
                    brOptions.MaximumExecutionTime = new TimeSpan(0, maxExecutionTimeInMins, 0);
                    brOptions.ServerTimeout        = new TimeSpan(0, maxServerTimeoutInMins, 0);

                    // return copyID incase user wants to kill the process later.
                    var copyID = blob.StartCopyFromBlob(new Uri(url), options: brOptions);

                    var bcd = new BlobCopyData {
                        CopyID = copyID, Blob = blob
                    };
                    return(bcd);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("StartCopyFromBlob error msg " + ex.Message);
                    Console.WriteLine("StartCopyFromBlob error stack " + ex.StackTrace);
                    throw;
                }
            }
            else
            {
                throw new NotImplementedException("Cannot copy blobs that are not block or page");
            }
        }
Exemplo n.º 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="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);
                }
            }
        }