我正在尝试使用 Curl 和 API 密钥上传到谷歌云存储,但没有成功。错误消息似乎表明我缺少 Content-length 标头,而我没有。有什么想法吗?
$ curl -v -T ./myfile -X POST https://storage.googleapis.com/my-app/my-bucket/myfile?key=<my-api-token>
> Host: storage.googleapis.com
> User-Agent: curl/7.51.0
> Accept: */*
> Content-Length: 4
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 411 Length Required
< Date: Thu, 23 Feb 2017 13:46:59 GMT
< Content-Type: text/html; charset=UTF-8
< Server: UploadServer
< Content-Length: 1564
< Alt-Svc: quic=":443"; ma=2592000; v="36,35,34"
< Connection: close
<
<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 411 (Length Required)!!1</title>
<style>
[snip snip]
</style>
<a href=//www.google.com/><span id=logo aria-label=Google></span></a>
<p><b>411.</b> <ins>That’s an error.</ins>
<p>POST requests require a <code>Content-length</code> header. <ins>That’s all we know.</ins>
* Curl_http_done: called premature == 0
* Closing connection 0
API 密钥不提供身份验证。他们出于配额和其他一些目的将呼叫与项目相关联,但您不能使用它们来访问需要匿名访问以外的任何资源。
你需要的是一个“访问密钥”,它可以通过多种方式获得,通常是通过 OAuth 交换。如果您安装了
gcloud
命令,获取与 cURL 一起使用的快速访问密钥的最简单方法是运行 gcloud auth print-access-token
。以下应该有效:
$> curl -v --upload-file my-file.txt \
-H "Authorization: Bearer `gcloud auth print-access-token`" \
'https://storage.googleapis.com/my-bucket/my-file.txt'
检查这个GCP文件出来!
TLDR;请按照以下步骤操作:
gcloud auth print-access-token
或执行步骤1-6TOKEN="your token from step #0 or #6"
curl -X POST --data-binary @/path/to/your/file/big_file.json \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
"https://storage.googleapis.com/upload/storage/v1/b/BUCKET_NAME/o?uploadType=media&name=big_file.json"
(curl -X GET -H "Authorization: Bearer $TOKEN" \
-o "/path/to/download/folder/big_file.json" \
"https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/big_file.json?alt=media") >/dev/null
注意:使用
>/dev/null
防止curl打印出文件内容。这要快得多,尤其是当您的文件很大时。