有一个共享目录(挂载路径)用作源。目的地是一个存储桶。我编写了以下云函数,该云函数在执行期间会失败。从命令行,我可以运行rsynch功能。
def synch_data(request):
try:
proc = subprocess.Popen(["gsutil", "-m", "rsync", "/home/raw/abc_ftp_data", "gs://abc-raw"])
print("PROC is instantiated ____")
outs, errs = proc.communicate(timeout=15)
except Exception as e:
print("Exception Occure ...........",e)
outs, errs = proc.communicate()
return 'Success!'
我收到的错误如下。
[Errno 2] No such file or directory: 'gsutil': 'gsutil'"
有人可以在这种情况下提供帮助吗?
答案 0 :(得分:0)
由于您使用的是在Python中运行的Google Cloud Function,因此建议您使用Google Cloud Storage Python API将文件上传到Cloud Storage Bucket。
可以在Cloud Storage > Uploading objects文档中找到该示例。
这是一个示例,您可以根据需要进行修改:
首先在requirements.txt
文件中添加Google Cloud Storage库:
# Function dependencies, for example:
# package>=version
google-cloud-storage
然后添加此附加功能并从您的主功能中调用它:
def copyToBucket(bucket_name, source_file_name, destination_blob_name):
# Import the library
from google.cloud import storage
""" Uploads a file to the bucket. """
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print('File {} uploaded to {}.'.format(
source_file_name,
destination_blob_name))
然后您可以像这样从主函数调用该函数:
copyToBucket("[BUCKET_NAME]", "/PATH/TO/FILE/IN/SHARED/DIRECTORY/FILE_NAME.py", "NEW_FILE_NAME.py")
只需在调用函数时修改传递给该函数的参数,您的文件就会上传到所需的Cloud Storage Bucket。我已经测试了代码,并且对我有用。