JPG is uploaded to a bucket https://storage.googleapis.com/ .... Should I use blobs or get_serving_url
API?
...I have tried a couple of different methods but keep getting an error. Here is the Hello World example:
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, World, Hello!')
app = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
If you want to use App Engine standard, you'll need to complete a few steps first:
cloudstorage
client library Then you can take the quickstart project for App Engine Standard Environment and modify the main.py
file to look like:
import webapp2
import cloudstorage as gcs
class MainPage(webapp2.RequestHandler):
def get(self):
gcs_file = gcs.open('/your-bucket-name/your-image.jpg')
contents = gcs_file.read()
gcs_file.close()
self.response.write(contents)
self.response.headers['Content-Type'] = 'image/jpeg'
app = webapp2.WSGIApplication([('/', MainPage)], debug=True)
When you're done you should have a directory structure with the following structure:
.
├── app.yaml
├── appengine_config.py
├── lib
│ └── cloudstorage
│ ├── __init__.py
│ ├── api_utils.py
│ ├── cloudstorage_api.py
│ ├── common.py
│ ├── errors.py
│ ├── rest_api.py
│ ├── storage_api.py
│ └── test_utils.py
└── main.py
And you can deploy it with:
$ gcloud app deploy
That said, you also don't really need a full-blown App Engine instance to serve a single image -- you could also just do it with a Google Cloud Function like so:
In requirements.txt
:
google-cloud-storage
In main.py
:
from google.cloud import storage
from flask import make_response
client = storage.Client()
bucket = client.get_bucket('your-bucket-name')
blob = bucket.get_blob('your-image.jpg').download_as_string()
def serve_image(request):
return make_response((blob, {'Content-Type': 'image/jpeg'}))
And deploy it with:
$ gcloud beta functions deploy serve_image --runtime python37 --trigger-http
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.