Skip to content

A Lua library for communicating with Google Cloud Storage

Notifications You must be signed in to change notification settings

roaminggamer/cloud_storage

 
 

Repository files navigation

cloud_storage

A library for connecting to Google Cloud Storage through Lua.

Tutorial

Communicating with Google Cloud Storage is done using what Google calls a service account. You need to create a new service account and download the private key that is generated with it. It's described in detail on this page but here's a quicker tutorial focused on this library:

First head to the APIs console, https://code.google.com/apis/console/. Enable cloud storage if you haven't done so already. You may also need to enter billing information.

Now navigate to API Access and click the big button "Create an OAuth 2.0 client ID".

API Access

Follow the dialog, when you get to Application type make sure to select Service account:

x

Now you'll be given download access to the newly created private key, along with the password which seems to be hard coded to notasecret.

x

Download the private key, it's a .p12 file. In order to use it we need to convert it to a .pem. Run the following command (replacing key.p12 and key.pem with the input filename and desired output filename). Enter notasecret for the password.

openssl pkcs12 -in key.p12 -out key.pem -nodes -clcerts

We'll need one more piece of information, the service account email address. It's of the form 111111111111@developer.gserviceaccount.com where 111111111111 is your project id. You can find it on the page where you created your service account:

x

Now we're ready to write some code. Let's write a simple application that lists the contents of a bucket. Remember, you must have access to the bucket. You can create a new bucket from the APIs console if you don't already have one.

local oauth = require "cloud_storage.oauth"

-- replace with your service account email address
o = oauth.OAuth("111111111111@developer.gserviceaccount.com", "path/to/key.pem")

local google = require "cloud_storage.google"

-- use your project id as the second argument, same number in service account email
local storage = google.CloudStorage(o, "111111111111")

local files = storage:get_bucket("my_bucket")

Reference

cloud_storage.oauth

Handles OAuth authenticated requests. You must create an OAuth object that will be used with the cloud storage API.

local oauth = require "cloud_storage.oauth"

ouath_instance = oauth.OAuth(service_email, path_to_private_key)

Create a new OAuth object.

cloud_storage.google

Communicates with the Google cloud storage API.

local google = require "cloud_storage.google"

storage = oauth.CloudStorage(ouath_instance, project_id)

local storage = google.CloudStorage(o, "111111111111")

storage:get_service()

https://developers.google.com/storage/docs/reference-methods#getservice

storage:get_bucket(bucket)

https://developers.google.com/storage/docs/reference-methods#getbucket

storage:get_file(bucket, key)

https://developers.google.com/storage/docs/reference-methods#getobject

storage:delete_file(bucket, key)

https://developers.google.com/storage/docs/reference-methods#deleteobject

storage:head_file(bucket, key)

https://developers.google.com/storage/docs/reference-methods#headobject

storage:put_file(bucket, fname, opts={})

Reads fname from disk and uploads it. The key of the file will be the name of the file unless opts.key is provided. The mimetype of the file is guessed based on the extension unless opts.mimetype is provided.

storage:put_file("my_bucket", "source.lua", { mimetype = "text/lua" })

storage:put_file_string(bucket, data, opts={})

Uploads the string data to the bucket. opts.key must be provided, and will be the key of the file in the bucket. Other options include:

  • opts.mimetype: sets Content-type header for the file, defaults to not being set
  • opts.acl: sets the x-goog-acl header for file, defaults to public-read
  • opts.headers: an optional array table of any additional headers to send
storage:put_file_string("my_bucket", "hello world!", {
  key = "message.txt",
  mimetype = "text/plain",
  acl = "private"
})

storage:signed_url(bucket, key, expiration)

Creates a temporarily URL for downloading an object regardless of it's ACL. expiration is a unix timestamp in the future, like one generated from os.time().

print(storage:signed_url("my_bucket", "message.txt", os.time() + 100))

About

A Lua library for communicating with Google Cloud Storage

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Lua 62.0%
  • MoonScript 37.4%
  • Makefile 0.6%