Skip to content

Instantly share code, notes, and snippets.

@sakama
Last active November 4, 2015 01:08
Show Gist options
  • Save sakama/a1f2c06ad1f9b0623049 to your computer and use it in GitHub Desktop.
Save sakama/a1f2c06ad1f9b0623049 to your computer and use it in GitHub Desktop.
Download objects from Google Cloud Storage using Google API Client Libraries for Ruby
client = Google::APIClient.new(
application_name: "hoge"
)
storage = client.discovered_api("storage", "v1")
scopes = ["https://www.googleapis.com/auth/devstorage.read_write"]
auth = Google::Auth::ServiceAccountCredentials.new(
json_key_io: StringIO.new(json_keyfile),
scope: scopes
)
auth.fetch_access_token!
client.authorization = auth
gcs_bucket = "my-bucket"
page_token = nil
result = client.execute(
api_method: storage.objects.list,
parameters: {bucket: gcs_bucket, prefix: prefix, pageToken: page_token}
)
result.data.items.each do |item|
response = client.execute(
api_method: storage.objects.get,
# get metadata of item
# parameters: {bucket: gcs_bucket, object: item.name}
# get item itself
parameters: {bucket: gcs_bucket, object: item.name, alt: "media"}
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment