robinrodricks / FluentStorage

A polycloud .NET cloud storage abstraction layer. Provides Blob storage (AWS S3, GCP, FTP, SFTP, Azure Blob/File/Event Hub/Data Lake) and Messaging (AWS SQS, Azure Queue/ServiceBus). Supports .NET 5+ and .NET Standard 2.0+. Pure C#.
MIT License
263 stars 33 forks source link

remove .empty #30

Closed ebrahimkhodadadi closed 11 months ago

ebrahimkhodadadi commented 1 year ago

after CreateFolderAsync why .empty create Automatically is it necessary? how skip to create it?

dammitjanet commented 11 months ago

it is created because the Blob storage provider you are using is not one that supports IHierarchicalBlobStorage so the only way to create a folder, is to put an empty file in it, otherwise the folder won't exist.

You can name it something different as the method signature support providing a different name but I've found that in cases where the storage provider doesn't actually support IHierarchicalBlobStorage, that they support full path and folder creation when creating files and actually don't need to have folders created prior to writing of files. if your implementation is only targetting one of these, then you can either explicitly ignore the files, or refactor to not use the CreateFolderAsync extension method

        /// <summary>
        /// Creates a new folder in this storage. If storage supports hierarchy, the folder is created as is, otherwise a folder is created by putting a dummy zero size file in that folder.
        /// </summary>
        /// <param name="blobStorage"></param>
        /// <param name="folderPath">Path to the folder</param>
        /// <param name="dummyFileName">If storage doesn't support hierary, you can override the dummy file name created in that empty folder.</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static async Task CreateFolderAsync(
           this IBlobStorage blobStorage, string folderPath, string dummyFileName = null, CancellationToken cancellationToken = default) {
            if (blobStorage is IHierarchicalBlobStorage hierarchicalBlobStorage) {
                await hierarchicalBlobStorage.CreateFolderAsync(folderPath, cancellationToken).ConfigureAwait(false);
            }
            else {
                string fullPath = StoragePath.Combine(folderPath, dummyFileName ?? ".empty");

                // Check if the file already exists before we try to create it to prevent 
                // AccessDenied exceptions if two processes are creating the folder at the same time.
                if (await blobStorage.ExistsAsync(fullPath)) {
                    return;
                }

                await blobStorage.WriteTextAsync(
                   fullPath,
                   "created as a workaround by FluentStorage when creating an empty parent folder",
                   null,
                   cancellationToken).ConfigureAwait(false);
            }
        }