Google Cloud Storage Javascript Usage

女生的网名这么多〃 提交于 2019-12-04 04:53:34

Uploading an object requires some form of authorization (you could also create a bucket that allowed truly anonymous writes, but that's seldom a good idea). There are a few ways to acquire authorization.

One way, as you found, is to have a user sign in to their Google account and use that as authorization. That's a good option if the app is for a small number of known individuals.

Another way to upload images is to use signed URL or signed form policy documents. This allows your users to use a standard HTML form to select and upload an image that has been pre-authorized by your app. The policy document is a small bit of text that describes what the user is allowed to do and has been signed by the appropriate credentials.

Here's an example of a policy document:

{"expiration": "2010-06-16T11:11:11Z",
 "conditions": [
    ["starts-with", "$key", "" ],
    {"acl": "bucket-owner-read" },
    {"bucket": "travel-maps"},
    {"success_action_redirect": "http://www.example.com/success_notification.html" },
    ["eq", "$Content-Type", "image/jpeg" ],
    ["content-length-range", 0, 1000000]
  ]
}

This document specifies that users can only use it to upload objects with the bucket-owner-read ACL, only for uploads to the bucket "travel-maps", may only upload objects up to 1 megabyte in size, and so on. The server would then sign this document using their secret key. The document (in base 64) and its signature would then be included in the HTML form, like so:

<form action="http://travel-maps.storage.googleapis.com" method="post" enctype="multipart/form-data">
  <input type="text" name="key" value="">
  <input type="hidden" name="bucket" value="travel-maps">
  <input type="hidden" name="Content-Type" value="image/jpeg">
  <input type="hidden" name="GoogleAccessId" value="1234567890123@developer.gserviceaccount.com">
  <input type="hidden" name="acl" value="bucket-owner-read">
  <input type="hidden" name="success_action_redirect" value="http://www.example.com/success_notification.html">
  <input type="hidden" name="policy" value="eyJleHBpcmF0aW9uIjogIjIwMTAtMDYtMTZUMTE6MTE6MTFaIiwNCiAiY29uZGl0aW9ucyI6IFsNCiAgWyJzdGFydHMtd2l0aCIsICJrZXkiLCAiIiBdLA0KICB7ImFjbCI6ICJidWNrZXQtb3duZXItcmVhZCIgfSwNCiAgeyJidWNrZXQiOiAidHJhdmVsLW1hcHMifSwNCiAgeyJzdWNjZXNzX2FjdGlvbl9yZWRpcmVjdCI6ICJodHRwOi8vd3d3LmV4YW1wbGUuY29tL3N1Y2Nlc3Nfbm90aWZpY2F0aW9uLmh0bWwiIH0sDQogIFsiZXEiLCAiQ29udGVudC1UeXBlIiwgImltYWdlL2pwZWciIF0sDQogIFsiY29udGVudC1sZW5ndGgtcmFuZ2UiLCAwLCAxMDAwMDAwXQ0KICBdDQp9">
  <input type="hidden" name="signature" value="BSAMPLEaASAMPLE6SAMPLE+SAMPPLEqSAMPLEPSAMPLE+SAMPLEgSAMPLEzCPlgWREeF7oPGowkeKk7J4WApzkzxERdOQmAdrvshKSzUHg8Jqp1lw9tbiJfE2ExdOOIoJVmGLoDeAGnfzCd4fTsWcLbal9sFpqXsQI8IQi1493mw=">
  <input name="file" type="file">
  <input type="submit" value="Upload">
</form>

There's a lot more documentation on how this works here:

https://cloud.google.com/storage/docs/reference-methods#policydocument

Keep in mind that a signed URL is different than a signed form. "Signed URLs" are very specific, arbitrary commands that can be executed exactly as stated. You could use them for uploads, but they're less versatile than signed policy documents.

As Google Cloud Storage doesnt have a simple way to upload an image (2014), I ended up using Amazon Web Services which even comes with a NodeJS Sample Code. Easy and simple to use.

http://aws.amazon.com/es/developers/getting-started/nodejs/

Clone the sample:

git clone https://github.com/awslabs/aws-nodejs-sample.git

Install:

npm install

Run:

node sample.js

The Sample comes with bucket usage and they even store a file in the bucket. So that will make it. Make sure to change your credentials:

aws_access_key_id = YOUR_ACCESS_KEY_ID
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY

Cheers!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!