![](/img/trans.png)
[英]How can I extract a tar.gz file in a Google Cloud Storage bucket from a Colab Notebook?
[英]How can I use a Google Cloud Function to push a file from a Cloud Storage bucket into an instance?
我有一个任务分配给我,想办法在 GCP 中设置一个云功能,它执行以下操作:
监控 Google Cloud Storage 存储桶中的新文件
当它检测到存储桶中的新文件时触发
将该文件复制到计算实例 (Ubuntu) 内的目录中
我一直在做一些研究,结果是空的。 我知道我可以轻松地设置一个 cron 作业,每分钟同步一次存储桶/目录或类似的东西,但我们正在构建的系统的设计理念之一是关闭触发器而不是计时器。
我问的可能吗?
您可以从 Google Cloud Storage 存储桶中触发 Cloud Function ,并通过将事件类型选择为Finalize/Create ,每次在存储桶中上传文件时,都会调用 Cloud Function。
每次在存储桶中创建新对象时,云函数都会收到一个带有Cloud Storage 对象格式的通知。
现在,进入第二步,我找不到任何可以将文件从 Cloud Storage 上传到实例 VM 的 API。 但是,我做了以下解决方法,假设您的实例 VM 配置了一个可以接收 HTTP 请求的服务器(例如 Apache 或 Nginx):
主文件
import requests
from google.cloud import storage
def hello_gcs(data, context):
"""Background Cloud Function to be triggered by Cloud Storage.
Args:
data (dict): The Cloud Functions event payload.
context (google.cloud.functions.Context): Metadata of triggering event.
Returns:
None; the file is sent as a request to
"""
print('Bucket: {}'.format(data['bucket']))
print('File: {}'.format(data['name']))
client = storage.Client()
bucket = client.get_bucket(data['bucket'])
blob = bucket.get_blob(data['name'])
contents = blob.download_as_string()
headers = {
'Content-type': 'text/plain',
}
data = '{"text":"{}"}'.format(contents)
response = requests.post('https://your-instance-server/endpoint-to-download-files', headers=headers, data=data)
return "Request sent to your instance with the data of the object"
要求.txt
google-cloud-storage
requests
最有可能的是,最好将对象名称和存储桶名称发送到您的服务器端点,然后使用Cloud Client Library从那里下载文件。
现在你可能会问...
如何制作 Compute Engine 实例来处理请求?
创建 Compute Engine 实例虚拟机。 确保它与云函数位于同一区域,并在创建时允许 HTTP 连接到它。 文档。 我在这个测试中使用了debian-9
镜像。
SSH 进入实例,并运行以下命令:
安装apache服务器
sudo apt-get update sudo apt-get install apache2 sudo apt-get install libapache2-mod-wsgi
也安装这个 python 库:
sudo apt-get install python-pip sudo pip install flask
为您的应用程序设置环境:
cd ~/ mkdir app sudo ln -sT ~/app /var/www/html/app
最后一行应该指向 apache 从中提供 index.html 文件的文件夹路径。
/home/<user_name>/app
创建您的应用/home/<user_name>/app
:主文件
from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=['POST'])
def receive_file():
file_content = request.form['data']
# TODO
# Implement process to save this data onto a file
return 'Hello from Flask!'
if __name__ == '__main__':
app.run()
主文件
import sys
sys.path.insert(0, '/var/www/html/app')
from main import app as application
/etc/apache2/sites-enabled/000-default.conf
下行添加到/etc/apache2/sites-enabled/000-default.conf
的DocumentRoot
标记之后:
WSGIDaemonProcess flaskapp threads=5 WSGIScriptAlias / /var/www/html/app/main.wsgi <Directory app> WSGIProcessGroup main WSGIApplicationGroup %{GLOBAL} Order deny,allow Allow from all </Directory>
运行sudo apachectl restart
。 您应该能够将 post 请求发送到您的应用程序,发送到 VM 实例的内部 IP(您可以在控制台的 Compute Engine 部分中看到它)。 一旦你有了它,在你的云函数中,你应该将响应行更改为:
response = requests.post('<INTERNAL_INSTANCE_IP>/', headers=headers, data=data) return "Request sent to your instance with the data of the object"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.