Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tim 43 posts 197 karma points
    Dec 01, 2020 @ 18:29
    Tim
    0

    S3 Media with sub-folder

    Hi,

    I have an issue with S3 and I'm wondering if anyone else has come across this and found a way around it?

    The client has a bucket, and rather than the media files being in the root of the S3 bucket, they're in a sub-folder, e.g. "project-name/media".

    Now, using the V8 port of the S3 provider: https://github.com/jacexor/Umbraco8-Simple-AWS-S3/issues I'm able to specify that the bucket prefix for media is ""project-name/media". When uploading the image via the back office, the image uploads to the bucket correctly.

    HOWEVER, the back office will then not load the images correctly. The CMS links to /project-name/media/normal-umbarco-path-stuff, which doesn't work. If I manually hack out the "/project-name" part, Umbraco is able to resolve the image URL as expected. The same issue exists if you call .Url() on the media item.

    Has anyone ever had to do similar and got it to work properly? I can think of some very hacky workarounds, but if there's a "correct" way to do this that works correctly out of the box, I'd rather use that!

  • P.J. Melies 18 posts 108 karma points
    Jan 07, 2021 @ 17:33
    P.J. Melies
    0

    Were you able to come up with a solution or a workaround? We are encountering the exact same issue. We're using Umbraco.Storage.S3, v 1.0.38.

  • Tim 43 posts 197 karma points
    Jan 07, 2021 @ 19:46
    Tim
    0

    Yes, there's a few extra steps you need to take to get it to work properly! You need to create "projectname/media" in your umbraco site and put the special web.config file that you'd normally put in the media folder in there too.

    Finally, assuming you're on V8, you need to register the virtual path provider in a composer to wire it all in. This is the composer code:

        [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
        public class MediaFilePathFixComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.Components().Append<MediaFilePathFixComponent>();
            }
        }
    
        public class MediaFilePathFixComponent : IComponent
        {
            private readonly SupportingFileSystems supportingFileSystems;
            private readonly BucketFileSystemConfig config;
    
            public MediaFilePathFixComponent(SupportingFileSystems supportingFileSystems, BucketFileSystemConfig config)
            {
                this.supportingFileSystems = supportingFileSystems;
                this.config = config;
            }
    
            public void Initialize()
            {
                //IMPORTANT: The virtual path is hard coded to /media in the S3 provider, so we MUST override it here in order to get the correct image paths displaying on the site where we are using production bucket paths
                var fs = supportingFileSystems.For<IMediaFileSystem>() as BucketFileSystem;
    
                if (!config.DisableVirtualPathProvider && fs != null && config.BucketPrefix != "media")
                {
                    FileSystemVirtualPathProvider.ConfigureMedia(config.BucketPrefix);
                }
            }
    
            public void Terminate()
            {
    
            }
        }
    

    This will check if the bucket path is anything other than the default media one and register the virtual path provider.

    I've raised an issue on the Umbraco.Storage.S3 repo, but it's not been fixed yet as far as I can see.

    Hope that helps!

Please Sign in or register to post replies

Write your reply to:

Draft