我在我的 cloudflare worker 中收到了一个请求,想要将数据上传到 Google 云存储。我的问题是我无法从收到的 multipart/form-data 数据中提取内容类型,以便将具有正确内容类型的内容上传到 GCS。
当我从 formData 读取请求时await req.formData()
,get('file')
它返回 GCS 所需的原始文件数据,但我似乎无法在任何地方看到我需要的文件内容类型(我只能在查看时看到它在原始请求正文中)。
这是我的(精简)代码:
event.respondWith((async () => {
const req = event.request
const formData = await req.formData()
const file = formData.get('file')
const filename = formData.get('filename')
const oauth = await getGoogleOAuth()
const gcsOptions = {
method: 'PUT',
headers: {
Authorization: oauth.token_type + ' ' + oauth.access_token,
'Content-Type': 'application/octet-stream' //this should by `'Content-Type': file.type`
},
body: file,
}
const gcsRes = await fetch(
`https://storage.googleapis.com/***-media/${filename}`,
gcsOptions,
)
if (gcsRes.status === 200) {
return new Response(JSON.stringify({filename}), gcsRes)
} else {
return new Response('Internal Server Error', {status: 500, statusText: 'Internal Server Error'})
}
})())
提醒 - 该代码是我们 cloudflare worker 代码的一部分。
在我看来,这应该是直截了当的,确定您从 multipart/form-data 数据中提取的文件类型。我错过了什么吗?