2

I'm a GCS newbie trying to write an app that uploads a file to a bucket I have created. I've followed the tutorial and created a bucket using the browser interface. However when I programmatically try to access my bucket, I get an error. Instead of printing my default bucket name, my app ouputs "None". My ENTIRE app code is as shown:

import webapp2
from google.appengine.api import app_identity

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write(app_identity.get_default_gcs_bucket_name())

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)

What am I doing wrong?

user1801060
  • 2,733
  • 6
  • 25
  • 44

1 Answers1

4

Update:

Seems you can now create a default bucket in the new console too by visiting your app engine's application settings in the cloud console https://console.cloud.google.com/appengine/settings?project=

Create Default Bucket


Original:

You're getting NONE because by default there's no longer a default bucket

About default Cloud Storage buckets

Applications created using the old App Engine console could use a default Cloud Storage bucket, which had free quota and didn't require billing to be enabled for the app. These applications continue to work as before in the Google Cloud Platform Console.

However, new App Engine projects created in the Google Cloud Platform Console don't have a default Cloud Storage bucket. Instead you must use a standard Cloud Storage bucket as described above.

But since you manually created the bucket, you will already have its name and so can probably skip app_identity.get_default_gcs_bucket_name() and hardcode the bucket name or call it from a config variable, etc, etc.

You can then use the gcloud-python lib to upload files to your bucket:

from gcloud import storage

client = storage.Client()
bucket = client.get_bucket('<bucket-name>')
my_file = bucket.blob('/path/to/be/saved/in/bucket')
my_file.upload_from_filename(filename='/path/to/local/file')

You'll have to vendor the gcloud lib if you're doing this on app engine.

All that said and done if you still want the old behaviour of getting the default bucket which is in the format <project-id.appspot.com>, you can follow GAEfan's answer, in short:

Go to the old console, appengine.google.com > Application Settings > Cloud Integration > Create

Community
  • 1
  • 1
Jeffrey Godwyll
  • 3,787
  • 3
  • 26
  • 37
  • Would like to suggest that you move the edit to the top of the answer as that is what actually fixed the problem for me. – John Mar 11 '16 at 14:07