我正在尝试设置一个云功能,它将文件从存储桶移动并删除到同一项目上的 Windows 实例。当然,如果我们从实例本地运行它并使用 gsutil,我们就可以让它工作。
但是我们如何获取本地文件夹位置的云函数脚本中编码的虚拟机路径?
我还在虚拟机内共享了本地文件夹。
非常感谢您的投入。
下面是代码,
import logging
from google.cloud import storage
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="serviceaccount.json"
#logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
bucket_name = 'bucket name'
#File_name = 'filename'
# Instance/VM location where the files should be downloaded into (VM NAME)
folder = '//VW123456789/Cloud-Files'
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blobs = bucket.list_blobs() #List all objects that satisfy the filter.
# Download the files inside windows-VM on GCP
def download_to_local():
logging.info('File download Started...Please wait for the job to complete!')
# Create this folder locally if not exists
if not os.path.exists(folder):
os.makedirs(folder)
# Iterating through for loop one by one using API call
for blob in blobs:
logging.info('Blobs: {}'.format(blob.name))
destination_files = '{}/{}'.format(folder, blob.name)
blob.download_to_filename(destination_files)
logging.info('Exported {} to {}'.format(blob.name, destination_files))
blob.delete()
if __name__ == '__main__':
download_to_local()
谢谢!
最佳答案
有几种方法可以将文件复制到 Windows 服务器或从 Windows 服务器复制文件。这些方法在云函数中实现起来都不容易。
Windows CIFS 文件共享
此方法涉及启用 Windows 共享。据我所知,没有可以在 Cloud Functions 中实现的简单 SAMBA 客户端。
SFTP
此方法需要为客户端(云函数)设置Windows Server SSH服务器和SSH key 对。有一些可与 Cloud Functions 配合使用的 Python SSH 客户端库 (paramiko)。通过 paramiko 可以轻松实现使用 SFTP 传输文件。
REST API 服务器
此方法需要创建您自己的软件,该软件提供 Cloud Functions 可以通过 HTTPS 调用的 REST API(或类似技术)。您将需要管理授权。实现您自己的 API 和安全性会带来重大风险。
推荐
Cloud Functions 是与 Windows 服务器交互的错误服务。我建议在调用的 Windows Server 上创建一个 HTTP 端点,而不是 Cloud Function。现在您已从设计方程式中删除了 Windows Server 授权。您的 Python 代码可以直接在与 Cloud Storage 连接的 Windows 上运行。
关于python-3.x - 使用 Cloud Functions 将文件从 Google Cloud Storage 传输到 Windows VM 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60384219/