简体   繁体   English

使用其余API POST上载到Google云存储失败

[英]uploading fails to google cloud storage using rest API POST

I'm trying to figure out how to upload a file to google cloud storage using rest API , i don't want to use the client Library . 我想弄清楚如何使用rest API将文件上传到Google云存储,但我不想使用客户端库。

i read the documents but it was not helpful for a beginner in this flied , 我阅读了文档,但是对于初学者来说,这是没有帮助的,

anyone can give me a step-by-step how to do this ? 任何人都可以逐步指导我该怎么做? and how the URL/header/body format should look like , if also can give me an examples that would be very helpful . 以及URL / header / body格式应该如何,如果还可以给我提供一个非常有用的示例。

If you're not going to use any of the helper libraries and are also a beginner, the hardest part of implementing an upload to GCS will likely be authenticating yourself. 如果您不打算使用任何帮助程序库,而且还是初学者,那么实现向GCS上传的最困难的部分可能就是对自己进行身份验证。 Let's ignore that for now. 现在让我们忽略它。

The simplest way to upload an object to Google Cloud Storage is to make an HTTPS call to storage.googleapis.com that looks like this: 将对象上传到Google Cloud Storage的最简单方法是对storage.googleapis.com进行HTTPS调用,如下所示:

PUT /your-bucket-name/your-object.txt HTTP/1.1
Authorization:  (YOUR ACCESS TOKEN GOES HERE)
Content-Length: 20
Content-Type: text/plain-or-whatever; charset=utf-8
Host: storage.googleapis.com
User-Agent: YourApplication/1.0

This is a test file

That will upload a file named "your-object.txt" of type "text/plain-or-whatever" to the bucket "your-bucket-name", with the contents "This is a test file." 这会将类型为“ text / plain-or-whatever”的名为“ your-object.txt”的文件上传到存储桶“ your-bucket-name”,内容为“ This is a test file”。

If your bucket allows anonymous users to upload files (you shouldn't do that), then just don't include the Authorization line and you're done. 如果您的存储桶允许匿名用户上传文件(您不应该这样做),则只需不包括授权行就可以了。

Now, since you really don't want to use any client libraries, and that presumably includes Google's OAuth libraries, you're going to need to implement authorization yourself, so let me give you an overview. 现在,由于您确实不想使用任何客户端库,并且大概包含了Google的OAuth库,因此您将需要自己实现授权,因此,让我概述一下。

First, though, if you want to try this out immediately, install the "gcloud" tool, login with "gcloud auth login", and the print an access token with gcloud auth print-access-token . 但是,首先,如果要立即尝试,请安装“ gcloud”工具,使用“ gcloud auth login”登录,然后使用gcloud auth print-access-token Then use the Authorization header Authorization: Bearer whatever.gcloudprintedout . 然后使用Authorization标头Authorization: Bearer whatever.gcloudprintedout That way you can be off and running with GCS quickly. 这样一来,您可以快速开始使用GCS。 But the token will only last an hour or so, so you'll need to implement OAuth for real. 但是令牌只会持续一个小时左右,因此您需要真正实现OAuth。

Google Cloud APIs use OAuth to handle their requests, which is a powerful but not simple auth mechanism. Google Cloud API使用OAuth来处理其请求,这是一种功能强大但不简单的身份验证机制。 There's extensive documentation on how OAuth with Google works: https://developers.google.com/identity/protocols/OAuth2 有关OAuth与Google的工作原理的大量文档: https//developers.google.com/identity/protocols/OAuth2

And there's also more general information on authorizing Google Cloud requests: https://cloud.google.com/docs/authentication 关于授权Google Cloud请求,还有更多常规信息: https : //cloud.google.com/docs/authentication

If you are running your application on a Google Cloud technology like App Engine or GCE, auth will be somewhat easier, but I will assume you're running this on your own machine. 如果您是在诸如App Engine或GCE之类的Google Cloud技术上运行应用程序,则auth会更容易一些,但是我假设您是在自己的计算机上运行它。 I will further assume that you want your application to have its own identity, rather than simply having you log in as part of the upload flow. 我将进一步假设您希望您的应用程序具有其自己的身份,而不只是在上载流程中进行登录。 For such a case, you'll need a service account, which will have an associated private key. 在这种情况下,您将需要一个服务帐户,该帐户将具有关联的私钥。

The basic flow for a service account is that you will create a JWT request for access credentials, then cryptographically sign that request with your private key, then send that signed request to Google. 服务帐户的基本流程是,您将创建一个访问凭证的JWT请求,然后使用私钥对该请求进行加密签名,然后将该签名的请求发送给Google。 It will return you a token that may then be passed to your actual upload request later. 它将返回一个令牌,该令牌随后可以传递给您的实际上传请求。 You can keep using that token until it expires, at which time you'll need to build another JWT request to request another token. 您可以继续使用该令牌直到令牌过期,这时您需要构建另一个JWT请求来请求另一个令牌。

Again, the client libraries entirely take care of this whole process for you. 同样,客户端库将完全为您处理整个过程。 I am describing the approach of implementing everything exclusively on your own. 我正在描述仅由您自己实现所有内容的方法。

You can find the same example here: 您可以在此处找到相同的示例:

https://stackoverflow.com/a/53955058/4345389 https://stackoverflow.com/a/53955058/4345389

in which I already explained how to upload a file to google cloud storage using rest API. 在其中,我已经说明了如何使用rest API将文件上传到Google云存储。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谷歌云存储正确的图像上传 - Google cloud storage correct image uploading 使用Google API在Google云存储中创建和共享存储桶 - Create and Share bucket in google cloud storage using google api's 使用Google Client API到Google Cloud Storage的文件上传错误 - File Upload error using Google Client API to Google Cloud Storage Android:Google Cloud Storage API中的SQLite存储 - Android : SQLite storage in Google Cloud Storage API ruby 中的谷歌云 api 在上传单个文件时执行失败 - Google cloud api in ruby fails execution expired while uploading a single file 使用谷歌云存储API与开发人员密钥(不是OAuth2) - using google cloud storage api with developer key (not OAuth2) 如何使用Java API更新Google Cloud Storage中文件的ACL - How to update ACL of a file in Google Cloud Storage using Java API 使用 DropZone.js 通过 signedUrl 将 MP3 上传到 Google Cloud Storage 已损坏 - Uploading an MP3 using DropZone.js to Google Cloud Storage via signedUrl is corrupted 上载到Google Cloud Storage时出现“套接字挂起” / ECONNRESET错误 - “Socket Hang Up” / ECONNRESET Error while uploading to Google Cloud Storage Google云端存储获取对象大小api - Google cloud storage get object size api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM