如何通过CURL复制Artifactory中最新的SNAPSHOT构件?
Great question! The issue here is exactly what you suspected: that morad-2.3.5-SNAPSHOT.zip isn't an actual file stored in your repository—it's a virtual redirect generated by Artifactory that points to the latest snapshot artifact. Artifactory's copy API doesn't follow these redirects automatically, so you need to target the real, specific snapshot file directly. Here are a few reliable solutions:
1. Use the Exact Filename of the Latest Snapshot
If you already know the filename of the latest snapshot (like morad-2.3.5-20201207_291847218.zip in your example), just update your copy command to use that real file path instead of the redirect link:
curl -u <USER:PASS> -X POST "www.arifactoryrepo.com/artifactory/api/copy/maven-last-dev/com/api/2.3.5-SNAPSHOT/morad-2.3.5-20201207_291847218.zip?to=/another/path"
This is the simplest approach when you're manually triggering the copy or already have visibility into the latest filename.
2. Automatically Fetch the Latest Snapshot via Artifactory's REST API
For automated workflows where you don't want to hardcode the latest filename, you can use Artifactory's Storage API to list the directory contents, sort by modification time, and grab the newest file:
- First, retrieve the list of files in the snapshot directory:
This returns a JSON object with details for every file, includingcurl -u <USER:PASS> "www.arifactoryrepo.com/artifactory/api/storage/maven-last-dev/com/api/2.3.5-SNAPSHOT?list&deep=0&listFolders=0"lastModifiedtimestamps. - Use a tool like
jqto parse the JSON and extract the latest filename:LATEST_FILE=$(curl -u <USER:PASS> "www.arifactoryrepo.com/artifactory/api/storage/maven-last-dev/com/api/2.3.5-SNAPSHOT?list&deep=0&listFolders=0" | jq -r '.files | sort_by(.lastModified) | last | .uri' | cut -c2-) - Finally, run the copy command with the dynamically fetched filename:
curl -u <USER:PASS> -X POST "www.arifactoryrepo.com/artifactory/api/copy/maven-last-dev/com/api/2.3.5-SNAPSHOT/${LATEST_FILE}?to=/another/path"
3. Follow the Redirect to Get the Real File Path
You can also have curl follow the redirect from the virtual morad-2.3.5-SNAPSHOT.zip link to find the actual file path, then use that path for the copy:
- Capture the final URL after redirects:
REAL_URL=$(curl -u <USER:PASS> -L -s -o /dev/null -w "%{url_effective}" "www.arifactoryrepo.com/maven-last-dev/com/api/2.3.5-SNAPSHOT/morad-2.3.5-SNAPSHOT.zip") - Extract the repository path portion from the URL (removing the domain and base Artifactory prefix):
REAL_PATH=$(echo "$REAL_URL" | sed -e 's|^.*artifactory/||') - Execute the copy command using the real path:
curl -u <USER:PASS> -X POST "www.arifactoryrepo.com/artifactory/api/copy/${REAL_PATH}?to=/another/path"
All three methods work by targeting the actual snapshot file rather than the virtual redirect link, which fixes the "failed to copy" error.
内容的提问来源于stack exchange,提问作者user2274204

