如何从URL及3D模型中获取storageID?遇无效URL报错求解决
Hey there, I’ve dealt with similar headaches before—let’s break this down clearly to help you get past that frustrating "invalid url" message and grab your storageID correctly.
Retrieving storageID from a URL
First, where storageID lives in a URL depends entirely on the platform or service hosting your content. Here’s how to pull it in common scenarios:
Case 1: storageID is part of the URL path
Many services structure URLs likehttps://your-service.com/storage/{storageID}/assets/model.glb. To extract this:- Use URL parsing utilities to split the path. For example, in JavaScript:
const targetUrl = new URL("https://your-service.com/storage/abc123/assets/model.glb"); // Adjust the index based on your URL's structure—here it's the 2nd path segment (0-indexed) const storageID = targetUrl.pathname.split("/")[2]; console.log(storageID); // Outputs "abc123" - In Python, you’d do something similar with
urllib.parse:from urllib.parse import urlparse parsed_url = urlparse("https://your-service.com/storage/abc123/assets/model.glb") storageID = parsed_url.path.split("/")[2]
- Use URL parsing utilities to split the path. For example, in JavaScript:
Case 2: storageID is a query parameter
If your URL looks likehttps://your-service.com/model?storageId=xyz789, extract it directly from the query string:- JavaScript example:
const targetUrl = new URL("https://your-service.com/model?storageId=xyz789"); const storageID = targetUrl.searchParams.get("storageId"); // Note: Parameter names are case-sensitive—double-check if it's "storageid", "storage_id", etc.
- JavaScript example:
Fixing "Invalid URL" for this scenario
That error almost always boils down to a malformed URL:
- Double-check that your URL includes a valid protocol (
http://orhttps://—missing this is the #1 culprit) - Ensure any special characters (spaces, emojis, non-Latin characters) are URL-encoded (replace spaces with
%20, etc.) - Verify the URL doesn’t have typos (extra slashes, misspelled domains)
Retrieving storageID from a 3D Model
This depends on whether the storageID is embedded in the model’s metadata or linked to the model via a platform API.
Case 1: storageID is embedded in model metadata
Most modern 3D formats (GLB/GLTF, FBX, USD) support custom metadata fields. Platforms often inject storageID here when exporting models:- For GLB/GLTF (the most common format), use a loader like Three.js to access metadata:
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; const loader = new GLTFLoader(); loader.load("your-model.glb", (gltf) => { // Check the extras field—platforms often store custom data here const storageID = gltf.scene.userData.extras?.storageId; if (storageID) { console.log("Found storageID:", storageID); } else { console.log("No storageID found in model metadata"); } }, undefined, (error) => { console.error("Error loading model:", error); }); - For FBX, use tools like Blender’s Python API or the FBX SDK to read custom properties attached to the model or its objects.
- For GLB/GLTF (the most common format), use a loader like Three.js to access metadata:
Case 2: storageID is linked via a platform API
If the model doesn’t have embedded metadata, you’ll need to use the hosting platform’s API to look up the storageID:- Most services let you query storageID using the model’s file hash, name, or upload timestamp. You’ll need to authenticate with the platform’s API and pass those details to retrieve the associated storageID.
Fixing "Invalid URL" for model loading
If you get this error when loading the 3D model:
- If loading a local file: Don’t use
file://URLs (browsers block them for security). Instead, spin up a local dev server (e.g.,python -m http.serverornpx serve) and usehttp://localhost:port/model.glb - If loading a remote model: Check CORS settings on the server, ensure the URL is publicly accessible, and verify there are no typos in the path
内容的提问来源于stack exchange,提问作者Alejandro Gurovich

