These instructions have been tested with a Mac mini with macOS version Monterey, an Apple Music library of 25k tracks, a Synology NAS with DSM 7.0.1 and a Sonos Controller for Mac Version 13.4.1.
Before starting the process make sure you have access to the NAS drive with sufficient access rights granted to the account (e.g. read/write to the music shared folder). To mitigate security issues for the music folders on the NAS you can create an account for the Sonos Controller with read access only. Feel free to follow these steps at your own risk.
Step 1: setup the Apple Music Library on the NAS
When you open Apple Music with the Option key on the keyboard pressed you can choose to create a new Library. Create a new Folder on the Synology NAS, under the music shared folder, to create the Apple Music Library (e.g. MyMusic). Check in the Preferences the Files tab for Music Media location so that the directory path points to the NAS. Keep the Music Media folder organised and Copy files to the Music Media folder when adding to the library.
Step 2: import the music files
Choose in Apple Music under the File menu the ‘Import’ option. Select the folder where your music files are stored on your local drive. The Apple Music app will now copy all the files in the selected folder and subfolders to the NAS and update its music library. Bear in mind that all files will be copied to the NAS but not be moved, so the initial Apple Music library and local music folders will remain without change on your local drive. Apple Music may proceed with additional processes for Gapless Playback and Album Artwork once all files have been copied. This may take a while if you have a large music library.
Step 3: import the playlists (optional)
If you previously exported playlists, in xml format, you now can import these. Choose in Apple Music under the File menu the ‘Library’ option and ‘Import Playlist’.
Step 4: export the library for Sonos
Create a subfolder, called iTunes, in the Synology NAS folder (e.g. MyMusic/iTunes) and export the Apple Music library. Choose in Apple Music under the File menu the ‘Library’ option and ‘Export Library’. Store the library file ‘iTunes Music Library.xml’ in the subfolder. Remember that you will need to export the library each time you made changes to your Apple Music tracks or playlists since this file is not automatically updated.
Step 5: update the Sonos music library settings
Update the Sonos App Music Library settings and enter the NAS folder path with the music tracks folder (e.g. //NAS_hostname/music/MyMusic).
Upon completion of the Sonos Music Library update you should find all Apple Music Playlists in the Imported Playlists.