我在这里编写了一个Python程序,它连接了各个平台的API以用于文件下载。该程序当前正在我的本地计算机(笔记本电脑)上运行,没有任何问题(当然,所有下载的文件都保存到我的本地驱动器中)。
这是我真正的问题,如果没有 Google Compute Engine,是否可以使用 Google App Engine 部署相同的 python 程序?如果是,我如何将我的文件(通过 API 调用)保存到此处的 Google Cloud Storage?
谢谢。
最佳答案
这是一个网络应用程序吗?如果是这样,您可以使用 GOOGLE APP ENGINE standard 来部署它或flexible .
要将文件发送到 Cloud Storage,请尝试 python-docs-samples 中的示例存储库(文件夹 appengine/flexible/storage/
):
# [START upload]
@app.route('/upload', methods=['POST'])
def upload():
"""Process the uploaded file and upload it to Google Cloud Storage."""
uploaded_file = request.files.get('file')
if not uploaded_file:
return 'No file uploaded.', 400
# Create a Cloud Storage client.
gcs = storage.Client()
# Get the bucket that the file will be uploaded to.
bucket = gcs.get_bucket(CLOUD_STORAGE_BUCKET)
# Create a new blob and upload the file's content.
blob = bucket.blob(uploaded_file.filename)
blob.upload_from_string(
uploaded_file.read(),
content_type=uploaded_file.content_type
)
# The public URL can be used to directly access the uploaded file via HTTP.
return blob.public_url
# [END upload]
关于python - 是否可以使用 Google App Engine(不带 Google Compute Engine)通过 API 调用(使用 Python)将文件下载到 Google Cloud Storage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50269040/