2017-02-23 79 views
1

我想用Curl和API密鑰上傳到谷歌雲存儲,但沒有成功。該錯誤消息似乎表明我缺少內容長度標題,我不這樣做。有任何想法嗎?如何使用Curl和API密鑰上傳到Google雲端存儲?

$ 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 

回答

5

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' 
+0

謝謝!現在我得到一個不同的錯誤:'<?xml version ='1.0'encoding ='UTF-8'?>InvalidArgument無效的參數。

POST對象期望Content-Type multipart/form-data
' – neu242

+1

您現在使用的是什麼curl命令?看起來你正在做一個POST而不是PUT。 –

+0

就是這樣!我不知何故在命令中仍然有'-X POST'。謝謝。 – neu242

相關問題