Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I upload to Google Cloud Storage with Curl and an API key?

I am trying to upload to google cloud storage with Curl and an API key, without success. The error message seems to indicate that I lack the Content-length header, which I don't. Any ideas?

$ 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
like image 307
neu242 Avatar asked Feb 23 '17 14:02

neu242


People also ask

How do I upload data to Google Cloud Storage?

Drag and drop the desired files from your desktop or file manager to the main pane in the Google Cloud console. Click the Upload Files button, select the files you want to upload in the dialog that appears, and click Open.

Can I upload files to Google Cloud Storage from URL?

It is not possible to upload a file to Google Cloud Storage directly from an URL. Since you are running the script from a local environment, the file contents that you want to upload, need to be in that same environment. This means that the contents of the url need to either be stored in the memory, or in a file.

How do I upload a CSV file to Google Cloud?

Select All Settings > Raw Data Export > CSV Upload. Select Google Cloud Storage from the dropdown menu. Upload your Service Account Key credential file. This is the JSON file created in the Google Cloud Console. Enter your Google Cloud Storage bucket name.


1 Answers

API keys do not provide authentication. They associate a call with a project for purposes of quota and a few other things, but you cannot use them to access resources that require anything beyond anonymous access.

What you'll need is an "Access Key", which can be acquired in a variety of ways, usually via an OAuth exchange. If you have the gcloud command installed, the easiest way to grab a quick access key to use with cURL is to run gcloud auth print-access-token. The following should work:

$> curl -v --upload-file my-file.txt \
    -H "Authorization: Bearer `gcloud auth print-access-token`" \ 
    'https://storage.googleapis.com/my-bucket/my-file.txt'
like image 199
Brandon Yarbrough Avatar answered Oct 05 '22 22:10

Brandon Yarbrough