umbraco / Umbraco.StorageProviders

MIT License
29 stars 21 forks source link

From GuidUdi, unable to get file stream from Azure Blob Storage - Always receiving 404 error #52

Closed ddprince-pro closed 1 year ago

ddprince-pro commented 2 years ago

Which exact Umbraco version are you using? For example: 9.0.1 - don't just write v9

10.1.0

Bug summary

Starting only with a media GuidUdi, we can't use IMediaService in conjonction with MediaFileManager to obtain a raw file stream. The underlying IFileSystem, which is an AzureBlobFileSystem at runtime, is always throwing an exception (404 Blob Not Found). In the end, instead of looking for a file, say, ~/media/%uniqueid%/myfile.pdf, the AzureBlobFileSystem is looking under the following path: ~/media/media/%uniqueid%/myfile.pdf. More details over the following sections.

Specifics

The following packages are installed within the site project:

    <PackageReference Include="GitVersion.MsBuild" Version="5.10.3" />
    <PackageReference Include="Jumoo.TranslationManager" Version="10.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.MicrosoftAccount" Version="6.0.8" />
    <PackageReference Include="SeoToolkit.Umbraco" Version="2.1.0" />
    <PackageReference Include="Umbraco.Cms" Version="10.1.0" />
    <PackageReference Include="Umbraco.Cloud.StorageProviders.AzureBlob" Version="10.0.0" />
    <PackageReference Include="Umbraco.Forms" Version="10.1.0" />
    <PackageReference Include="uSync" Version="10.1.0" />
    <PackageReference Include="uSync.Community.Contrib" Version="10.1.0" />
    <PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.2.2" />
    <PackageReference Include="Microsoft.Identity.Web" Version="1.25.1" />
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />

AddAzureBlobMediaFileSystem has been called so that the proper services are added during initialization.

appsettings.json configuration for the Azure Blob Storage:

  "Umbraco": {
    "Storage": {
      "AzureBlob": {
        "Media": {
          "ConnectionString": "<Set under KeyVault>"
          "ContainerName": "media",
          "ContainerRootPath": ""
        }
      }
    }
  }

No other customizations.

Steps to reproduce

Use this controller and try both actions. WorkingDownload will work and NotWorkingDownload will not work.

    public class DownloadController : SurfaceController
    {
        private readonly MediaFileManager _mediaFileManager;
        private readonly IWebHostEnvironment _webHostEnvironment;
        private readonly IMediaService _mediaService;

        public DownloadController(IUmbracoContextAccessor umbracoContextAccessor, IUmbracoDatabaseFactory databaseFactory, ServiceContext services,
            AppCaches appCaches, IProfilingLogger profilingLogger, IPublishedUrlProvider publishedUrlProvider, MediaFileManager mediaFileManager, IWebHostEnvironment webHostEnvironment, IMediaService mediaService) : base(
            umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
        {
            _mediaFileManager = mediaFileManager;
            _webHostEnvironment = webHostEnvironment;
            _mediaService = mediaService;
        }

        [HttpPost]
        public IActionResult WorkingDownload(GuidUdi mediaPath)
        {
            var fileProvider = _webHostEnvironment.WebRootFileProvider;
            var mediaFilePath = PublishedUrlProvider.GetMediaUrl(mediaPath.Guid);
            var mediaFileInfo = fileProvider.GetFileInfo(mediaFilePath);
            var mediaStream = mediaFileInfo.CreateReadStream();
            var fileName = Path.GetFileName(mediaFilePath);

            return File(mediaStream, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
        }

        [HttpPost]
        public IActionResult NotWorkingDownload(GuidUdi mediaPath)
        {
            var media = _mediaService.GetById(mediaPath.Guid);
            var mediaStream = _mediaFileManager.GetFile(media!, out var mediaFilePath);
            var fileName = Path.GetFileName(mediaFilePath);

            return File(mediaStream, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
        }
    }

Expected result / actual result

Not receiving a 404 error.

github-actions[bot] commented 2 years ago

Hi there @ddprince-yaksa!

Firstly, a big thank you for raising this issue. Every piece of feedback we receive helps us to make Umbraco better.

We really appreciate your patience while we wait for our team to have a look at this but we wanted to let you know that we see this and share with you the plan for what comes next.

We wish we could work with everyone directly and assess your issue immediately but we're in the fortunate position of having lots of contributions to work with and only a few humans who are able to do it. We are making progress though and in the meantime, we will keep you in the loop and let you know when we have any questions.

Thanks, from your friendly Umbraco GitHub bot :robot: :slightly_smiling_face:

nul800sebastiaan commented 2 years ago

I've moved this to the issuetracker that's specific to the blob storage provider.

I think what you're saying is that the URL is wrong and you get /media/media in it? Maybe that's an accidental default being left behind, or maybe the intention of ContainerName is different from what it looks like, not sure.

Pinging @ronaldbarendse on this one.

ddprince-pro commented 2 years ago

Thanks @nul800sebastiaan , this is indeed what happens. One thing to note, I've voluntarily set the ContainerRootPath option to an empty string, because otherwise I would face the same issue but for almost all files on the site (/media/media in file path).

Hope this helps!

ronaldbarendse commented 1 year ago

This is expected behaviour, because _webHostEnvironment.WebRootFileProvider uses a CompositeFileProvider that delegates the /media folder to the media file system without including this prefix (so /media/%uniqueid%/myfile.pdf will request %uniqueid%/myfile.pdf from the underlying AzureBlobFileSystem). If you're interacting with the media file system directly, you need to ensure the custom media path prefix is removed.