0

I just received an access to bucket gs://asdasdasdasdd-sadasdasd on Google Cloud Storage with files for test exercise.

They said I have an access for my google account.

But how am I supposed to download file rom there in python? With which credentails?

I created service account and downloaded json file with my credentials, but I am forbidden to download files form the bucket.

How should I process further?

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from io import BytesIO

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="account.json"

from google.cloud import storage

storage_client = storage.Client()
bucket = storage_client.get_bucket('asdasdasdasdd-sadasdasd')
blob = bucket.blob('streams/2017/09/09/allcountries')
path = "gs://asdasdasdasdd-sadasdasd/streams/2017/09/09/allcountries.csv"
df = pd.read_csv(path)

I am able to download file with gsutil but I need to do the same with python. Someway I need to verify my email becuase I was granted to download file on my google email.

paveltr
  • 474
  • 1
  • 8
  • 22

2 Answers2

0

I assume you were granted a role to access the bucket. If so, you do not need the service account key (.json file), as this key has been generated by you, therefore it is granting permissions to the resources under your project and not someone else's.

Make sure the role you were given is roles/storage.admin as this is the role needed to download files from the specified bucket.

Another option would be to indeed use a service account key, containing the same role, but it has to be given to you by the owner of the bucket.

Lastly, I tried your code and came across an error once I was able to connect to the bucket. If you encounter an IOError telling you the file does not exist, take a look at this post for a possible solution.

Maxim
  • 4,075
  • 1
  • 14
  • 23
0

Give this a try:

import pathlib
import google.cloud.storage as gcs

client = gcs.Client()

#set target file to write to
target = pathlib.Path("local_file.txt")

#set file to download
FULL_FILE_PATH = "gs://bucket_name/folder_name/file_name.txt"

#open filestream with write permissions
with target.open(mode="wb") as downloaded_file:

        #download and write file locally 
        client.download_blob_to_file(FULL_FILE_PATH, downloaded_file)
tb.
  • 723
  • 7
  • 16