Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Media.PublicUrl is wrong when using BlobStorage #1815

Open
dozer75 opened this issue Jan 28, 2022 · 5 comments
Open

Media.PublicUrl is wrong when using BlobStorage #1815

dozer75 opened this issue Jan 28, 2022 · 5 comments
Labels

Comments

@dozer75
Copy link

dozer75 commented Jan 28, 2022

When using BlobStorage for media the URL that is generated in the PublicUrl is wrong. This worked in 9.1.0 (at least).

It uses a sas url and appends the filename like this:

https://<mystorage>.blob.core.windows.net/piranha-media?sv=2020-08-04&ss=b&srt=co&spr=https&st=2021-08-10T14%3A26%3A50Z&se=2025-12-30T23%3A26%3A50Z&sp=<sp>&sig=<sig>/c76c08bc-f5ba-43a4-af08-4c5bfdddc9e6/_MG_2955_210x160.jpg

The blob storage configuration is configured like this:

services.AddPiranhaBlobStorage(configuration.GetConnectionString("PiranhaBlobStorage"), "piranha-media", Piranha.Azure.BlobStorageNaming.UniqueFolderNames);

I can see in the code that in BlobStorage.GetPublicUrl that this has been changed between the versions and that the problem came with this change.

This is a bit critical error, since we're up on .NET 6.0 and thereby forced to use 10.0.x of Piranha and we cannot downgrade from .NET 6.

EDIT:

It seems that this PR broke the SAS possibility. I cannot use managed identity here, we need to have SAS possibility...

@tidyui
Copy link
Member

tidyui commented Jan 28, 2022

Hi! Could you please provide an example of how the correct URL you are expecting should be formatted.

Regards

@dozer75
Copy link
Author

dozer75 commented Jan 30, 2022

Well, as you can see, the URL is in basically wrong, it uses the absolute URI which contains the query string of the container piranha-media with the SAS key and the folder/image appended...

The (at least) 9.1.0 version of this library did it correct, it creates a URL that is https://<mystorage>.blob.core.windows.net/piranha-media/c76c08bc-f5ba-43a4-af08-4c5bfdddc9e6/_MG_2955_210x160.jpg<querystring here>, which is correct.

@dozer75
Copy link
Author

dozer75 commented Jan 30, 2022

I checked the code now; the problem is in the GetPublicUrl method of BlobStorage.

It uses the _blobContainerClient.Uri.AbsoluteUri and just prepends the media.Id and id to it.

This is wrong in many ways (e.g. manipulating URLs should be done with UriBuilder), but a more correct way to retrieve the correct URL is to get the BlobClient from the _blobContainerClient and just return it's Uri like this:

var bc = _blobContainerClient.GetBlobClient(GetResourceName(media, id, false));

return bc.Uri.ToString();

This ensures that the correct blob URI is returned. However, the URI contains an SAS key which isn't good.

To avoid this, a shadow container that uses the _blobContainerClient.Uri (without the query string) should be created to retrieve a real public URL. Something like this:

var pubUri = new UriBuilder(_blobContainerClient.Uri) { Query = "" }.Uri;

var pubBcc = new BlobContainerClient(pubUri);

var bc = pubBcc.GetBlobClient(GetResourceName(media, id, false));

It seems that the issue only occurs if you're using a SAS key to connect, if you use managed identity or the Shared Access Key for the storage it works.

Steps to reproduce:

  1. Create an azure storage
  2. Create a Shared Access Signature key that gives default rights to blob container and object
  3. Copy the generated connection string and use this when setting up the azure storage in Piranha in one of the example projects
  4. Start the project and see that no images are shown.

EDIT: I modified this post a bit, since I found this article at Microsoft that used the URL without the SAS key to retrieve the anonymous URL (The Microsoft example itself contains an error, but that's out of scope here, my example above works but is a bit ineffective).

@tidyui tidyui added the bug label Feb 3, 2022
@tidyui tidyui modified the milestones: Version 10.0.2, Version 10.0.3 Feb 3, 2022
@tidyui tidyui added this to To do in Version 10.1 Feb 22, 2022
@tidyui tidyui added this to To do in Version 10.1 Feb 22, 2022
@martijntakken
Copy link
Contributor

Hi, is the any update on this bug or a workaround?

@tidyui
Copy link
Member

tidyui commented Mar 23, 2022

This will be released in the next minor version but we haven't started it yet. If you have the same issues as @dozer75 you can take his solution from previous comment and temporarily create your own BlobStorage implementation which you use instead of our package.

Regards

@tidyui tidyui removed this from To do in Version 10.1 Apr 25, 2022
@tidyui tidyui modified the milestones: Version 10.1, Version 10.2, Version 11.0 Apr 25, 2022
@tidyui tidyui removed this from the Version 11.0 milestone Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Development

No branches or pull requests

3 participants