From d76d50790c90d5eac3844575cbda4521aba7e0ac Mon Sep 17 00:00:00 2001 From: Thinira Date: Fri, 11 Feb 2022 16:55:27 +0530 Subject: [PATCH] Bucket prefix issue fixed Eg: /prefix1/prefix2/prefix3/media --- .../BucketMediaFileSystemComposer.cs | 4 +- .../Umbraco.Storage.S3/BucketFileSystem.cs | 53 +++++++++---------- .../BucketFileSystemConfig.cs | 2 + 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/Umbraco.Storage.S3/Umbraco.Storage.S3.Media/BucketMediaFileSystemComposer.cs b/Umbraco.Storage.S3/Umbraco.Storage.S3.Media/BucketMediaFileSystemComposer.cs index 1885b8f..a089fc3 100644 --- a/Umbraco.Storage.S3/Umbraco.Storage.S3.Media/BucketMediaFileSystemComposer.cs +++ b/Umbraco.Storage.S3/Umbraco.Storage.S3.Media/BucketMediaFileSystemComposer.cs @@ -44,6 +44,7 @@ private BucketFileSystemConfig CreateConfiguration() var bucketName = ConfigurationManager.AppSettings[$"{AppSettingsKey}:BucketName"]; var bucketHostName = ConfigurationManager.AppSettings[$"{AppSettingsKey}:BucketHostname"]; var bucketPrefix = ConfigurationManager.AppSettings[$"{AppSettingsKey}:MediaPrefix"]; + var mediaPrefix = ConfigurationManager.AppSettings[$"{AppSettingsKey}:MediaFolderName"]; var region = ConfigurationManager.AppSettings[$"{AppSettingsKey}:Region"]; bool.TryParse(ConfigurationManager.AppSettings[$"{AppSettingsKey}:DisableVirtualPathProvider"], out var disableVirtualPathProvider); @@ -67,7 +68,8 @@ private BucketFileSystemConfig CreateConfiguration() Region = region, CannedACL = new S3CannedACL("public-read"), ServerSideEncryptionMethod = "", - DisableVirtualPathProvider = disableVirtualPathProvider + DisableVirtualPathProvider = disableVirtualPathProvider, + MediaPrefix = mediaPrefix }; } } diff --git a/Umbraco.Storage.S3/Umbraco.Storage.S3/BucketFileSystem.cs b/Umbraco.Storage.S3/Umbraco.Storage.S3/BucketFileSystem.cs index c07b5a2..e94c1c3 100644 --- a/Umbraco.Storage.S3/Umbraco.Storage.S3/BucketFileSystem.cs +++ b/Umbraco.Storage.S3/Umbraco.Storage.S3/BucketFileSystem.cs @@ -70,32 +70,31 @@ protected virtual IEnumerable ExecuteWithContinuation(ListO } } - protected virtual string ResolveBucketPath(string path, bool isDir = false) + + protected virtual string ResolveMediaPath(string path, bool isDir = false) { if (string.IsNullOrEmpty(path)) - return Config.BucketPrefix; + return Config.MediaPrefix; //Remove Bucket Hostname - if (!path.Equals("/") && path.StartsWith(Config.BucketHostName, StringComparison.InvariantCultureIgnoreCase)) - path = path.Substring(Config.BucketHostName.Length); + if (!path.Equals("/") && path.StartsWith(Config.MediaPrefix, StringComparison.InvariantCultureIgnoreCase)) + path = path.Substring(Config.MediaPrefix.Length); - // Equalise delimiters - path = path.Replace("/", Delimiter).Replace("\\", Delimiter); + path = path.Replace("\\", Delimiter); + if (path == Delimiter) + return ""; if (path.StartsWith(Delimiter)) path = path.Substring(1); //Remove Key Prefix If Duplicate - if (path.StartsWith(Config.BucketPrefix, StringComparison.InvariantCultureIgnoreCase)) - path = path.Substring(Config.BucketPrefix.Length); + if (path.StartsWith(Config.MediaPrefix, StringComparison.InvariantCultureIgnoreCase)) + path = path.Substring(Config.MediaPrefix.Length); - if (isDir && !path.EndsWith(Delimiter)) - path = string.Concat(path, Delimiter); + if (isDir && (!path.EndsWith(Delimiter))) + path = string.Concat(Delimiter, path); - if (path.StartsWith(Delimiter)) - path = path.Substring(1); - - return string.Concat(Config.BucketPrefix, "/", path); + return path; } protected virtual string RemovePrefix(string key) @@ -111,7 +110,7 @@ public virtual IEnumerable GetDirectories(string path) if (string.IsNullOrEmpty(path)) path = "/"; - path = ResolveBucketPath(path, true); + path = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true)); var request = new ListObjectsRequest { BucketName = Config.BucketName, @@ -137,7 +136,7 @@ public virtual void DeleteDirectory(string path, bool recursive) var listRequest = new ListObjectsRequest { BucketName = Config.BucketName, - Prefix = ResolveBucketPath(path, true) + Prefix = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true)) }; var listResponse = ExecuteWithContinuation(listRequest); @@ -163,7 +162,7 @@ public virtual bool DirectoryExists(string path) var request = new ListObjectsRequest { BucketName = Config.BucketName, - Prefix = ResolveBucketPath(path, true), + Prefix = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true)), MaxKeys = 1 }; @@ -184,7 +183,7 @@ public virtual void AddFile(string path, Stream stream, bool overrideIfExists) var request = new PutObjectRequest { BucketName = Config.BucketName, - Key = ResolveBucketPath(path), + Key = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true)), CannedACL = Config.CannedACL, ContentType = MimeTypeResolver.Resolve(path), InputStream = memoryStream, @@ -208,7 +207,7 @@ public virtual IEnumerable GetFiles(string path) public virtual IEnumerable GetFiles(string path, string filter) { - path = ResolveBucketPath(path, true); + path = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true)); string filename = Path.GetFileNameWithoutExtension(filter); if (filename.EndsWith("*")) @@ -246,7 +245,7 @@ public virtual Stream OpenFile(string path) var request = new GetObjectRequest { BucketName = Config.BucketName, - Key = ResolveBucketPath(path) + Key = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true)) }; MemoryStream stream; @@ -270,7 +269,7 @@ public virtual void DeleteFile(string path) var request = new DeleteObjectRequest { BucketName = Config.BucketName, - Key = ResolveBucketPath(path) + Key = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true)) }; Execute(client => client.DeleteObject(request)); } @@ -283,7 +282,7 @@ public virtual bool FileExists(string path) var request = new GetObjectMetadataRequest { BucketName = Config.BucketName, - Key = ResolveBucketPath(path) + Key = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true)) }; try @@ -323,9 +322,9 @@ public virtual string GetRelativePath(string fullPathOrUrl) } //Strip Bucket Prefix - if (fullPathOrUrl.StartsWith(Config.BucketPrefix, StringComparison.InvariantCultureIgnoreCase)) + if (fullPathOrUrl.StartsWith(Config.MediaPrefix, StringComparison.InvariantCultureIgnoreCase)) { - fullPathOrUrl = fullPathOrUrl.Substring(Config.BucketPrefix.Length); + fullPathOrUrl = fullPathOrUrl.Substring(Config.MediaPrefix.Length); fullPathOrUrl = fullPathOrUrl.TrimStart(Delimiter.ToCharArray()); } @@ -351,7 +350,7 @@ public virtual string GetUrl(string path) hostName = ""; } - return string.Concat(hostName, "/", ResolveBucketPath(path)); + return string.Concat(hostName, "/", Config.MediaPrefix, ResolveMediaPath(path, true)); } public virtual DateTimeOffset GetLastModified(string path) @@ -359,7 +358,7 @@ public virtual DateTimeOffset GetLastModified(string path) var request = new GetObjectMetadataRequest { BucketName = Config.BucketName, - Key = ResolveBucketPath(path) + Key = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true)) }; var response = Execute(client => client.GetObjectMetadata(request)); @@ -378,7 +377,7 @@ public long GetSize(string path) var request = new GetObjectMetadataRequest { BucketName = Config.BucketName, - Key = ResolveBucketPath(path) + Key = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true)) }; var response = Execute(client => client.GetObjectMetadata(request)); diff --git a/Umbraco.Storage.S3/Umbraco.Storage.S3/BucketFileSystemConfig.cs b/Umbraco.Storage.S3/Umbraco.Storage.S3/BucketFileSystemConfig.cs index a4fc655..b48597f 100644 --- a/Umbraco.Storage.S3/Umbraco.Storage.S3/BucketFileSystemConfig.cs +++ b/Umbraco.Storage.S3/Umbraco.Storage.S3/BucketFileSystemConfig.cs @@ -10,6 +10,8 @@ public class BucketFileSystemConfig public string BucketPrefix { get; set; } + public string MediaPrefix { get; set; } + public string Region { get; set; } public S3CannedACL CannedACL { get; set; }