John Constantine
John Constantine

Reputation: 421

Uploading folder to Google Cloud - Python 2.7

I am trying to upload a folder in my local machine to google cloud bucket. I get an error with the credentials. Where should I be providing the credentials and what all information is needed in it.

from_dest = '/Users/xyzDocuments/tmp'
gsutil_link = 'gs://bucket-1991'

from google.cloud import storage
try:
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(destination_blob_name)
    blob.upload_from_filename(source_file_name)
    print('File {} uploaded to {}.'.format(source_file_name,destination_blob_name))

except Exception as e:
    print e

The error is

could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://developers.google.com/accounts/do`cs/application-default-credentials.

Upvotes: 1

Views: 874

Answers (2)

Armin_SC
Armin_SC

Reputation: 2260

This error message is usually thrown when the application is not being authenticated correctly due to several reasons such as missing files, invalid credential paths, incorrect environment variables assignations, among other causes. Keep in mind that when you set an environment variable value in a session, it is reset every time the session is dropped.

Based on this, I recommend you to validate that the credential file and file path are being correctly assigned, as well as follow the Obtaining and providing service account credentials manually guide, in order to explicitly specify your service account file directly into your code; In this way, you will be able to set it permanently and verify if you are passing the service credentials correctly.

Passing the path to the service account key in code example:

def explicit():
  from google.cloud import storage

  # Explicitly use service account credentials by specifying the private key
  # file.
  storage_client = storage.Client.from_service_account_json('service_account.json')

  # Make an authenticated API request
  buckets = list(storage_client.list_buckets())
  print(buckets)

Upvotes: 0

Dustin Ingram
Dustin Ingram

Reputation: 21520

You need to acquire the application default credentials for your project and set them as an environmental variable:

  1. Go to the Create service account key page in the GCP Console.
  2. From the Service account drop-down list, select New service account.
  3. Enter a name into the Service account name field.
  4. From the Role drop-down list, select Project > Owner.
  5. Click Create. A JSON file that contains your key downloads to your computer.

Then, set an environmental variable which will provide the application credentials to your application when it runs locally:

$ export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/[FILE_NAME].json"

Upvotes: 1

Related Questions