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());
        }
Exemplo n.º 3
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 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));
            }
        }
        /// <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
        }
Exemplo n.º 6
0
        /// <summary>
        /// Make a directory (changes to it and makes it the current directory)
        /// </summary>
        /// <param name="directory">The name of the new directory (which is made current)</param>
        /// <returns>True if successful. False if not. Any exceptions are thrown.</returns>
        public bool MakeDirectory(string directory)
        {
            if (FileShare == null)
            {
                throw new Exception("Cannot make Azure directory: " + directory + " (null share).");
            }

            // Get a reference to the directory.
            Directory = FileShare.GetDirectoryClient(directory);

            // Ensure that the directory exists.
            if (Directory.Exists())
            {
                throw new Exception("Cannot make Azure directory: " + directory + " - it aleady exists.");
            }
            else
            {
                Directory.Create();
            }
            return(true);
        }
        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()));
        }