Exemplo n.º 1
0
        // </snippet_RestoreFileFromSnapshot>

        // <snippet_DeleteSnapshot>
        //-------------------------------------------------
        // Delete a snapshot
        //-------------------------------------------------
        public async Task DeleteSnapshotAsync(string shareName, string snapshotTime)
        {
            // Get the connection string from app settings
            string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];

            // Instatiate a ShareServiceClient
            ShareServiceClient shareService = new ShareServiceClient(connectionString);

            // Get a ShareClient
            ShareClient share = shareService.GetShareClient(shareName);

            // Get a ShareClient that points to a snapshot
            ShareClient snapshotShare = share.WithSnapshot(snapshotTime);

            try
            {
                // Delete the snapshot
                await snapshotShare.DeleteIfExistsAsync();
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine($"Exception: {ex.Message}");
                Console.WriteLine($"Error code: {ex.Status}\t{ex.ErrorCode}");
            }
        }
        /// <summary>
        /// Manage share metadata
        /// </summary>
        /// <param name="shareServiceClient"></param>
        /// <returns></returns>
        private static async Task ShareMetadataSample(ShareServiceClient shareServiceClient)
        {
            Console.WriteLine();

            // Create the share name -- use a guid in the name so it's unique.
            string shareName = "demotest-" + Guid.NewGuid();

            // Create Share
            Console.WriteLine("Create Share");

            ShareClient shareClient = shareServiceClient.GetShareClient(shareName);
            await shareClient.CreateIfNotExistsAsync();

            // Set share metadata
            Console.WriteLine("Set share metadata");

            var metadata = new Dictionary <string, string> {
                { "key1", "value1" }, { "key2", "value2" }
            };
            await shareClient.SetMetadataAsync(metadata);

            try
            {
                // Create Share
                Console.WriteLine("Create Share");
                await shareClient.CreateIfNotExistsAsync();

                // Fetch share attributes
                ShareProperties shareProperties = await shareClient.GetPropertiesAsync();

                Console.WriteLine("Get share metadata:");
                foreach (var keyValue in shareProperties.Metadata)
                {
                    Console.WriteLine("    {0}: {1}", keyValue.Key, keyValue.Value);
                }
                Console.WriteLine();
            }
            catch (RequestFailedException exRequest)
            {
                Common.WriteException(exRequest);
                Console.WriteLine(
                    "Please make sure your storage account is specified correctly in the app.config - then restart the sample.");
                Console.WriteLine("Press any key to exit");
                Console.ReadLine();
                throw;
            }
            catch (Exception ex)
            {
                Console.WriteLine("    Exception thrown creating share.");
                Common.WriteException(ex);
                throw;
            }
            finally
            {
                // Delete share
                Console.WriteLine("Delete share");
                await shareClient.DeleteIfExistsAsync();
            }
        }
        /// <summary>
        /// Manage share properties
        /// </summary>
        /// <param name="shareServiceClient"></param>
        /// <returns></returns>
        private static async Task SharePropertiesSample(ShareServiceClient shareServiceClient)
        {
            Console.WriteLine();

            // Create the share name -- use a guid in the name so it's unique.
            string      shareName   = "demotest-" + Guid.NewGuid();
            ShareClient shareClient = shareServiceClient.GetShareClient(shareName);

            try
            {
                // Create Share
                Console.WriteLine("Create Share");
                await shareClient.CreateIfNotExistsAsync();

                // Set share properties
                Console.WriteLine("Set share properties");
                await shareClient.SetQuotaAsync(100);

                // Fetch share properties
                ShareProperties shareProperties = await shareClient.GetPropertiesAsync();

                Console.WriteLine("Get share properties:");
                Console.WriteLine("    Quota: {0}", shareProperties.QuotaInGB);
                Console.WriteLine("    ETag: {0}", shareProperties.ETag);
                Console.WriteLine("    Last modified: {0}", shareProperties.LastModified);
            }
            catch (RequestFailedException exRequest)
            {
                Common.WriteException(exRequest);
                Console.WriteLine(
                    "Please make sure your storage account is specified correctly in the app.config - then restart the sample.");
                Console.WriteLine("Press any key to exit");
                Console.ReadLine();
                throw;
            }
            catch (Exception ex)
            {
                Console.WriteLine("    Exception thrown creating share.");
                Common.WriteException(ex);
                throw;
            }
            finally
            {
                // Delete share
                Console.WriteLine("Delete share");
                await shareClient.DeleteIfExistsAsync();
            }

            Console.WriteLine();
        }
        // </snippet_RestoreFileFromSnapshot>
        //Uri snapshotFileUri = GetFileSasUri(shareName, snapshotDir.Name + "/" + snapshotFile.Name, DateTime.UtcNow.AddHours(24), ShareFileSasPermissions.Read);

        // <snippet_DeleteSnapshot>
        //-------------------------------------------------
        // Delete a snapshot
        //-------------------------------------------------
        public async Task DeleteSnapshotAsync(string shareName, string snapshotTime)
        {
            // Get the connection string from app settings
            string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];

            // Instatiate a ShareServiceClient
            ShareServiceClient shareService = new ShareServiceClient(connectionString);

            // Get a ShareClient
            ShareClient share = shareService.GetShareClient(shareName);

            // Get as ShareClient that points to a snapshot
            ShareClient snapshot = share.WithSnapshot(snapshotTime);

            // This deletes the whole share, not just the snapshot
            await snapshot.DeleteIfExistsAsync();
        }
        public override async Task CleanupAsync()
        {
            await _shareClient.DeleteIfExistsAsync();

            await base.CleanupAsync();
        }
        /// <summary>
        /// Manage file properties
        /// </summary>
        /// <param name="shareServiceClient"></param>
        /// <returns></returns>
        private static async Task FilePropertiesSample(ShareServiceClient shareServiceClient)
        {
            Console.WriteLine();

            // Create the share name -- use a guid in the name so it's unique.
            string      shareName   = "demotest-" + Guid.NewGuid();
            ShareClient shareClient = shareServiceClient.GetShareClient(shareName);

            try
            {
                // Create share
                Console.WriteLine("Create Share");
                await shareClient.CreateIfNotExistsAsync();

                // Create directory
                Console.WriteLine("Create directory");
                ShareDirectoryClient rootDirectory = shareClient.GetRootDirectoryClient();

                ShareFileClient file = rootDirectory.GetFileClient("demofile");

                // Create file
                Console.WriteLine("Create file");
                await file.CreateAsync(1000);

                // Set file properties
                var headers = new ShareFileHttpHeaders
                {
                    ContentType     = "plain/text",
                    ContentEncoding = new string[] { "UTF-8" },
                    ContentLanguage = new string[] { "en" }
                };

                await file.SetHttpHeadersAsync(httpHeaders : headers);

                // Fetch file attributes
                ShareFileProperties shareFileProperties = await file.GetPropertiesAsync();

                Console.WriteLine("Get file properties:");
                Console.WriteLine("    Content type: {0}", shareFileProperties.ContentType);
                Console.WriteLine("    Content encoding: {0}", string.Join("", shareFileProperties.ContentEncoding));
                Console.WriteLine("    Content language: {0}", string.Join("", shareFileProperties.ContentLanguage));
                Console.WriteLine("    Length: {0}", shareFileProperties.ContentLength);
            }
            catch (RequestFailedException exRequest)
            {
                Common.WriteException(exRequest);
                Console.WriteLine(
                    "Please make sure your storage account is specified correctly in the app.config - then restart the sample.");
                Console.WriteLine("Press any key to exit");
                Console.ReadLine();
                throw;
            }
            catch (Exception ex)
            {
                Console.WriteLine("    Exception thrown creating share.");
                Common.WriteException(ex);
                throw;
            }
            finally
            {
                // Delete share
                Console.WriteLine("Delete share");
                await shareClient.DeleteIfExistsAsync();
            }

            Console.WriteLine();
        }