Nav*_*eed 5 python-3.x google-cloud-platform google-cloud-functions
我已经设置了 3 个 Google Cloud Storge 存储桶和 3 个函数(每个存储桶一个),它们将在 PDF 文件上传到存储桶时触发。函数将 PDF 转换为 png 图像并进行进一步处理。
当我尝试创建第四个存储桶和类似的功能时,奇怪的是它不起作用。即使我复制了现有的 3 个函数之一,它仍然无法正常工作,并且出现此错误:
Traceback (most recent call last): File "/env/local/lib/python3.7/site-packages/google/cloud/functions_v1beta2/worker.py", line 333, in run_background_function _function_handler.invoke_user_function(event_object) File "/env/local/lib/python3.7/site-packages/google/cloud/functions_v1beta2/worker.py", line 199, in invoke_user_function return call_user_function(request_or_event) File "/env/local/lib/python3.7/site-packages/google/cloud/functions_v1beta2/worker.py", line 196, in call_user_function event_context.Context(**request_or_event.context)) File "/user_code/main.py", line 27, in pdf_to_img with Image(filename=tmp_pdf, resolution=300) as image: File "/env/local/lib/python3.7/site-packages/wand/image.py", line 2874, in __init__ self.read(filename=filename, resolution=resolution) File "/env/local/lib/python3.7/site-packages/wand/image.py", line 2952, in read self.raise_exception() File "/env/local/lib/python3.7/site-packages/wand/resource.py", line 222, in raise_exception raise e wand.exceptions.PolicyError: not authorized/tmp/tmphm3hiezy'@error/constitute.c/ReadImage/412`
令我感到困惑的是,为什么相同的功能可以在现有存储桶上运行,但在新存储桶上不起作用。
更新:即使这不起作用(出现“缓存资源耗尽”错误):
在requirements.txt:
google-cloud-storage
wand
Run Code Online (Sandbox Code Playgroud)
在main.py:
import tempfile
from google.cloud import storage
from wand.image import Image
storage_client = storage.Client()
def pdf_to_img(data, context):
file_data = data
pdf = file_data['name']
if pdf.startswith('v-'):
return
bucket_name = file_data['bucket']
blob = storage_client.bucket(bucket_name).get_blob(pdf)
_, tmp_pdf = tempfile.mkstemp()
_, tmp_png = tempfile.mkstemp()
tmp_png = tmp_png+".png"
blob.download_to_filename(tmp_pdf)
with Image(filename=tmp_pdf) as image:
image.save(filename=tmp_png)
print("Image created")
new_file_name = "v-"+pdf.split('.')[0]+".png"
blob.bucket.blob(new_file_name).upload_from_filename(tmp_png)
Run Code Online (Sandbox Code Playgroud)
上面的代码应该只是创建一个上传到存储桶的图像文件的副本。
由于该漏洞已在 Ghostscript 中修复,但在 ImageMagick 中未更新,因此在 Google Cloud Functions 中将 PDF 转换为图像的解决方法是使用此Ghostscript 包装器并直接从 Ghostscript 请求将 PDF 转换为 png(绕过 ImageMagick)。
要求.txt
google-cloud-storage
ghostscript==0.6
Run Code Online (Sandbox Code Playgroud)
主要.py
import locale
import tempfile
import ghostscript
from google.cloud import storage
storage_client = storage.Client()
def pdf_to_img(data, context):
file_data = data
pdf = file_data['name']
if pdf.startswith('v-'):
return
bucket_name = file_data['bucket']
blob = storage_client.bucket(bucket_name).get_blob(pdf)
_, tmp_pdf = tempfile.mkstemp()
_, tmp_png = tempfile.mkstemp()
tmp_png = tmp_png+".png"
blob.download_to_filename(tmp_pdf)
# create a temp folder based on temp_local_filename
# use ghostscript to export the pdf into pages as pngs in the temp dir
args = [
"pdf2png", # actual value doesn't matter
"-dSAFER",
"-sDEVICE=pngalpha",
"-o", tmp_png,
"-r300", tmp_pdf
]
# the above arguments have to be bytes, encode them
encoding = locale.getpreferredencoding()
args = [a.encode(encoding) for a in args]
#run the request through ghostscript
ghostscript.Ghostscript(*args)
print("Image created")
new_file_name = "v-"+pdf.split('.')[0]+".png"
blob.bucket.blob(new_file_name).upload_from_filename(tmp_png)
Run Code Online (Sandbox Code Playgroud)
不管怎样,这可以让你解决这个问题,并为你保留 GCF 中的所有处理。希望能帮助到你。不过,您的代码适用于单页 PDF。我的用例是多页 pdf 转换、ghostscript 代码和这个问题的解决方案。
| 归档时间: |
|
| 查看次数: |
1157 次 |
| 最近记录: |