简体   繁体   English

如何在 Google Cloud 上提供 REST API?

[英]How to serve REST API on Google Cloud?

I have made a REST API for my Ubuntu machine on Google Cloud.我在 Google Cloud 上为我的 Ubuntu 机器制作了一个 REST API。 How can I connect this API with public IP address?如何将此 API 与公共 IP 地址连接?

In the below code I am uploading image file in multipart/form-data format.在下面的代码中,我以 multipart/form-data 格式上传图像文件。

from flask import Flask, url_for, send_from_directory, request
import logging, os
# from werkzeug import secure_filename
import werkzeug
app = Flask(__name__)
file_handler = logging.FileHandler('server.log')
app.logger.addHandler(file_handler)
app.logger.setLevel(logging.INFO)

PROJECT_HOME = os.path.dirname(os.path.realpath(__file__))
UPLOAD_FOLDER = '{}/uploads/'.format(PROJECT_HOME)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER


def create_new_folder(local_dir):
    newpath = local_dir
    if not os.path.exists(newpath):
        os.makedirs(newpath)
    return newpath

@app.route('/Classifier01', methods = ['POST'])
def api_upload():


    app.logger.info(PROJECT_HOME)
    if request.method == 'POST':
      app.logger.info(app.config['UPLOAD_FOLDER'])

      #get image and save in ../uploads
      img = request.files['image']
      img_name = werkzeug.secure_filename(img.filename)
      create_new_folder(app.config['UPLOAD_FOLDER'])
      saved_path = os.path.join(app.config['UPLOAD_FOLDER'], img_name)
      app.logger.info("saving {}".format(saved_path))
      img.save(saved_path)

      #do some processing here

      return "I got your image"
    else:
      return "Where is the image?"

if __name__ == '__main__':
    app.run()

It start on 127.0.0.1:5000 how make it on public IP.它从127.0.0.1:5000开始,如何在公共 IP 上创建它。

First, start your app to listen on interface of your VM -> run it like this app.run(host='0.0.0.0')首先,启动您的应用程序以侦听 VM 的接口 -> 像这样运行app.run(host='0.0.0.0')

Then, you have 2 solutions那么,你有2个解决方案

  • Use the public IP of your VM to reach it.使用 VM 的公共 IP 访问它。 Don't forget to open the correct firewall rule for this (source IP range= 0.0.0.0/0, port TCP 5000).不要忘记为此打开正确的防火墙规则(源 IP 范围= 0.0.0.0/0,端口 TCP 5000)。 But it's not perfect.但它并不完美。 You can update your app for listening on the port 80 for a more standard API port on internet.您可以更新您的应用程序以侦听端口 80,以获取 Internet 上更标准的 API 端口。 You can also plug an nginx on your VM.您还可以在 VM 上插入 nginx。 On it, you can also deploy a SSL certificate and allow to reach your service on the port 443 in HTTPS.在它上面,您还可以部署 SSL 证书并允许在 HTTPS 的 443 端口上访问您的服务。 pff, lot of work pff,很多工作
  • You can use HTTP(s) load balancer for this.您可以为此使用HTTP(s) 负载平衡器 SSL in automatically managed. SSL 在自动管理中。 Simply create a backend with your VM (a NEG), your health check and set up your frontend.只需使用您的 VM(一个 NEG)创建一个后端、您的健康检查并设置您的前端。 It's the easiest way!这是最简单的方法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何提供Google Cloud Storage映像? - How to serve Google Cloud Storage images? Google Cloud App Engine:如何在灵活的环境中提供https - Google Cloud App Engine: How to serve https in a Flexible environment 如何使用python flask从Google云存储提供图像 - How to serve an image from google cloud storage using python flask Rest API Google Cloud-创建/编辑/列出 API 密钥 - Rest API Google Cloud- create/edit/list API Keys 如何使用 Python api 更新 Google Cloud 调度程序 - How to update the Google Cloud scheduler with Python api 如何使用App Engine app.yaml文件中定义的路由从Google Cloud Storage提供内容? - How to serve content from Google Cloud Storage with routes defined in App Engine app.yaml file? 如何使用自己的域 www.yourdomain.com 来服务 Google Cloud Storage 内容 - How to use your own domain www.yourdomain.com to serve Google Cloud Storage content 将 Pi 上的 REST API 连接到云 - Connect REST API on Pi to the Cloud GCP - 将数据从 REST API 插入/加载到 BigQuery,使用 Google Cloud Function 和 ZA7F5F35426B927824717BFCFC11 - GCP - Insert/Load Data to BigQuery from REST API using Google Cloud Function with Python 如何使用python发送rest API(Google Vision的API)请求? - How to send rest API (Google Vision's API) request with python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM