Пример #1
0
        public void Errors()
        {
            // Get a connection string to our Azure Storage account.
            string connectionString = ConnectionString;

            // Get a reference to a share named "sample-share" and then create it
            ShareClient share = new ShareClient(connectionString, Randomize("sample-share"));

            share.Create();

            try
            {
                // Try to create the share again
                share.Create();
            }
            catch (StorageRequestFailedException ex)
                when(ex.ErrorCode == FileErrorCode.ShareAlreadyExists)
                {
                    // Ignore any errors if the share already exists
                }
            catch (StorageRequestFailedException ex)
            {
                Assert.Fail($"Unexpected error: {ex}");
            }

            // Clean up after the test when we're finished
            share.Delete();
        }
        public async Task <string> SaveFileUri(string fileShare, string fileName, string fileContent)
        {
            ShareClient share = new ShareClient(connectionString, fileShare);

            if (!share.Exists())
            {
                share.Create();
            }
            ShareDirectoryClient rootDir = share.GetRootDirectoryClient();
            ShareFileClient      file    = rootDir.GetFileClient(fileName);

            if (!file.Exists())
            {
                file.Create(fileContent.Length);
            }

            byte[] outBuff = Encoding.ASCII.GetBytes(fileContent);
            ShareFileOpenWriteOptions options = new ShareFileOpenWriteOptions {
                MaxSize = outBuff.Length
            };
            var stream = await file.OpenWriteAsync(true, 0, options);

            stream.Write(outBuff, 0, outBuff.Length);
            stream.Flush();
            return(file.Uri.ToString());
        }
        public IActionResult DownloadFile(string userId, string fileName)
        {
            // Get a reference to a share and then create it
            ShareClient share = new ShareClient(Secret.AzureConnectionString, Secret.AzureFileShareName);

            if (!share.Exists())
            {
                share.Create();
            }

            // Get a reference to a directory and create it
            ShareDirectoryClient directory = share.GetDirectoryClient(Auth0Utils.GetUserFolder());

            if (!directory.Exists())
            {
                directory.Create();
            }

            // Get a reference to a file and upload it
            ShareFileClient fileClient = directory.GetFileClient(fileName);

            if (fileClient.Exists())
            {
                //ShareFileDownloadInfo download = file.Download();
                return(File(fileClient.OpenRead(), "application/octet-stream"));
            }
            else
            {
                return(NotFound());
            }
        }
        public IActionResult DeleteFile(string userId, string fileName)
        {
            // Get a reference to a share and then create it
            ShareClient share = new ShareClient(Secret.AzureConnectionString, Secret.AzureFileShareName);

            if (!share.Exists())
            {
                share.Create();
            }

            // Get a reference to a directory and create it
            ShareDirectoryClient directory = share.GetDirectoryClient(Auth0Utils.GetUserFolder());

            if (!directory.Exists())
            {
                directory.Create();
            }

            // Get a reference to a file and upload it
            ShareFileClient fileClient = directory.GetFileClient(fileName);

            fileClient.DeleteIfExists();

            return(Ok());
        }
Пример #5
0
        private ShareFileClient StorageSetup(IFormFile loadedFile, string fileType)
        {
            string dirName  = fileType;
            string fileName = loadedFile.FileName;

            ShareClient share = new ShareClient(_storagekey, _storageshare);

            try
            {
                // Try to create the share again
                share.Create();
            }
            catch (RequestFailedException ex)
                when(ex.ErrorCode == ShareErrorCode.ShareAlreadyExists)
                {
                    // Ignore any errors if the share already exists
                }
            ShareDirectoryClient directory = share.GetDirectoryClient(dirName);

            try
            {
                // Try to create the share again
                directory.Create();
            }
            catch (RequestFailedException ex)
            {
                // Ignore any errors if the share already exists
            }
            ShareFileClient shareFile = directory.GetFileClient(fileName);

            return(shareFile);
        }
        public virtual Response <ShareClient> CreateShare(
            string shareName,
            IDictionary <string, string> metadata = default,
            int?quotaInGB = default,
            CancellationToken cancellationToken = default)
        {
            ShareClient          share    = GetShareClient(shareName);
            Response <ShareInfo> response = share.Create(metadata, quotaInGB, cancellationToken);

            return(Response.FromValue(share, response.GetRawResponse()));
        }
Пример #7
0
        public void Download()
        {
            // Create a temporary Lorem Ipsum file on disk that we can upload
            string originalPath = CreateTempFile(SampleFileContent);

            // Get a temporary path on disk where we can download the file
            string downloadPath = CreateTempPath();

            // Get a connection string to our Azure Storage account.
            string connectionString = ConnectionString;

            // Get a reference to a share named "sample-share" and then create it
            ShareClient share = new ShareClient(connectionString, Randomize("sample-share"));

            share.Create();
            try
            {
                // Get a reference to a directory named "sample-dir" and then create it
                DirectoryClient directory = share.GetDirectoryClient(Randomize("sample-dir"));
                directory.Create();

                // Get a reference to a file named "sample-file" in directory "sample-dir"
                FileClient file = directory.GetFileClient(Randomize("sample-file"));

                // Upload the file
                using (FileStream stream = File.OpenRead(originalPath))
                {
                    file.Create(stream.Length);
                    file.UploadRange(
                        FileRangeWriteType.Update,
                        new HttpRange(0, stream.Length),
                        stream);
                }

                // Download the file
                StorageFileDownloadInfo download = file.Download();
                using (FileStream stream = File.OpenWrite(downloadPath))
                {
                    download.Content.CopyTo(stream);
                }

                // Verify the contents
                Assert.AreEqual(SampleFileContent, File.ReadAllText(downloadPath));
            }
            finally
            {
                // Clean up after the test when we're finished
                share.Delete();
            }
        }
Пример #8
0
        public void Upload()
        {
            // Create a temporary Lorem Ipsum file on disk that we can upload
            string path = CreateTempFile(SampleFileContent);

            // Get a connection string to our Azure Storage account.  You can
            // obtain your connection string from the Azure Portal (click
            // Access Keys under Settings in the Portal Storage account blade)
            // or using the Azure CLI with:
            //
            //     az storage account show-connection-string --name <account_name> --resource-group <resource_group>
            //
            // And you can provide the connection string to your application
            // using an environment variable.
            string connectionString = ConnectionString;

            // Get a reference to a share named "sample-share" and then create it
            ShareClient share = new ShareClient(connectionString, Randomize("sample-share"));

            share.Create();
            try
            {
                // Get a reference to a directory named "sample-dir" and then create it
                DirectoryClient directory = share.GetDirectoryClient(Randomize("sample-dir"));
                directory.Create();

                // Get a reference to a file named "sample-file" in directory "sample-dir"
                FileClient file = directory.GetFileClient(Randomize("sample-file"));

                // Upload the file
                using (FileStream stream = File.OpenRead(path))
                {
                    file.Create(stream.Length);
                    file.UploadRange(
                        FileRangeWriteType.Update,
                        new HttpRange(0, stream.Length),
                        stream);
                }

                // Verify the file exists
                StorageFileProperties properties = file.GetProperties();
                Assert.AreEqual(SampleFileContent.Length, properties.ContentLength);
            }
            finally
            {
                // Clean up after the test when we're finished
                share.Delete();
            }
        }
        public IActionResult Post(string userId)
        {
            try
            {
                IFormFileCollection files = Request.Form.Files;
                if (files.Count == 0)
                {
                    return(BadRequest());
                }

                // Get a reference to a share and then create it
                ShareClient share = new ShareClient(Secret.AzureConnectionString, Secret.AzureFileShareName);
                if (!share.Exists())
                {
                    share.Create();
                }

                // Get a reference to a directory and create it
                ShareDirectoryClient directory = share.GetDirectoryClient(Auth0Utils.GetUserFolder());
                if (!directory.Exists())
                {
                    directory.Create();
                }

                // Get a reference to a file and upload it
                ShareFileClient fileClient;

                foreach (IFormFile file in files)
                {
                    fileClient = directory.GetFileClient(file.FileName);

                    fileClient.DeleteIfExists();

                    using (Stream stream = file.OpenReadStream())
                    {
                        fileClient.Create(stream.Length);
                        fileClient.UploadRange(
                            new HttpRange(0, stream.Length),
                            stream);
                    }
                }

                return(Ok());
            }
            catch (Exception e)
            {
                return(StatusCode(500, e.Message));
            }
        }
Пример #10
0
        public void Sample01a_HelloWorld_Errors()
        {
            // Get a reference to a share named "sample-share" and then create it
            string      shareName = Randomize("sample-share");
            ShareClient share     = new ShareClient(ConnectionString, shareName);

            try
            {
                share.Create();
                Sample01a_HelloWorld.Errors(ConnectionString, shareName);
            }
            finally
            {
                share.Delete();
            }
        }
        /// <summary>
        /// Create a share and upload a file.
        /// </summary>
        /// <param name="connectionString">
        /// A connection string to your Azure Storage account.
        /// </param>
        /// <param name="shareName">
        /// The name of the share to create and upload to.
        /// </param>
        /// <param name="localFilePath">
        /// Path of the file to upload.
        /// </param>
        public static void Upload(string connectionString, string shareName, string localFilePath)
        {
            #region Snippet:Azure_Storage_Files_Shares_Samples_Sample01a_HelloWorld_Upload
            // Get a connection string to our Azure Storage account.  You can
            // obtain your connection string from the Azure Portal (click
            // Access Keys under Settings in the Portal Storage account blade)
            // or using the Azure CLI with:
            //
            //     az storage account show-connection-string --name <account_name> --resource-group <resource_group>
            //
            // And you can provide the connection string to your application
            // using an environment variable.
            //@@ string connectionString = "<connection_string>";

            // Name of the share, directory, and file we'll create
            //@@ string shareName = "sample-share";
            string dirName  = "sample-dir";
            string fileName = "sample-file";

            // Path to the local file to upload
            //@@ string localFilePath = @"<path_to_local_file>";

            // Get a reference to a share and then create it
            ShareClient share = new ShareClient(connectionString, shareName);
            share.Create();

            // Get a reference to a directory and create it
            ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
            directory.Create();

            // Get a reference to a file and upload it
            ShareFileClient file = directory.GetFileClient(fileName);
            using (FileStream stream = File.OpenRead(localFilePath))
            {
                file.Create(stream.Length);
                file.UploadRange(
                    ShareFileRangeWriteType.Update,
                    new HttpRange(0, stream.Length),
                    stream);
            }
            #endregion Snippet:Azure_Storage_Files_Shares_Samples_Sample01a_HelloWorld_Upload
        }
        /// <summary>
        /// Trigger a recoverable error.
        /// </summary>
        /// <param name="connectionString">
        /// A connection string to your Azure Storage account.
        /// </param>
        /// <param name="shareName">
        /// The name of an existing share
        /// </param>
        public static void Errors(string connectionString, string shareName)
        {
            #region Snippet:Azure_Storage_Files_Shares_Samples_Sample01a_HelloWorld_Errors
            // Connect to the existing share
            //@@ string connectionString = "<connection_string>";
            //@@ string shareName = "sample-share";
            ShareClient share = new ShareClient(connectionString, shareName);

            try
            {
                // Try to create the share again
                share.Create();
            }
            catch (RequestFailedException ex)
                when(ex.ErrorCode == ShareErrorCode.ShareAlreadyExists)
                {
                    // Ignore any errors if the share already exists
                }
            #endregion Snippet:Azure_Storage_Files_Shares_Samples_Sample01a_HelloWorld_Errors
        }
Пример #13
0
        public void Sample01a_HelloWorld_Traverse()
        {
            // Create a temporary Lorem Ipsum file on disk that we can upload
            string originalPath = CreateTempFile(SampleFileContent);

            // Get a reference to a share named "sample-share" and then create it
            string      shareName = Randomize("sample-share");
            ShareClient share     = new ShareClient(ConnectionString, shareName);

            share.Create();
            try
            {
                // Create a bunch of directories
                ShareDirectoryClient first = share.CreateDirectory("first");
                first.CreateSubdirectory("a");
                first.CreateSubdirectory("b");
                ShareDirectoryClient second = share.CreateDirectory("second");
                second.CreateSubdirectory("c");
                second.CreateSubdirectory("d");
                share.CreateDirectory("third");
                ShareDirectoryClient fourth  = share.CreateDirectory("fourth");
                ShareDirectoryClient deepest = fourth.CreateSubdirectory("e");

                // Upload a file named "file"
                ShareFileClient file = deepest.GetFileClient("file");
                using (FileStream stream = File.OpenRead(originalPath))
                {
                    file.Create(stream.Length);
                    file.UploadRange(
                        ShareFileRangeWriteType.Update,
                        new HttpRange(0, stream.Length),
                        stream);
                }

                Sample01a_HelloWorld.Traverse(ConnectionString, shareName);
            }
            finally
            {
                share.Delete();
            }
        }
Пример #14
0
        public void Sample01a_HelloWorld_Download()
        {
            string      originalPath  = CreateTempFile(SampleFileContent);
            string      localFilePath = CreateTempPath();
            string      shareName     = Randomize("sample-share");
            ShareClient share         = new ShareClient(ConnectionString, shareName);

            try
            {
                share.Create();

                // Get a reference to a directory named "sample-dir" and then create it
                ShareDirectoryClient directory = share.GetDirectoryClient("sample-dir");
                directory.Create();

                // Get a reference to a file named "sample-file" in directory "sample-dir"
                ShareFileClient file = directory.GetFileClient("sample-file");

                // Upload the file
                using (FileStream stream = File.OpenRead(originalPath))
                {
                    file.Create(stream.Length);
                    file.UploadRange(
                        ShareFileRangeWriteType.Update,
                        new HttpRange(0, stream.Length),
                        stream);
                }

                Sample01a_HelloWorld.Download(ConnectionString, shareName, localFilePath);

                // Verify the contents
                Assert.AreEqual(SampleFileContent, File.ReadAllText(localFilePath));
            }
            finally
            {
                share.Delete();
                File.Delete(originalPath);
                File.Delete(localFilePath);
            }
        }
        public IActionResult GetFileNames(string userId)
        {
            // Get a reference to a share and then create it
            ShareClient share = new ShareClient(Secret.AzureConnectionString, Secret.AzureFileShareName);

            if (!share.Exists())
            {
                share.Create();
            }

            // Get a reference to a directory and create it
            ShareDirectoryClient directory = share.GetDirectoryClient(Auth0Utils.GetUserFolder());

            if (!directory.Exists())
            {
                directory.Create();
            }

            var files = directory.GetFilesAndDirectories();

            return(Ok(files.ToList()));
        }
Пример #16
0
        public void Traverse()
        {
            // Create a temporary Lorem Ipsum file on disk that we can upload
            string originalPath = CreateTempFile(SampleFileContent);

            // Get a connection string to our Azure Storage account.
            string connectionString = ConnectionString;

            // Get a reference to a share named "sample-share" and then create it
            ShareClient share = new ShareClient(connectionString, Randomize("sample-share"));

            share.Create();
            try
            {
                // Create a bunch of directories
                DirectoryClient first = share.CreateDirectory("first");
                first.CreateSubdirectory("a");
                first.CreateSubdirectory("b");
                DirectoryClient second = share.CreateDirectory("second");
                second.CreateSubdirectory("c");
                second.CreateSubdirectory("d");
                share.CreateDirectory("third");
                DirectoryClient fourth  = share.CreateDirectory("fourth");
                DirectoryClient deepest = fourth.CreateSubdirectory("e");

                // Upload a file named "file"
                FileClient file = deepest.GetFileClient("file");
                using (FileStream stream = File.OpenRead(originalPath))
                {
                    file.Create(stream.Length);
                    file.UploadRange(
                        FileRangeWriteType.Update,
                        new HttpRange(0, stream.Length),
                        stream);
                }

                // Keep track of all the names we encounter
                List <string> names = new List <string>();

                // Track the remaining directories to walk, starting from the root
                Queue <DirectoryClient> remaining = new Queue <DirectoryClient>();
                remaining.Enqueue(share.GetRootDirectoryClient());
                while (remaining.Count > 0)
                {
                    // Get all of the next directory's files and subdirectories
                    DirectoryClient dir = remaining.Dequeue();
                    foreach (StorageFileItem item in dir.GetFilesAndDirectories())
                    {
                        // Track the name of the item
                        names.Add(item.Name);

                        // Keep walking down directories
                        if (item.IsDirectory)
                        {
                            remaining.Enqueue(dir.GetSubdirectoryClient(item.Name));
                        }
                    }
                }

                // Verify we've seen everything
                Assert.AreEqual(10, names.Count);
                Assert.Contains("first", names);
                Assert.Contains("second", names);
                Assert.Contains("third", names);
                Assert.Contains("fourth", names);
                Assert.Contains("a", names);
                Assert.Contains("b", names);
                Assert.Contains("c", names);
                Assert.Contains("d", names);
                Assert.Contains("e", names);
                Assert.Contains("file", names);
            }
            finally
            {
                // Clean up after the test when we're finished
                share.Delete();
            }
        }