GaurangR
GaurangR

Reputation: 59

Unable to access file kept in google storage bucket path through python script

I have kept sample file at GCS location for eg: gs://MyBucket/Folder1/sampleData.csv

Now , I am trying to access this file through my python script but I am unable to do so.

The python script :

import argparse
import json
import time
import uuid
import csv
from google.cloud import bigquery
from google.cloud.bigquery import SchemaField
from google.cloud import storage
import os
import sys
import googleapiclient.discovery


bigquery = googleapiclient.discovery.build('bigquery', 'v2') 

client = storage.Client()
bucket = client.get_bucket('MyBucket')
print bucket

blob = bucket.get_blob('gs://MyBucket/Folder1/sampleData.csv')
print(blob.download_as_string())

Upvotes: 2

Views: 230

Answers (1)

KeepLearn
KeepLearn

Reputation: 308

You are trying to access the google cloud storage but in scope you gave bigquery.

Please see the sample python script to list the files and access the object. Replace the bucket name and object name and execute the code. It should work.

import argparse
import json

import googleapiclient.discovery

service = googleapiclient.discovery.build('storage', 'v1')


#### List Bucket
fields_to_return = \
        'nextPageToken,items(name,size,contentType,metadata(my-key))'
req = service.objects().list(bucket='XYZ', fields=fields_to_return)
resp = req.execute()
print resp

### Get Object 
req = service.objects().get_media(bucket='XYZ', object='ad_list04_08_2017.csv')
result=req.execute()
print result

Upvotes: 1

Related Questions