我正在尝试使用api创建云函数,该源代码将以zip归档文件的形式提供,其中包括index.js和package.json文件。 我已将该存档上传到存储桶并通过API请求创建了云功能,但是现在我需要提取该zip存档以指出云功能的来源,我该如何实现?
这是我所做的: 来自views.py
sclient = storage.Client()
bucket = sclient.get_bucket(func_obj.bucket)
blob = bucket.blob(func_obj.sourceFile.name)
print('Uploading archive...')
print(blob.upload_from_filename(file_name))
name = "projects/{}/locations/us-central1/functions/{}".format(func_obj.project, func_obj.fname,)
print(name)
req_body = {
"name": name,
"entryPoint": func_obj.entryPoint,
"timeout": "3.5s",
"availableMemoryMb": func_obj.fmemory,
# "sourceUploadUrl": upload_url,
"sourceArchiveUrl": "gs://newer_bucket/func.zip",
"httpsTrigger": {},
}
service = discovery.build('cloudfunctions', 'v1')
func_api = service.projects().locations().functions()
response = func_api.create(location='projects/' + func_obj.project + '/locations/us-central1',
body=req_body).execute()
pprint.pprint(response)
根据Cloud Function Docs , functions.create
REST端点的请求主体必须包含CloudFunction的实例。 在第二个链接中可以看到,云功能JSON资源的字段之一是"sourceArchiveUrl"
,它指向包含该功能的zip存档 。 据我了解,您无需为 Google Cloud Functions提取zip存档即可访问代码。 检查Cloud Functions的非REST API部署 ; 它需要档案,而不是提取的档案。
但是无论如何,没有直接的方法可以在存储桶中提取档案。 您需要将存档下载/传输到某处(例如本地计算机,Google Compute Engine VM实例等),对其进行提取操作,然后将结果上传回存储桶中。 如果随后需要提取存档,为什么还要将存档上传到Google Cloud Storage存储桶? 为什么不上传文件?
您可能还想查看此答案 。 相同的问题,不同的上下文。