This is part of the writeup on Using Google Cloud Storage with Apps Script. By now you should have completed all the steps in Setting up or creating a console project, Enabling APIs and OAuth2 and Using the service account to enable access to cloud storage and now have a project and credentials you can use to create your storage buckets. You can also read more about the background of how all this works at GcsStore overview – Google Cloud Storage and Apps Script

Libraries

You should already have the cGoa library, from when you set up your credentials in Using the service account to enable access to cloud storage
You’ll also need the cGcsStore library on GitHub or at this key.

1w0dgijlIMA_o5p63ajzcaa_LJeUMYnrrSgfOzLKHesKZJqDCzw36qorl

It’s not necessary, but I usually also include the cUseful library too, as it has lots of shortcuts and I may use some of them in the examples throughout this post. You’ll find that on Github or at

1EbLSESpiGkI3PYmJqWh3-rmLkYKAtCNPi1L2YCtMgo2Ut8xMThfJ41Ex

So my final resource manifest looks like this.

Some examples

These examples are various scenarios of writing and reading different kinds of data, exercising expiry, folderkey, compression and various other capabilities.

Getting started

Consuming an access token, opening the store, creating a bucket if necessary, setting up lifetime (I’m using this one as self-cleansing since I want it to go away later)

Putting and Getting data

Getting the keys in the store

Expiration

Working with blobs

GcsStore can write blobs too.

Default expiration

By default items don’t expire, but you can make them

Compressing

GcsStore can compress and uncompress the data on writing/reading if you want it to.

Predefined ACLS

You can change the predefined ACL of an existing object as in the example below which sets its access to public read.
gcs.patchPredefinedAcl(“compress1”, “publicRead”);

SelfLink

If you want to share an object, you can get its self-link like this, which will return a URL to access its metadata directly. If you’ve set the ACL to publicRead, this would allow you share a file’s metadata publicly. To get the link to its content, use getPublicLink
Logger.log(gcs.getSelfLink(“compress1”));

PublicLink

If you want to share an object, you can get its self-link like this, which will return a URL to access its content directly. If you’ve set the ACL to publicRead, this would allow you share a file publicly.
Logger.log(gcs.getPublicLink(“compress1”));

CORS

GcsStore has methods to enable cors. For more info see Google cloud storage and CORS

More

For more detail on all this, see GcsStore overview – Google Cloud Storage and Apps Script

For more like this see Google Apps Scripts Snippets
Why not join our forum, follow the blog or follow me on Twitter to ensure you get updates when they are available.