Ejemplo n.º 1
0
        public static List<DiscountBlobContainer> GetSubContainers(string parent = "")
        {
            try {
                // Retrieve storage account from connection string
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));

                // Create the blobl client
                CloudBlobClient client = storageAccount.CreateCloudBlobClient();

                CloudBlobDirectory container = client.GetBlobDirectoryReference(parent);
                List<DiscountBlobContainer> subs = new List<DiscountBlobContainer>();

                foreach (CloudBlobDirectory sub_dir in container.ListBlobs().OfType<CloudBlobDirectory>()) {
                    DiscountBlobContainer sub = new DiscountBlobContainer {
                        BlobCount = sub_dir.ListBlobs().Count(),
                        Container = sub_dir.Container,
                        uri = sub_dir.Uri
                    };
                    subs.Add(sub);
                }

                return subs;
            } catch (Exception) {
                return new List<DiscountBlobContainer>();
            }
        }
Ejemplo n.º 2
0
        internal static DiscountBlobContainer CreateContainer(string name = "", string parent = "", bool make_public = true)
        {
            // Build out a relationship between the new container name and parent (if parent is provided)
            string conName = name.ToLower().Replace(' ','-');
            // Retrieve storage account from connection string
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));

            // Create the blobl client
            CloudBlobClient client = storageAccount.CreateCloudBlobClient();
            DiscountBlobContainer container = new DiscountBlobContainer();

            CloudBlobContainer con = null;
            if (parent.Length > 0) {
                string[] folders = parent.Split('/');
                parent = "";
                for (var i = 1; i < folders.Count(); i++) {
                    parent += folders[i];
                    if (i < folders.Count() - 1 && parent != "") {
                        parent += "/";
                    }
                }
                conName = (parent != "") ? parent + "/" + conName : conName;
                string fileName = "/required.req";
                string filetext = "#REQUIRED: At least one file is required to be present in this folder.";
                con = client.GetContainerReference(folders[0]);
                CloudBlob f = con.GetBlobReference(conName + fileName);
                f.UploadText(filetext);
                CloudBlobDirectory d = con.GetDirectoryReference(conName);

                // Cast to our object
                container = new DiscountBlobContainer {
                    Container = d.Container,
                    BlobCount = d.ListBlobs().Count(),
                    uri = d.Uri,
                    SubContainers = GetSubContainers(conName)
                };

            }else{
                // Retrieve a reference to a container
                con = client.GetContainerReference(conName);

                // Create the container if it doesn't already exist
                con.CreateIfNotExist();

                // Cast to our object
                container = new DiscountBlobContainer {
                    Container = client.GetContainerReference(conName),
                    BlobCount = 0,
                    uri = client.GetContainerReference(conName).Uri,
                    SubContainers = GetSubContainers(conName)
                };
            }

            if (make_public) { // Make access to this container public
                con.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
            }

            return container;
        }
Ejemplo n.º 3
0
 public static void log(string message = "")
 {
     Settings settings = new Settings();
     string loggingstring = settings.Get("logging");
     bool isLoggingEnabled = (loggingstring.ToLower().Trim() != "true") ? false : true;
     if (isLoggingEnabled) {
         DiscountBlobContainer logblobs = new DiscountBlobContainer();
         logblobs = BlobManagement.GetContainer("logs");
         BlobContainerPermissions perms = new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob };
         logblobs.Container.SetPermissions(perms);
         CloudBlob blob = logblobs.Container.GetBlobReference("log-" + String.Format("{0:MM-dd-yyyy}", DateTime.Now) + ".txt");
         string blobtext = "";
         try {
             blobtext = blob.DownloadText();
         } catch { };
         blob.UploadText(blobtext + String.Format("{0:M-d-yyyy HH:mm:ss.fff: }", DateTime.Now) + message + Environment.NewLine);
     }
 }
Ejemplo n.º 4
0
        internal static DiscountBlobContainer RenameContainer(string old_name, string new_name)
        {
            if (old_name == null || old_name.Length == 0) {
                throw new Exception("Invalid reference to existing name.");
            }
            if (new_name == null || new_name.Length == 0) {
                throw new Exception("No new name specified.");
            }

            // Retrieve storage account from connection string
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));

            // Create blob client
            CloudBlobClient client = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a previously created container
            CloudBlobContainer oldContainer = client.GetContainerReference(old_name);

            // Create and retrieve reference to new container
            CloudBlobContainer newContainer = client.GetContainerReference(new_name);
            newContainer.CreateIfNotExist();
            newContainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

            foreach (var blob in oldContainer.ListBlobs()) {

                if (blob.GetType().ToString().ToUpper() == "CLOUDBLOBDIRECTORY") {
                    CloudBlobDirectory oldDir = (CloudBlobDirectory)blob;

                    // Get the name of the directory
                    string dirName = oldDir.Container.Name;
                } else {

                    CloudBlob oldBlob = (CloudBlob)blob;

                    // Get the filename of the existing blob
                    string filename = Path.GetFileName(blob.Uri.ToString());

                    // Create blob reference for the new container using the existing blob's filename
                    CloudBlob newBlob = newContainer.GetBlobReference(filename);

                    // Copy old blob to new blob
                    newBlob.CopyFromBlob(oldBlob);

                    // Delete old Blob
                    oldBlob.DeleteIfExists();
                }
            }

            // Delete old container
            oldContainer.Delete();

            DiscountBlobContainer con = new DiscountBlobContainer {
                Container = newContainer,
                uri = newContainer.Uri,
                BlobCount = newContainer.ListBlobs().Count()
            };
            return con;
        }
Ejemplo n.º 5
0
        internal static DiscountBlobContainer GetFolderForSerialization(string name)
        {
            if (name == null || name.Length == 0) {
                return new DiscountBlobContainer {
                    Container = null,
                    BlobCount = 0
                };
            }
            // Retrieve storage account from connection string
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));

            // Create the blobl client
            CloudBlobClient client = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a previously created container
            CloudBlobDirectory con = client.GetBlobDirectoryReference(name);

            DiscountBlobContainer container = new DiscountBlobContainer {
                Container = null,
                parent = null,
                BlobCount = (con.ListBlobs() == null) ? 0 : con.ListBlobs().Count(),
                uri = con.Uri,
                SubContainers = GetSubContainers(name)
            };
            return container;
        }
Ejemplo n.º 6
0
        internal static List<DiscountBlobContainer> GetContainers()
        {
            try {
                // Retrieve storage account from connection string
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));

                // Create the blobl client
                CloudBlobClient client = storageAccount.CreateCloudBlobClient();

                List<CloudBlobContainer> cloudContainers = new List<CloudBlobContainer>();
                List<DiscountBlobContainer> containers = new List<DiscountBlobContainer>();
                cloudContainers = client.ListContainers().ToList<CloudBlobContainer>();

                foreach (CloudBlobContainer con in cloudContainers) {
                    List<IListBlobItem> blobs = GetBlobs(con.Name);
                    List<BlobFile> files = new List<BlobFile>();
                    DiscountBlobContainer discountContainer = new DiscountBlobContainer {
                        BlobCount = blobs.Count,
                        Container = con,
                        uri = con.Uri,
                        SubContainers = GetSubContainers(con.Name)
                    };
                    foreach (IListBlobItem blob in blobs) {
                        if (!blob.GetType().Equals(typeof(CloudBlobDirectory))) {
                            BlobFile bf = new BlobFile {
                                uri = blob.Uri
                            };
                            files.Add(bf);
                        }
                    }
                    discountContainer.files = files;
                    containers.Add(discountContainer);
                }

                return containers;
            } catch (Exception) {
                return new List<DiscountBlobContainer>();
            }
        }
Ejemplo n.º 7
0
        internal static DiscountBlobContainer GetContainer(string name)
        {
            if (name == null || name.Length == 0) {
                return new DiscountBlobContainer {
                    Container = null,
                    BlobCount = 0
                };
            }
            // Retrieve storage account from connection string
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));

            // Create the blobl client
            CloudBlobClient client = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a previously created container
            CloudBlobContainer con = client.GetContainerReference(name);
            con.CreateIfNotExist();

            DiscountBlobContainer container = new DiscountBlobContainer {
                Container = con,
                BlobCount = GetBlobs(con.Name).Count,
                uri = con.Uri,
                SubContainers = GetSubContainers(con.Name)
            };
            return container;
        }