Example #1
0
        // ************************************
        // Create a new Blob Container
        // ************************************
        public void CreateContainer(string ContainerName, bool isPublic,
                                    string BlobEndPoint, string Account, string SharedKey)
        {
            StorageAccountInfo AccountInfo =
                new StorageAccountInfo(new Uri(BlobEndPoint),
                                       null, Account, SharedKey);

            var blobStore = BlobStorage.Create(AccountInfo);

            ContainerAccessControl accessControl = isPublic ?
                                                   ContainerAccessControl.Public : ContainerAccessControl.Private;

            blobStore.GetBlobContainer(ContainerName).CreateContainer(null, accessControl);
        }
        public void SetContainerACL(string containerName, ContainerAccessControl accessType)
        {
            BlobContainer container = BlobStorageType.GetBlobContainer(containerName);

            container.SetContainerAccessControl(accessType);
        }
        public bool CreateContainer(string containerName, ContainerAccessControl accessType, NameValueCollection metadata)
        {
            BlobContainer container = BlobStorageType.GetBlobContainer(containerName);

            return(container.CreateContainer(metadata, accessType));
        }
Example #4
0
 public abstract void SetContainerAccessControl(ContainerAccessControl acl);
Example #5
0
 /// <summary>
 /// Create the container with the specified metadata and access control if it does not exist
 /// </summary>
 /// <param name="metadata">The metadata for the container. Can be null to indicate no metadata</param>
 /// <param name="accessControl">The access control (public or private) with which to create the container</param>
 /// <returns>true if the container was created. false if the container already exists</returns>
 public abstract bool CreateContainer(NameValueCollection metadata, ContainerAccessControl accessControl);
Example #6
0
 public abstract void SetContainerAccessControl(ContainerAccessControl acl);
Example #7
0
 /// <summary>
 /// Create the container with the specified metadata and access control if it does not exist
 /// </summary>
 /// <param name="metadata">The metadata for the container. Can be null to indicate no metadata</param>
 /// <param name="accessControl">The access control (public or private) with which to create the container</param>
 /// <returns>true if the container was created. false if the container already exists</returns>
 public abstract bool CreateContainer(NameValueCollection metadata, ContainerAccessControl accessControl); 
Example #8
0
        private bool CreateContainerImpl(NameValueCollection metadata, ContainerAccessControl accessControl)
        {
            bool result = false;
            RetryPolicy(() =>
            {
                ResourceUriComponents uriComponents;
                Uri uri = Utilities.CreateRequestUri(BaseUri, UsePathStyleUris, AccountName, ContainerName, null, Timeout, new NameValueCollection(),
                                  out uriComponents);
                HttpWebRequest request = Utilities.CreateHttpRequest(uri, StorageHttpConstants.HttpMethod.Put, Timeout);
                if (metadata != null)
                {
                    Utilities.AddMetadataHeaders(request, metadata);
                }
                if (accessControl == ContainerAccessControl.Public)
                {
                    request.Headers.Add(StorageHttpConstants.HeaderNames.PublicAccess, "true");
                }
                credentials.SignRequest(request, uriComponents);

                try
                {
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        if (response.StatusCode == HttpStatusCode.Created)
                            result = true;
                        else if (response.StatusCode == HttpStatusCode.Conflict)
                            result = false;
                        else
                            Utilities.ProcessUnexpectedStatusCode(response);
                        response.Close();
                    }
                }
                catch (WebException we)
                {
                    if (we.Response != null && ((HttpWebResponse)we.Response).StatusCode == HttpStatusCode.Conflict)
                        result = false;
                    else
                        throw Utilities.TranslateWebException(we);
                }
            });
            return result;
        }
Example #9
0
        /// <summary>
        /// Get the access control permissions associated with the container.
        /// </summary>
        /// <returns></returns>
        public override void SetContainerAccessControl(ContainerAccessControl acl)
        {
            RetryPolicy(() =>
            {
                ResourceUriComponents uriComponents;
                NameValueCollection queryParams = new NameValueCollection();
                queryParams.Add(StorageHttpConstants.QueryParams.QueryParamComp, StorageHttpConstants.CompConstants.Acl);

                Uri uri = Utilities.CreateRequestUri(BaseUri, UsePathStyleUris, AccountName, ContainerName, null, Timeout,
                            queryParams, out uriComponents);
                HttpWebRequest request = Utilities.CreateHttpRequest(uri, StorageHttpConstants.HttpMethod.Put, Timeout);
                request.Headers.Add(StorageHttpConstants.HeaderNames.PublicAccess,
                    (acl == ContainerAccessControl.Public).ToString());
                credentials.SignRequest(request, uriComponents);

                try
                {
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            Utilities.ProcessUnexpectedStatusCode(response);
                        }
                        response.Close();
                    }
                }
                catch (WebException we)
                {
                    throw Utilities.TranslateWebException(we);
                }

            });
        }
Example #10
0
 /// <summary>
 /// Create the container with the specified access control if it does not exist
 /// </summary>
 /// <param name="metadata">The metadata for the container. Can be null to indicate no metadata</param>
 /// <param name="accessControl">The access control (public or private) with which to create the container</param>
 /// <returns>true if the container was created. false if the container already exists</returns>
 public override bool CreateContainer(NameValueCollection metadata, ContainerAccessControl accessControl)
 {
     return CreateContainerImpl(metadata, accessControl);
 }
Example #11
0
        internal static void RunSamples()
        {
            StorageAccountInfo account       = StorageAccountInfo.GetDefaultBlobStorageAccountFromConfiguration();
            string             containerName = StorageAccountInfo.GetConfigurationSetting("ContainerName", null, true);

            NameValueCollection containerMetadata = new NameValueCollection();

            containerMetadata.Add("Name", "StorageSample");

            BlobStorage blobStorage = BlobStorage.Create(account);

            blobStorage.RetryPolicy = RetryPolicies.RetryN(2, TimeSpan.FromMilliseconds(100));



            try
            {
                BlobContainer container = blobStorage.GetBlobContainer(containerName);

                //Create the container if it does not exist.
                container.CreateContainer(containerMetadata, ContainerAccessControl.Private);

                ContainerProperties containerProperties = container.GetContainerProperties();
                Console.WriteLine("Container {0} LastModified {1} ETag {2} Metadata {3}",
                                  containerProperties.Name,
                                  containerProperties.LastModifiedTime,
                                  containerProperties.ETag,
                                  StringBlob.MetadataToString(containerProperties.Metadata)
                                  );

                ContainerAccessControl acl = container.GetContainerAccessControl();
                Console.WriteLine("Container has access control {0}", acl);


                // write some text blobs
                NameValueCollection nv1 = new NameValueCollection();
                nv1["m1"] = "v1";
                nv1["m2"] = "v2";

                StringBlob hello1 = new StringBlob("hello.txt", "Hello World");
                hello1.Blob.Metadata = nv1;
                Console.WriteLine("Creating blob hello.txt");
                PutTextBlob(container, hello1);

                BlobProperties prop = container.GetBlobProperties("hello.txt");
                Console.WriteLine("hello.txt content length = " + prop.ContentLength);


                StringBlob goodbye1 = new StringBlob("goodbye.txt", "Goodbye world");
                Console.WriteLine("Creating blob goodbye.txt");
                PutTextBlob(container, goodbye1);

                // read back the blobs
                Console.WriteLine("Getting hello.txt and goodbye.txt");
                StringBlob hello2 = GetTextBlob(container, "hello.txt");
                Console.WriteLine("hello.txt: " + hello2.ToString());
                StringBlob goodbye2 = GetTextBlob(container, "goodbye.txt");
                Console.WriteLine("goodbye.txt " + goodbye2.ToString());


                //Try to get a blob that does not exist
                try
                {
                    GetTextBlob(container, "noSuchBlob");
                }
                catch (StorageClientException sce)
                {
                    //The extended error information when present provides more specific and detailed information
                    // about the cause of the error.
                    Console.WriteLine(
                        "Error attempting to get blob 'noSuchBlob' Error Code = {0} Message = {1}",
                        sce.ExtendedErrorInformation != null ?
                        sce.ExtendedErrorInformation.ErrorCode : sce.ErrorCode.ToString(),
                        sce.Message
                        );
                }

                //update metadata of hello.txt
                hello2.Blob.Metadata["m3"] = "v3";
                Console.WriteLine("Updating metadata of hello.txt");
                container.UpdateBlobMetadata(hello2.Blob);

                hello2.Blob.Metadata["m4"] = "v4";
                container.UpdateBlobMetadataIfNotModified(hello2.Blob);

                //Refresh hello.txt. It has changed.
                bool refreshed = RefreshTextBlob(container, hello1);
                if (refreshed)
                {
                    Console.WriteLine("hello.txt refreshed " + hello1.ToString());
                }
                else
                {
                    Console.WriteLine("hello.txt not refreshed");
                }

                Console.WriteLine("Uploading a large blob");
                PutLargeString(
                    container,
                    new StringBlob("LargeBlob.txt", "Let us repeat this string a large number of times "),
                    50000
                    );

                Console.WriteLine("Downloading large blob to file LargeBlob.txt");
                DownloadToFile(container, "LargeBlob.txt", "LargeBlob.txt");

                //Refresh hello.txt. It hasn't changed.
                refreshed = RefreshTextBlob(container, hello2);
                if (refreshed)
                {
                    Console.WriteLine("hello.txt refreshed " + hello2.ToString());
                }
                else
                {
                    Console.WriteLine("hello.txt not refreshed");
                }

                //Change goodbye.txt and refresh it
                StringBlob goodbye3 = new StringBlob("goodbye.txt", "Goodbye again world");
                PutTextBlob(container, goodbye3);

                //Now refresh the other reference to goodbye.txt (goodbye2)
                refreshed = RefreshTextBlob(container, goodbye2);
                if (refreshed)
                {
                    Console.WriteLine("goodbye.txt refreshed " + goodbye2.ToString());
                }
                else
                {
                    Console.WriteLine("goodbye.txt not refreshed");
                }


                //Update hello.txt
                hello2.Value = "Hello again world";
                bool updated = UpdateTextBlob(container, hello2);
                if (updated)
                {
                    Console.WriteLine("hello.txt updated " + hello2.ToString());
                }
                else
                {
                    Console.WriteLine("hello.txt not updated because it has been changed");
                }

                //Try to update goodbye.txt through goodbye1.
                //This should fail because it has been updated thru goodbye3
                goodbye1.Value = "Farewell world";
                updated        = UpdateTextBlob(container, goodbye1);
                if (updated)
                {
                    Console.WriteLine("goodbye.txt updated " + goodbye1.ToString());
                }
                else
                {
                    Console.WriteLine("goodbye.txt not updated because it has been changed");
                }

                Console.WriteLine("Creating blob 'deleteme.txt'");
                PutTextBlob(container, new StringBlob("deleteme.txt", "deleteme"));

                Console.WriteLine("Creating blobs f/a.txt and f/b.txt");
                PutTextBlob(container, new StringBlob("f/a.txt", "This is a.txt"));
                PutTextBlob(container, new StringBlob("f/b.txt", "This is b.txt"));

                Console.WriteLine("Enumerating all blobs");

                // enumerate all the blobs
                foreach (object b1 in container.ListBlobs("", false))
                {
                    Console.WriteLine("{0}", ((BlobProperties)b1).Uri);
                }

                Console.WriteLine("Enumerating all blobs with combining common prefixes");
                foreach (object b2 in container.ListBlobs("", true))
                {
                    BlobProperties blobProperties = b2 as BlobProperties;
                    if (blobProperties != null)
                    {
                        Console.WriteLine("{0}", blobProperties.Uri);
                    }
                    else
                    {
                        Console.WriteLine("Common prefix: {0}", (string)b2);
                    }
                }

                Console.WriteLine("Deleting blob 'deleteme.txt'");
                container.DeleteBlob("deleteme.txt");

                Console.WriteLine("Enumerate the blobs again");
                foreach (object b3 in container.ListBlobs("", false))
                {
                    Console.WriteLine("{0}", ((BlobProperties)b3).Uri);
                }

                // Create another container
                Console.WriteLine("Creating container 'deleteme'");
                BlobContainer container2 = blobStorage.GetBlobContainer("deleteme");
                container2.CreateContainer();


                // enumerate containers
                foreach (BlobContainer c in blobStorage.ListBlobContainers())
                {
                    Console.WriteLine("Container: {0}", c.ContainerUri);
                }

                // Delete the container
                Console.WriteLine("Deleting container 'deleteme'");
                container2.DeleteContainer();

                // enumerate containers
                foreach (BlobContainer c in blobStorage.ListBlobContainers())
                {
                    Console.WriteLine("Container: {0}", c.ContainerUri);
                }
            }
            catch (System.Net.WebException we)
            {
                Console.WriteLine("Network error: " + we.Message);
                if (we.Status == System.Net.WebExceptionStatus.ConnectFailure)
                {
                    Console.WriteLine("Please check if the blob storage service is running at " + account.BaseUri.ToString());
                    Console.WriteLine("Detailed information on how to run the development storage tool " +
                                      "locally can be found in the readme file that comes with this sample.");
                }
            }
            catch (StorageException se)
            {
                Console.WriteLine("Storage service error: " + se.Message);
            }
        }