Download file from google cloud storage using ruby client gets HTTP redirect -
i trying download file gcs using ruby client , service account authorization; here code:
def build_client client = google::apiclient.new() key = google::apiclient::pkcs12.load_key(service_account_pkcs12_file_path, 'notasecret') service_account = google::apiclient::jwtasserter.new( service_account_email, 'https://www.googleapis.com/auth/devstorage.full_control', key) client.authorization = service_account.authorize client end client = build_client storage = client.discovered_api('storage', 'v1beta2') #get (download) specific object bucket bucket_get_result = client.execute( api_method: storage.objects.get, parameters: {bucket: bucket, object: params[:file_name], alt: 'media'} ) puts bucket_get_result.body
the result body redirect detail follows:
<html> <head> <title>temporary redirect</title> </head> <body bgcolor="#ffffff" text="#000000"> <h1>temporary redirect</h1> document has moved <a href="https://storage.googleapis.com/my_bucket_name/my_file_name">here</a>. </body> </html>
...and if try go redirect 'location' unauthorized http. so, how can download file gcs using ruby client? https://developers.google.com/storage/docs/json_api/v1/objects/get says:
if provide url parameter alt=media, respond object data in response body
the google ruby api client not support downloading media.
however, rest of work yourself.
require 'httparty' get_result = bucket_get_result = client.execute( api_method: storage.objects.get, parameters: {bucket: bucket, object: params[:file_name], :alt=>'media'}) url = get_result.response.env[:response_headers]['location'] token = "bearer #{bucket_get_result.request.authorization.access_token}" puts httparty.get(url, :headers => {"authorization" => token}).body
Comments
Post a Comment