Creating signed URLs for Google Storage

Hi there,
Is there a hook or filter I can use to rewrite or translate all urls on all my pages that match a given regular expression?
I am storing my media files in a google storage bucket with restricted permissions. Objects can be accessed with urls signed using the private key.
There is a function here that can sign a url, but how can I incorporate this function into the WordPress action execution order?
https://stackoverflow.com/questions/25778840/google-cloud-storage-signed-url-for-media?rq=1
Thanks – Mark.

  • James Morris

    Hello Mark

    Given this is a bit more advanced code query, I'm flagging our SLS Team so they can look into this further. I've already spoken with them and they could use a little clarification on your situation to better understand what you're trying to do.

    Could you clarify what media is being uploaded and how you are uploading it? Is it going through WP Admin -> Media Library? Are you using a third-party plugin to push it to your Google bucket? Or, are you manually uploading to your bucket and adding the link references to the post content.

    Providing as much detail as possible will help our team come up with a solution that fits your needs.

    Best regards,

    James Morris

  • Panos

    Hi there Mark ,

    You could try using the wp_calculate_image_srcset which is responsible on creating the srcset of the images in the post content.

    It get's triggered by the_content filter so you should not have any issues with the expiration parameter which is based on time.

    Here is a simple example of using this filter which only adds a custom query var to the urls:

    function wpmudev_storageURL( $url ) {
    
    	//Replace this part:
    	$url = add_query_arg( 'private-key', '1234567890', $url );
    
    	return $url;
    }
    
    add_filter( 'wp_calculate_image_srcset', function( $sources, $size_array, $image_src, $image_meta, $attachment_id ){
    
    	foreach ( $sources as $source_size => $source ) {
    		if ( isset( $source['url'] ) ) {
    			$sources[$source_size]['url'] = wpmudev_storageURL( $source['url'] );
    		}
    	}
    
    	return $sources;
    
    }, 20, 5 );

    Hope this helps :slight_smile:

    Please let us know if there is anything that we may be missing also let us know about what James asked above in case there is something else that you need.

    Kind regards!

  • Panos

    Hi Mark!

    From what you say there shouldn't be an issue. I'm not familiar with the Google Cloud Storage service not the plugin you are using, but I assume that when you select an image from the Media Gallery the correct url should be added in the content.

    If you still need to modify urls in a post/page content, the above snippet should do the trick. Please let us know if you tried using the snippet above. If you are having any difficulties could you please provide an example of what you need to do?

    Thanks!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.