-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.sh
More file actions
executable file
·70 lines (61 loc) · 2.15 KB
/
Copy pathsample.sh
File metadata and controls
executable file
·70 lines (61 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
#
# Upload a file using the GenAI File API via curl.
api_key=""
input_file=""
display_name=""
while getopts a:i:d: flag
do
case "${flag}" in
a) api_key=${OPTARG};;
i) input_file=${OPTARG};;
d) display_name=${OPTARG};;
esac
done
BASE_URL="https://generativelanguage.googleapis.com"
CHUNK_SIZE=8388608 # 8 MiB
MIME_TYPE=$(file -b --mime-type "${input_file}")
NUM_BYTES=$(wc -c < "${input_file}")
echo "Starting upload of '${input_file}' to ${BASE_URL}..."
echo " MIME type: '${MIME_TYPE}'"
echo " Size: ${NUM_BYTES} bytes"
# Initial resumable request defining metadata.
tmp_header_file=$(mktemp /tmp/upload-header.XXX)
curl "${BASE_URL}/upload/v1beta/files?key=${api_key}" \
-D "${tmp_header_file}" \
-H "X-Goog-Upload-Protocol: resumable" \
-H "X-Goog-Upload-Command: start" \
-H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
-H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
-H "Content-Type: application/json" \
-d "{'file': {'display_name': '${display_name}'}}"
upload_url=$(grep "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r")
rm "${tmp_header_file}"
if [[ -z "${upload_url}" ]]; then
echo "Failed initial resumable upload request."
exit 1
fi
# Upload the actual bytes.
NUM_CHUNKS=$(((NUM_BYTES + CHUNK_SIZE - 1) / CHUNK_SIZE))
tmp_chunk_file=$(mktemp /tmp/upload-chunk.XXX)
for i in $(seq 1 ${NUM_CHUNKS})
do
offset=$((i - 1))
byte_offset=$((offset * CHUNK_SIZE))
# Read the actual bytes to the tmp file.
dd skip="${offset}" bs="${CHUNK_SIZE}" count=1 if="${input_file}" of="${tmp_chunk_file}" 2>/dev/null
num_chunk_bytes=$(wc -c < "${tmp_chunk_file}")
upload_command="upload"
if [[ ${i} -eq ${NUM_CHUNKS} ]] ; then
# For the final chunk, specify "finalize".
upload_command="${upload_command}, finalize"
fi
echo " Uploading ${byte_offset} - $((byte_offset + num_chunk_bytes)) of ${NUM_BYTES}..."
curl "${upload_url}" \
-H "Content-Length: ${num_chunk_bytes}" \
-H "X-Goog-Upload-Offset: ${byte_offset}" \
-H "X-Goog-Upload-Command: ${upload_command}" \
--data-binary "@${tmp_chunk_file}"
done
rm "${tmp_chunk_file}"
echo "Upload complete!"