我正在尝试设置一个Cloud Function,该功能将文件存储桶中的文件移动和删除到同一项目的Windows实例中。当然,如果我们从实例在本地运行它并使用gsutil,就可以使其正常运行。
但是,我们如何在本地文件夹位置的云功能脚本中对VM路径进行编码?
我还共享了VM内的本地文件夹。
非常感谢您的投入。
下面是代码,
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服务器复制文件。这些方法都不容易在Cloud Function中实现。
Windows CIFS文件共享
此方法涉及启用Windows共享。AFAIK没有简单的SAMBA客户端可以在Cloud Functions中实现。
SFTP
此方法需要为客户端设置Windows Server SSH服务器和SSH密钥对(云功能)。有一些Python SSH客户端库(paramiko)可以与Cloud Functions一起使用。通过paramiko可以轻松实现使用SFTP传输文件。
REST API服务器
此方法需要创建自己的软件,该软件提供REST API(或类似技术),Cloud Functions可以通过HTTPS调用该API。您将需要管理授权。实现自己的API和安全性会带来巨大的风险。
建议
云功能是与Windows Server连接的错误服务。我建议在调用的Windows Server而不是Cloud Function上创建HTTP端点。现在,您已从设计方程式中删除了Windows Server授权。您的Python代码可以直接在与Cloud Storage连接的Windows上运行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句