How to access Google Cloud Bucket from development Environment - google-app-engine

I am trying to access my Google Cloud Bucket from development environment but when I write import statement I get error
from google.cloud import storage
Command I use to run server with bucket flag
dev_appserver.py app.yaml --default_gcs_bucket_name ABC-test-bucket
Error I get
File "C:\Users\ABC\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\setuptools-0.6c11\pkg_resources.py", line 565, in resolve
raise DistributionNotFound(req) # XXX put more info here
DistributionNotFound: google-cloud-storage
I think I completed all the steps like creating bucket, downloading client library using pip i.e.
pip install GoogleAppEngineCloudStorageClient -t <your_app_directory/lib>
I am new to GAE projects(Using Webapp2 Python framework for server) so I will really appreciate any pointers and help

The library you're using is not the correct one. You need the one below, as per the documentation:
pip install --upgrade google-cloud-storage

Related

Django+React AWS LightSails Ubuntu deployment not using js

this is my first deployment, I have gone so far as to create and configure Ubuntu instance on lightsails following official guide. However, I run into problems when instead of an empty project I use my own Django hosting React frontend.
My project works on my computer and if I start up django's production server thorugh manage.py runserver but does not run at all if I use gunicorn to run it with:
gunicorn my_app.wsgi --bind 0.0.0.0:8000 --workers 4
It seems that it cannot find proper filepaths of any static files but I do not undertstand how to configure it to use it since Django already has all the right paths.
One of the errors in the browser console:
Refused to execute http://3.127.76.103/static/rest_framework/js/bootstrap.min.js as script because "X-Content-Type-Options: nosniff" was given and its Content-Type is not a script MIME type.
How can I start fixing it, I do not understand why it does not see the any files.

Installing private packages in app engine from a google artifact registry

I am trying to modularize our code, and running into difficulties installing private packages on app engine.
We have a repository on GCP in the same project, so I would think this is not too difficult.
My requirement.txt looks like:
--extra-index-url=https://us-central1-python.pkg.dev/myproject/my-python-repo/simple/
Flask==2.0.1
my-new-package
I can pip install the package locally, and it uses the keyrings.google-artifactregistry-auth package to authenticate.
The deployment fails with:
File "/opt/python3.8/lib/python3.8/site-packages/pip/_internal/utils/misc.py", line 218, in ask_input
return input(message)
EOFError: EOF when reading a line
This is clearly pip asking for a username, where i
Is google's own keyring package not available in its own environment? This Suggests so. Of course adding it to requirements.txt has no effect, as it is too late.
How can I install packages properly?
A possible solution is to:
--extra-index-url=_json_key_base64:KEY#us-central1-python.pkg.dev/myproject/my-python-repo/simple/
Where KEY is the result of base64 -w 0 < credentials.json
Of course this then requires some rewriting of the requirements.txt file in your deployment pipeline to ensure service account credentials are secured in repository variables.

Error with module using Cloud Storage with Python and his tutorial

I'm trying to test Google Cloud Storage to store images (I need it in an app that I'm developing) and I'm following the Bookshelf App tutorial that they have in his webpage.
I'm using python and the problem is that when I execute the requirementes.txt all packages have been installed fine, but when I try execute the code, I see this error:
...sandbox.py", line 948, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named cryptography.hazmat.bindings._openssl
I have been trying hundred of posibles solutions, reinstalling only the cryptography package, trying to use different versions of the same module, and installing other packages that contains it but anything resolved the problem.
The requirements contains this:
Flask==0.10.1
gcloud==0.9.0
gunicorn==19.4.5
oauth2client==1.5.2
Flask-SQLAlchemy==2.1
PyMySQL==0.7.1
Flask-PyMongo==0.4.0
PyMongo==3.2.1
six==1.10.0
I'm sure that it is a simple error but I don't find the way to solve it.
Any help will be welcome. Thanks.
EDIT:
When I try do this with a python program this work fine:
import os
from gcloud import storage
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'key.json'
client = storage.Client(project='xxxxxxx')
bucket = client.get_bucket('yyyyyyy')
f = open('profile.jpg', 'rb')
blob = bucket.blob(f.name)
blob.upload_from_string(f.read(), 'image/jpeg')
url = blob.public_url
print url
Why I don't can use gcloud library without erros in a GAE app?
It seems you're following the bookshelf tutorial, but according to this line in your stacktrace:
...sandbox.py", line 948, in load_module
It hints that you're using dev_appserver.py to run the code. This isn't necessary for Managed VMs/Flexible unless you're using the compat runtime.
If this is the case, the tutorial provides correct instructions:
$ virtualenv env
$ source env/bin/activate
$ pip install -r requirements.txt
$ python main.py
(If this is not the case, please feel free to comment on this with more details about how you're running your application).

Google API access using Service Account oauth2client.client.CryptoUnavailableError: No crypto library available

I am trying to create a service account app so that I can access Google Analytics api using Python.
Two things are confusing me. First, when I use the following code:
`from oauth2client.client import SignedJwtAssertionCredentials
client_email = "#####client_email#######.gserviceaccount.com"
with open("XXXXXX.p12") as f:
private_key = f.read()
credentials = SignedJwtAssertionCredentials(client_email, private_key,'https://www.googleapis.com/auth/sqlservice.admin')`
I get the following error:
`oauth2client.client.CryptoUnavailableError: No crypto library available`
After doing a little research I found that this might have to do with granting the app domain-wide authority to the service account. However, when I log on to the Google Developers Console I cannot locate the security icon or the more-options button. Any help much appreciated thank you.
This did the trick for me (without converting to PEM):
pip install PyOpenSSL
pip freeze says I have version 0.15.1
That error probably means you need the python-openssl package.
apt-get install python-openssl
Even if you are installed pycrypto & python-ssl libraries in your development environment, You need to add this pycrypto library in your application's app.yaml file.
libraries:
- name: pycrypto
version: "latest"
I just recently set this up but opted to go with PyCrypto 2.6.1, but you can also use python-openssl as mentioned in the previous answer.
The only problem I had and I can't pinpoint this down, but the P12 key generated by the Google Developer Console wasn't working with my Service Account API call (to the Content API for Shopping), and I had to switch the key to the PEM format to get things going.
My setup: (Win7, python 2.7.x, PyCrypto 2.6.1)
The error I got when trying to use the P12 key, but later resolved when converting it to PEM:
Error 3: PKCS12 format is not supported by the PyCrypto library.
NotImplementedError: PKCS12 format is not supported by the PyCrypto library. Try converting to a “PEM” (openssl pkcs12 -in xxxxx.p12 -nodes -nocerts > privatekey.pem) or using PyOpenSSL if native code is an option.
One important thing, don't forget to go inside Google Analytics and grant the appropriate permissions for the client email address that is created during the creation of the Service Account.
OSX 10.11 El Capitan does not distribute OpenSSL anymore. I was able to install cryptography using Homebrew and static build:
env CRYPTOGRAPHY_OSX_NO_LINK_FLAGS=1 LDFLAGS="$(brew --prefix openssl)/lib/libssl.a $(brew --prefix openssl)/lib/libcrypto.a" CFLAGS="-I$(brew --prefix openssl)/include" pip install cryptography
More info
http://cryptography.readthedocs.org/en/latest/installation/

I want to download code from Google App Engine

I want to update the app in google app store.
But I can't download the code...
Is there any way to update the app without downloading the code?
I tried to download with python, google app engine SDK...
But
appcfg.py download_app -A
This command does not work giving this error
NameError: global name 'execfile' is not defined...
Can you help me with this?
The error you have shown may occur due to incorrect PYTHONPATH environment variable.
If you are using the Windows version of the GAE SDK, then do the following:
1) Go to Edit > Preferences
2) Correct your Python Path.
To know the Python Path in windows do the following in the Python IDLE or Python CMD:
import os
import sys
print os.path.dirname(sys.executable)
For downloading your source code try this:
download_app -A app_name -V version C:\path_to_project
You may or may not need to escape the backslash.
Replace app_name, version and C:\path_to_project with appropriate values
To know the version go to the app engine admin website appengine.appspot.com

Resources