从谷歌网盘下载大文件
文件太大,gdown无法使用。
https://github.com/wkentaro/gdown/issues/43#issuecomment-2275059988
https://stackoverflow.com/questions/65312867/how-to-download-large-file-from-google-drive-from-terminal-gdown-doesnt-work
#!/bin/bash
# (或者写 #!/usr/bin/env bash,但在 zsh 命令行下直接跑也没问题)
# --- 修改点:使用兼容的输入方式 ---
printf "Enter your Google Drive access token: "
read ACCESS_TOKEN
# Define the input file containing URLs
INPUT_FILE="file_urls.txt"
# Define the output directory
OUTPUT_DIR="/data/h803/datasets/public/AudioCaps2"
# Check if the input file exists
# Zsh 也完美支持 [[ ... ]],所以这里不需要改
if [[ ! -f "$INPUT_FILE" ]]; then
echo "Input file '$INPUT_FILE' not found!"
exit 1
fi
# Check if the output directory exists
if [[ ! -d "$OUTPUT_DIR" ]]; then
echo "Output directory '$OUTPUT_DIR' does not exist!"
exit 1
fi
echo "Starting download of files listed in '$INPUT_FILE' to '$OUTPUT_DIR'..."
# Loop through each line in the input file
while IFS= read -r URL; do
echo "Reading URL: $URL"
# Extract the file ID from the URL
# grep -oP 依赖系统的 grep 命令,与 shell 无关,无需修改
FILE_ID=$(echo "$URL" | grep -oP 'd/\K[^/]+')
echo "Processing URL: $URL"
if [[ -z "$FILE_ID" ]]; then
echo "Failed to extract file ID from URL: $URL"
continue
fi
FILE_METADATA=$(curl -s -H "Authorization: Bearer $ACCESS_TOKEN" "https://www.googleapis.com/drive/v3/files/$FILE_ID?fields=name")
ORIGINAL_NAME=$(echo "$FILE_METADATA" | jq -r '.name')
if [[ -z "$ORIGINAL_NAME" ]]; then
echo "Failed to retrieve original name for file ID: $FILE_ID"
continue
fi
OUTPUT_FILE="$OUTPUT_DIR/$ORIGINAL_NAME"
echo "Downloading file with ID: $FILE_ID as '$ORIGINAL_NAME' to '$OUTPUT_FILE'"
curl -H "Authorization: Bearer $ACCESS_TOKEN" -C - "https://www.googleapis.com/drive/v3/files/$FILE_ID?alt=media" -o "$OUTPUT_FILE"
if [[ $? -eq 0 ]]; then
echo "Downloaded: $OUTPUT_FILE"
else
echo "Failed to download file with ID: $FILE_ID"
fi
done < "$INPUT_FILE"

浙公网安备 33010602011771号