![](/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.