我正在尝试设置一个云函数,它将文件从存储桶移动并删除到同一个项目上的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()谢谢!
发布于 2020-02-27 04:32:11
有几种方法可以将文件复制到/从Windows服务器。在云函数中实现这些方法都不简单。
Windows文件共享
此方法涉及启用Windows共享。AFAIK没有简单的SAMBA客户端可以在云函数中实现。
SFTP
此方法需要为客户端(云函数)设置Windows Server SSH服务器和SSH密钥对。有一些Python客户端库(paramiko)可以用于云函数。使用SFTP传输文件很容易通过paramiko实现。
REST服务器
此方法需要创建您自己的软件,该软件提供了云函数可以通过HTTPS调用的REST (或类似技术)。您需要管理授权。实现自己的API和安全性会带来很大的风险。
RECOMMENDATION
云功能是与Windows服务器接口的错误服务。我建议在Windows上创建一个HTTP端点,该端点将被调用,而不是云函数。现在,您已经从设计公式中删除了Windows授权。Python代码可以直接在Windows与云存储接口上运行。
https://stackoverflow.com/questions/60384219
复制相似问题